aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/event.rs
diff options
context:
space:
mode:
authorConrad Ludgate <conrad.ludgate@truelayer.com>2022-04-21 10:12:56 +0100
committerGitHub <noreply@github.com>2022-04-21 09:12:56 +0000
commitd57f549855caf8ab90b5ea0ae7cc9445f3abedfc (patch)
tree0818ff405a3b697a0ca981d215ceb4dbb30cd15a /src/command/event.rs
parentFix SQL cache query (#318) (diff)
downloadatuin-d57f549855caf8ab90b5ea0ae7cc9445f3abedfc.zip
refactor commands for better separation (#313)
* refactor commands for better separation * fmt
Diffstat (limited to 'src/command/event.rs')
-rw-r--r--src/command/event.rs68
1 files changed, 0 insertions, 68 deletions
diff --git a/src/command/event.rs b/src/command/event.rs
deleted file mode 100644
index f09752d6..00000000
--- a/src/command/event.rs
+++ /dev/null
@@ -1,68 +0,0 @@
-use std::thread;
-use std::time::Duration;
-
-use crossbeam_channel::unbounded;
-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: crossbeam_channel::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) = unbounded();
-
- {
- 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>, crossbeam_channel::RecvError> {
- self.rx.recv()
- }
-}