Revised input capturing.
This commit is contained in:
parent
7950187cb7
commit
21f546dc52
4 changed files with 149 additions and 130 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -8,7 +8,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kitchentimer"
|
name = "kitchentimer"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"signal-hook",
|
"signal-hook",
|
||||||
"termion",
|
"termion",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kitchentimer"
|
name = "kitchentimer"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Shy <shy@posteo.de>"]
|
authors = ["Shy <shy@posteo.de>"]
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
272
src/lib.rs
272
src/lib.rs
|
@ -32,7 +32,7 @@ pub use alarm::AlarmRoster;
|
||||||
use alarm::Countdown;
|
use alarm::Countdown;
|
||||||
use buffer::Buffer;
|
use buffer::Buffer;
|
||||||
use clock::{font, Clock};
|
use clock::{font, Clock};
|
||||||
pub use consts::ui::*;
|
use consts::ui::*;
|
||||||
use cradle::Cradle;
|
use cradle::Cradle;
|
||||||
use layout::Layout;
|
use layout::Layout;
|
||||||
use signal_hook::consts::signal::*;
|
use signal_hook::consts::signal::*;
|
||||||
|
@ -40,6 +40,7 @@ use signal_hook::iterator::Signals;
|
||||||
use signal_hook::low_level;
|
use signal_hook::low_level;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::{env, process, thread, time};
|
use std::{env, process, thread, time};
|
||||||
|
use std::sync::mpsc;
|
||||||
use termion::event::Key;
|
use termion::event::Key;
|
||||||
use termion::input::TermRead;
|
use termion::input::TermRead;
|
||||||
use termion::raw::{IntoRawMode, RawTerminal};
|
use termion::raw::{IntoRawMode, RawTerminal};
|
||||||
|
@ -55,9 +56,6 @@ pub fn run(
|
||||||
let mut clock = Clock::new(&config);
|
let mut clock = Clock::new(&config);
|
||||||
let mut countdown = Countdown::new();
|
let mut countdown = Countdown::new();
|
||||||
let mut buffer = Buffer::new();
|
let mut buffer = Buffer::new();
|
||||||
|
|
||||||
let async_stdin = termion::async_stdin();
|
|
||||||
let mut input_keys = async_stdin.keys();
|
|
||||||
let stdout = std::io::stdout();
|
let stdout = std::io::stdout();
|
||||||
let mut stdout = stdout.lock().into_raw_mode()?;
|
let mut stdout = stdout.lock().into_raw_mode()?;
|
||||||
let mut force_redraw = true;
|
let mut force_redraw = true;
|
||||||
|
@ -67,6 +65,17 @@ pub fn run(
|
||||||
SIGTSTP, SIGCONT, SIGWINCH, SIGTERM, SIGINT, SIGUSR1, SIGUSR2,
|
SIGTSTP, SIGCONT, SIGWINCH, SIGTERM, SIGINT, SIGUSR1, SIGUSR2,
|
||||||
])?;
|
])?;
|
||||||
|
|
||||||
|
// Read input keys and send them back to the main thread.
|
||||||
|
let tty = termion::get_tty()?;
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
thread::spawn(move || {
|
||||||
|
for key in tty.keys() {
|
||||||
|
if tx.send(key).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Main loop entry.
|
// Main loop entry.
|
||||||
loop {
|
loop {
|
||||||
// Process received signals.
|
// Process received signals.
|
||||||
|
@ -207,131 +216,138 @@ pub fn run(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process input.
|
// Process input.
|
||||||
if let Some(key) = input_keys.next() {
|
match rx.recv_timeout(time::Duration::from_millis(250)) {
|
||||||
match key.expect("Error reading input") {
|
// Timeout. Expected.
|
||||||
// Enter.
|
Err(mpsc::RecvTimeoutError::Timeout) => (),
|
||||||
Key::Char('\n') => {
|
// Disconnect.
|
||||||
if !buffer.is_empty() {
|
// TODO: When? Why? What to do?
|
||||||
if let Err(e) = alarm_roster.add(buffer.read()) {
|
Err(mpsc::RecvTimeoutError::Disconnected) => {
|
||||||
// Error while processing input buffer.
|
eprintln!("Unexpected disconnect from input thread.");
|
||||||
buffer.message(e);
|
break;
|
||||||
} else {
|
}
|
||||||
// Input buffer processed without error.
|
Ok(key) => {
|
||||||
layout.set_roster_width(alarm_roster.width());
|
match key.expect("Error reading input") {
|
||||||
}
|
// Enter.
|
||||||
buffer.clear();
|
Key::Char('\n') => {
|
||||||
buffer.visible = false;
|
if !buffer.is_empty() {
|
||||||
force_redraw = true;
|
if let Err(e) = alarm_roster.add(buffer.read()) {
|
||||||
}
|
// Error while processing input buffer.
|
||||||
}
|
buffer.message(e);
|
||||||
// Escape and ^U clear input buffer.
|
} else {
|
||||||
Key::Esc | Key::Ctrl('u') => {
|
// Input buffer processed without error.
|
||||||
buffer.reset();
|
layout.set_roster_width(alarm_roster.width());
|
||||||
buffer.visible = false;
|
}
|
||||||
force_redraw = true;
|
buffer.clear();
|
||||||
}
|
buffer.visible = false;
|
||||||
// ^W removes last word.
|
force_redraw = true;
|
||||||
Key::Ctrl('w') => {
|
}
|
||||||
buffer.strip_word();
|
}
|
||||||
if buffer.is_empty() {
|
// Escape and ^U clear input buffer.
|
||||||
buffer.visible = false;
|
Key::Esc | Key::Ctrl('u') => {
|
||||||
force_redraw = true;
|
buffer.reset();
|
||||||
}
|
buffer.visible = false;
|
||||||
}
|
force_redraw = true;
|
||||||
// Backspace.
|
}
|
||||||
Key::Backspace => {
|
// ^W removes last word.
|
||||||
// Delete last char in buffer.
|
Key::Ctrl('w') => {
|
||||||
buffer.strip_char();
|
buffer.strip_word();
|
||||||
if buffer.is_empty() {
|
if buffer.is_empty() {
|
||||||
buffer.visible = false;
|
buffer.visible = false;
|
||||||
force_redraw = true;
|
force_redraw = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Set clock.
|
// Backspace.
|
||||||
Key::Up if clock.paused => {
|
Key::Backspace => {
|
||||||
clock.shift(10);
|
// Delete last char in buffer.
|
||||||
// We would very likely not detect us passing the hour
|
buffer.strip_char();
|
||||||
// barrier and would panic when trying to draw hours
|
if buffer.is_empty() {
|
||||||
// without position if we do not schedule a recalculation
|
buffer.visible = false;
|
||||||
// here.
|
force_redraw = true;
|
||||||
layout.schedule_recalc();
|
}
|
||||||
force_redraw = true;
|
}
|
||||||
}
|
// Set clock.
|
||||||
Key::Down if clock.paused => {
|
Key::Up if clock.paused => {
|
||||||
clock.shift(-10);
|
clock.shift(10);
|
||||||
alarm_roster.time_travel(&mut clock);
|
// We would very likely not detect us passing the hour
|
||||||
layout.schedule_recalc();
|
// barrier and would panic when trying to draw hours
|
||||||
force_redraw = true;
|
// without position if we do not schedule a recalculation
|
||||||
}
|
// here.
|
||||||
// Scroll alarm roster.
|
layout.schedule_recalc();
|
||||||
Key::PageUp => {
|
force_redraw = true;
|
||||||
alarm_roster.scroll_up(&layout);
|
}
|
||||||
force_redraw = true;
|
Key::Down if clock.paused => {
|
||||||
}
|
clock.shift(-10);
|
||||||
Key::PageDown => {
|
alarm_roster.time_travel(&mut clock);
|
||||||
alarm_roster.scroll_down(&layout);
|
layout.schedule_recalc();
|
||||||
force_redraw = true;
|
force_redraw = true;
|
||||||
}
|
}
|
||||||
// Forward every char if in insert mode.
|
// Scroll alarm roster.
|
||||||
Key::Char(c) if buffer.visible => {
|
Key::PageUp => {
|
||||||
buffer.push(c);
|
alarm_roster.scroll_up(&layout);
|
||||||
}
|
force_redraw = true;
|
||||||
// Reset clock on 'r'.
|
}
|
||||||
Key::Char('r') => {
|
Key::PageDown => {
|
||||||
clock.reset();
|
alarm_roster.scroll_down(&layout);
|
||||||
alarm_roster.reset_all();
|
force_redraw = true;
|
||||||
layout.schedule_recalc();
|
}
|
||||||
force_redraw = true;
|
// Forward every char if in insert mode.
|
||||||
}
|
Key::Char(c) if buffer.visible => {
|
||||||
// (Un-)Pause on space.
|
buffer.push(c);
|
||||||
Key::Char(' ') => {
|
}
|
||||||
clock.toggle();
|
// Reset clock on 'r'.
|
||||||
force_redraw = true;
|
Key::Char('r') => {
|
||||||
}
|
clock.reset();
|
||||||
// Clear clock color on 'c'.
|
alarm_roster.reset_all();
|
||||||
Key::Char('c') => {
|
layout.schedule_recalc();
|
||||||
clock.color_index = None;
|
force_redraw = true;
|
||||||
force_redraw = true;
|
}
|
||||||
}
|
// (Un-)Pause on space.
|
||||||
// Delete last alarm on 'd'.
|
Key::Char(' ') => {
|
||||||
Key::Char('d') => {
|
clock.toggle();
|
||||||
if alarm_roster.pop().is_some() {
|
force_redraw = true;
|
||||||
// If we remove the last alarm we have to reset "countdown"
|
}
|
||||||
// manually. It is safe to do it anyway.
|
// Clear clock color on 'c'.
|
||||||
layout.set_roster_width(alarm_roster.width());
|
Key::Char('c') => {
|
||||||
countdown.reset();
|
clock.color_index = None;
|
||||||
force_redraw = true;
|
force_redraw = true;
|
||||||
}
|
}
|
||||||
}
|
// Delete last alarm on 'd'.
|
||||||
// Exit on q and ^C.
|
Key::Char('d') => {
|
||||||
Key::Char('q') | Key::Ctrl('c') => break,
|
if alarm_roster.pop().is_some() {
|
||||||
// Force redraw on ^R.
|
// If we remove the last alarm we have to reset "countdown"
|
||||||
Key::Ctrl('r') => force_redraw = true,
|
// manually. It is safe to do it anyway.
|
||||||
// Suspend an ^Z.
|
layout.set_roster_width(alarm_roster.width());
|
||||||
Key::Ctrl('z') => {
|
countdown.reset();
|
||||||
suspend(&mut stdout)?;
|
force_redraw = true;
|
||||||
// Clear SIGCONT, as we have already taken care to reset
|
}
|
||||||
// the terminal.
|
}
|
||||||
//signal.compare_and_swap(SIGCONT, 0, Ordering::Relaxed);
|
// Exit on q and ^C.
|
||||||
force_redraw = true;
|
Key::Char('q') | Key::Ctrl('c') => break,
|
||||||
// Jump to the start of the main loop.
|
// Force redraw on ^R.
|
||||||
continue;
|
Key::Ctrl('r') => force_redraw = true,
|
||||||
}
|
// Suspend an ^Z.
|
||||||
Key::Char(c) => {
|
Key::Ctrl('z') => {
|
||||||
if c.is_ascii_digit() {
|
suspend(&mut stdout)?;
|
||||||
buffer.push(c);
|
// Clear SIGCONT, as we have already taken care to reset
|
||||||
buffer.visible = true;
|
// the terminal.
|
||||||
force_redraw = true;
|
//signal.compare_and_swap(SIGCONT, 0, Ordering::Relaxed);
|
||||||
} else if !buffer.is_empty() && c == ':' {
|
force_redraw = true;
|
||||||
buffer.push(':');
|
// Jump to the start of the main loop.
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
// Any other key.
|
Key::Char(c) => {
|
||||||
_ => (),
|
if c.is_ascii_digit() {
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.visible = true;
|
||||||
|
force_redraw = true;
|
||||||
|
} else if !buffer.is_empty() && c == ':' {
|
||||||
|
buffer.push(':');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Any other key.
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Main loop delay. Skipped after key press.
|
|
||||||
thread::sleep(time::Duration::from_millis(100));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,9 @@ fn main() {
|
||||||
eprintln!("Error while reading alarm times from stdin. ({})", e);
|
eprintln!("Error while reading alarm times from stdin. ({})", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// We don't need stdin anymore.
|
||||||
|
drop(stdin);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run main loop. Returns spawned child process if any.
|
// Run main loop. Returns spawned child process if any.
|
||||||
|
|
Loading…
Reference in a new issue