Revised input capturing.

This commit is contained in:
shy 2021-04-21 12:26:26 +02:00
parent 7950187cb7
commit 21f546dc52
4 changed files with 149 additions and 130 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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,7 +216,16 @@ pub fn run(
} }
// Process input. // Process input.
if let Some(key) = input_keys.next() { match rx.recv_timeout(time::Duration::from_millis(250)) {
// Timeout. Expected.
Err(mpsc::RecvTimeoutError::Timeout) => (),
// Disconnect.
// TODO: When? Why? What to do?
Err(mpsc::RecvTimeoutError::Disconnected) => {
eprintln!("Unexpected disconnect from input thread.");
break;
}
Ok(key) => {
match key.expect("Error reading input") { match key.expect("Error reading input") {
// Enter. // Enter.
Key::Char('\n') => { Key::Char('\n') => {
@ -329,9 +347,7 @@ pub fn run(
// Any other key. // Any other key.
_ => (), _ => (),
} }
} else { }
// Main loop delay. Skipped after key press.
thread::sleep(time::Duration::from_millis(100));
} }
} }

View file

@ -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.