forked from kageru/tracc
Don’t use copied input handling from the examples
This commit is contained in:
parent
4fa4764cd4
commit
78cb4bb1cb
|
@ -1,77 +0,0 @@
|
||||||
// This file was copied from https://github.com/fdehau/tui-rs/blob/master/examples/util/event.rs
|
|
||||||
use std::io;
|
|
||||||
use std::sync::mpsc;
|
|
||||||
use std::sync::{
|
|
||||||
atomic::{AtomicBool, Ordering},
|
|
||||||
Arc,
|
|
||||||
};
|
|
||||||
use std::thread;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use termion::event::Key;
|
|
||||||
use termion::input::TermRead;
|
|
||||||
|
|
||||||
pub enum Event<I> {
|
|
||||||
Input(I),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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>>,
|
|
||||||
input_handle: thread::JoinHandle<()>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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 ignore_exit_key = Arc::new(AtomicBool::new(false));
|
|
||||||
let input_handle = {
|
|
||||||
let tx = tx.clone();
|
|
||||||
let ignore_exit_key = ignore_exit_key.clone();
|
|
||||||
thread::spawn(move || {
|
|
||||||
let stdin = io::stdin();
|
|
||||||
for evt in stdin.keys() {
|
|
||||||
match evt {
|
|
||||||
Ok(key) => {
|
|
||||||
if let Err(_) = tx.send(Event::Input(key)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if !ignore_exit_key.load(Ordering::Relaxed) && key == config.exit_key {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
};
|
|
||||||
Events {
|
|
||||||
rx,
|
|
||||||
input_handle,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
|
|
||||||
self.rx.recv()
|
|
||||||
}
|
|
||||||
}
|
|
14
src/main.rs
14
src/main.rs
|
@ -1,13 +1,12 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
use termion::event::Key;
|
use termion::event::Key;
|
||||||
use termion::raw::IntoRawMode;
|
use termion::raw::IntoRawMode;
|
||||||
|
use termion::input::TermRead;
|
||||||
use tui::backend::Backend;
|
use tui::backend::Backend;
|
||||||
use tui::backend::TermionBackend;
|
use tui::backend::TermionBackend;
|
||||||
use tui::style::{Color, Style};
|
use tui::style::{Color, Style};
|
||||||
use tui::widgets::*;
|
use tui::widgets::*;
|
||||||
use tui::Terminal;
|
use tui::Terminal;
|
||||||
mod events;
|
|
||||||
use events::{Event, Events};
|
|
||||||
mod tracc;
|
mod tracc;
|
||||||
use tracc::Tracc;
|
use tracc::Tracc;
|
||||||
|
|
||||||
|
@ -18,17 +17,17 @@ pub enum Mode {
|
||||||
|
|
||||||
fn main() -> Result<(), io::Error> {
|
fn main() -> Result<(), io::Error> {
|
||||||
let stdout = io::stdout().into_raw_mode()?;
|
let stdout = io::stdout().into_raw_mode()?;
|
||||||
|
let mut inputs = io::stdin().keys();
|
||||||
let backend = TermionBackend::new(stdout);
|
let backend = TermionBackend::new(stdout);
|
||||||
let mut terminal = Terminal::new(backend)?;
|
let mut terminal = Terminal::new(backend)?;
|
||||||
let mut tracc = Tracc::new();
|
let mut tracc = Tracc::new();
|
||||||
terminal.hide_cursor()?;
|
terminal.hide_cursor()?;
|
||||||
terminal.clear()?;
|
terminal.clear()?;
|
||||||
let events = Events::new();
|
|
||||||
loop {
|
loop {
|
||||||
refresh(&mut terminal, &tracc)?;
|
refresh(&mut terminal, &tracc)?;
|
||||||
// I need to find a better way to handle inputs. This is awful.
|
// I need to find a better way to handle inputs. This is awful.
|
||||||
match events.next().expect("input ded?") {
|
let input = inputs.next().unwrap().expect("input ded?");
|
||||||
Event::Input(input) => match tracc.mode {
|
match tracc.mode {
|
||||||
Mode::Normal => match input {
|
Mode::Normal => match input {
|
||||||
Key::Char('q') => break,
|
Key::Char('q') => break,
|
||||||
Key::Char('j') => tracc.selection_down(),
|
Key::Char('j') => tracc.selection_down(),
|
||||||
|
@ -42,7 +41,7 @@ fn main() -> Result<(), io::Error> {
|
||||||
Key::Char(' ') => tracc.toggle_current(),
|
Key::Char(' ') => tracc.toggle_current(),
|
||||||
// dd
|
// dd
|
||||||
Key::Char('d') => {
|
Key::Char('d') => {
|
||||||
if let Event::Input(Key::Char('d')) = events.next().unwrap() {
|
if let Key::Char('d') = inputs.next().unwrap().unwrap() {
|
||||||
tracc.remove_current()
|
tracc.remove_current()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,8 +54,7 @@ fn main() -> Result<(), io::Error> {
|
||||||
Key::Char(x) => tracc.append_to_current(x),
|
Key::Char(x) => tracc.append_to_current(x),
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user