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]]
|
||||
name = "kitchentimer"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"signal-hook",
|
||||
"termion",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "kitchentimer"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
authors = ["Shy <shy@posteo.de>"]
|
||||
license = "GPL-3.0-or-later"
|
||||
edition = "2018"
|
||||
|
|
32
src/lib.rs
32
src/lib.rs
|
@ -32,7 +32,7 @@ pub use alarm::AlarmRoster;
|
|||
use alarm::Countdown;
|
||||
use buffer::Buffer;
|
||||
use clock::{font, Clock};
|
||||
pub use consts::ui::*;
|
||||
use consts::ui::*;
|
||||
use cradle::Cradle;
|
||||
use layout::Layout;
|
||||
use signal_hook::consts::signal::*;
|
||||
|
@ -40,6 +40,7 @@ use signal_hook::iterator::Signals;
|
|||
use signal_hook::low_level;
|
||||
use std::io::Write;
|
||||
use std::{env, process, thread, time};
|
||||
use std::sync::mpsc;
|
||||
use termion::event::Key;
|
||||
use termion::input::TermRead;
|
||||
use termion::raw::{IntoRawMode, RawTerminal};
|
||||
|
@ -55,9 +56,6 @@ pub fn run(
|
|||
let mut clock = Clock::new(&config);
|
||||
let mut countdown = Countdown::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 mut stdout = stdout.lock().into_raw_mode()?;
|
||||
let mut force_redraw = true;
|
||||
|
@ -67,6 +65,17 @@ pub fn run(
|
|||
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.
|
||||
loop {
|
||||
// Process received signals.
|
||||
|
@ -207,7 +216,16 @@ pub fn run(
|
|||
}
|
||||
|
||||
// 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") {
|
||||
// Enter.
|
||||
Key::Char('\n') => {
|
||||
|
@ -329,9 +347,7 @@ pub fn run(
|
|||
// 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);
|
||||
process::exit(1);
|
||||
}
|
||||
} else {
|
||||
// We don't need stdin anymore.
|
||||
drop(stdin);
|
||||
}
|
||||
|
||||
// Run main loop. Returns spawned child process if any.
|
||||
|
|
Loading…
Reference in a new issue