diff options
| author | Ellie Huxtable <e@elm.sh> | 2021-03-20 00:50:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-20 00:50:31 +0000 |
| commit | 716c7722cda29bf612508bb96f51822a86e0f69e (patch) | |
| tree | fa3c4c192fc05b078397fcd510d39ae78e46abfa /src/command/event.rs | |
| parent | Add config file support (#15) (diff) | |
| download | atuin-716c7722cda29bf612508bb96f51822a86e0f69e.zip | |
Add TUI, resolve #19, #17, #16 (#21)
Diffstat (limited to '')
| -rw-r--r-- | src/command/event.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/command/event.rs b/src/command/event.rs new file mode 100644 index 00000000..b205be70 --- /dev/null +++ b/src/command/event.rs @@ -0,0 +1,68 @@ +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +use termion::event::Key; +use termion::input::TermRead; + +pub enum Event<I> { + Input(I), + Tick, +} + +/// A small event handler that wrap termion input and tick events. Each event +/// type is handled in its own thread and returned to a common `Receiver` +pub struct Events { + rx: mpsc::Receiver<Event<Key>>, +} + +#[derive(Debug, Clone, Copy)] +pub struct Config { + pub exit_key: Key, + pub tick_rate: Duration, +} + +impl Default for Config { + fn default() -> Config { + Config { + exit_key: Key::Char('q'), + tick_rate: Duration::from_millis(250), + } + } +} + +impl Events { + pub fn new() -> Events { + Events::with_config(Config::default()) + } + + pub fn with_config(config: Config) -> Events { + let (tx, rx) = mpsc::channel(); + + { + let tx = tx.clone(); + thread::spawn(move || { + let tty = termion::get_tty().expect("Could not find tty"); + for key in tty.keys().flatten() { + if let Err(err) = tx.send(Event::Input(key)) { + eprintln!("{}", err); + return; + } + } + }) + }; + + thread::spawn(move || loop { + if tx.send(Event::Tick).is_err() { + break; + } + thread::sleep(config.tick_rate); + }); + + Events { rx } + } + + pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> { + self.rx.recv() + } +} |
