Add crossterm support

This commit is contained in:
Dheepak Krishnamurthy 2020-07-27 02:11:13 -06:00
parent d9f2e0b5b7
commit 7ad5483a92
5 changed files with 158 additions and 257 deletions

169
Cargo.lock generated
View file

@ -21,7 +21,7 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [ dependencies = [
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -38,7 +38,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi",
"libc", "libc",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -120,22 +120,6 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "crossterm"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5750773d74a7dc612eac2ded3f55e9cdeeaa072210cd17c0192aedb48adb3618"
dependencies = [
"bitflags",
"crossterm_winapi 0.5.1",
"lazy_static",
"libc",
"mio 0.6.22",
"parking_lot",
"signal-hook",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "crossterm" name = "crossterm"
version = "0.17.7" version = "0.17.7"
@ -143,22 +127,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f4919d60f26ae233e14233cc39746c8c8bb8cd7b05840ace83604917b51b6c7" checksum = "6f4919d60f26ae233e14233cc39746c8c8bb8cd7b05840ace83604917b51b6c7"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"crossterm_winapi 0.6.1", "crossterm_winapi",
"lazy_static", "lazy_static",
"libc", "libc",
"mio 0.7.0", "mio",
"parking_lot", "parking_lot",
"signal-hook", "signal-hook",
"winapi 0.3.9", "winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8777c700901e2d5b50c406f736ed6b8f9e43645c7e104ddb74f8bc42b8ae62f6"
dependencies = [
"winapi 0.3.9",
] ]
[[package]] [[package]]
@ -167,7 +142,7 @@ version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "057b7146d02fb50175fd7dbe5158f6097f33d02831f43b4ee8ae4ddf67b68f5c" checksum = "057b7146d02fb50175fd7dbe5158f6097f33d02831f43b4ee8ae4ddf67b68f5c"
dependencies = [ dependencies = [
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -263,22 +238,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
dependencies = [
"bitflags",
"fuchsia-zircon-sys",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.14" version = "0.1.14"
@ -311,31 +270,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "iovec"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.6" version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -375,25 +315,6 @@ dependencies = [
"adler", "adler",
] ]
[[package]]
name = "mio"
version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
dependencies = [
"cfg-if",
"fuchsia-zircon",
"fuchsia-zircon-sys",
"iovec",
"kernel32-sys",
"libc",
"log",
"miow 0.2.1",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.7.0" version = "0.7.0"
@ -403,21 +324,9 @@ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",
"log", "log",
"miow 0.3.5", "miow",
"ntapi", "ntapi",
"winapi 0.3.9", "winapi",
]
[[package]]
name = "miow"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
dependencies = [
"kernel32-sys",
"net2",
"winapi 0.2.8",
"ws2_32-sys",
] ]
[[package]] [[package]]
@ -427,18 +336,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e" checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e"
dependencies = [ dependencies = [
"socket2", "socket2",
"winapi 0.3.9", "winapi",
]
[[package]]
name = "net2"
version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
dependencies = [
"cfg-if",
"libc",
"winapi 0.3.9",
] ]
[[package]] [[package]]
@ -447,7 +345,7 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2" checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2"
dependencies = [ dependencies = [
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -502,7 +400,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -563,7 +461,7 @@ dependencies = [
"rand_os", "rand_os",
"rand_pcg", "rand_pcg",
"rand_xorshift", "rand_xorshift",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -658,7 +556,7 @@ checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b"
dependencies = [ dependencies = [
"libc", "libc",
"rand_core 0.4.2", "rand_core 0.4.2",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -672,7 +570,7 @@ dependencies = [
"libc", "libc",
"rand_core 0.4.2", "rand_core 0.4.2",
"rdrand", "rdrand",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -780,8 +678,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "604508c1418b99dfe1925ca9224829bb2a8a9a04dda655cc01fcad46f4ab05ed" checksum = "604508c1418b99dfe1925ca9224829bb2a8a9a04dda655cc01fcad46f4ab05ed"
dependencies = [ dependencies = [
"libc", "libc",
"mio 0.6.22", "mio",
"mio 0.7.0",
"signal-hook-registry", "signal-hook-registry",
] ]
@ -795,12 +692,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "slab"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.4.1" version = "1.4.1"
@ -816,7 +707,7 @@ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"redox_syscall", "redox_syscall",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -881,7 +772,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",
"crossterm 0.14.2", "crossterm",
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
"serde_json", "serde_json",
@ -920,7 +811,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [ dependencies = [
"libc", "libc",
"winapi 0.3.9", "winapi",
] ]
[[package]] [[package]]
@ -931,7 +822,7 @@ checksum = "a977b0bb2e2033a6fef950f218f13622c3c34e59754b704ce3492dedab1dfe95"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cassowary", "cassowary",
"crossterm 0.17.7", "crossterm",
"termion", "termion",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
@ -983,12 +874,6 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -999,12 +884,6 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu",
] ]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
[[package]] [[package]]
name = "winapi-i686-pc-windows-gnu" name = "winapi-i686-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
@ -1016,13 +895,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "ws2_32-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]

View file

@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features] [features]
default = ["termion-backend"] default = ["crossterm-backend"]
termion-backend = ["tui/termion", "termion"] termion-backend = ["tui/termion", "termion"]
crossterm-backend = ["tui/crossterm", "crossterm"] crossterm-backend = ["tui/crossterm", "crossterm"]
@ -18,7 +18,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
task-hookrs = "0.7.0" task-hookrs = "0.7.0"
termion = { version = "1.5.4", optional = true } termion = { version = "1.5.4", optional = true }
crossterm = { version = "0.14.2", optional = true } crossterm = { version = "0.17", optional = true }
rand = "0.7" rand = "0.7"
shlex = "0.1.1" shlex = "0.1.1"
chrono = "0.4" chrono = "0.4"

View file

@ -14,7 +14,7 @@ use unicode_width::UnicodeWidthStr;
use chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone}; use chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone};
use tui::{ use tui::{
backend::{Backend, TermionBackend}, backend::{Backend},
layout::{Constraint, Direction, Layout, Rect}, layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
terminal::Frame, terminal::Frame,
@ -23,9 +23,9 @@ use tui::{
Terminal, Terminal,
}; };
use termion::event::Key; use crate::util::{Key};
use crate::util::{Config, Event, Events};
use crossterm::event::KeyCode;
pub fn cmp(t1: &Task, t2: &Task) -> Ordering { pub fn cmp(t1: &Task, t2: &Task) -> Ordering {
let urgency1 = match &t1.uda()["urgency"] { let urgency1 = match &t1.uda()["urgency"] {
@ -382,7 +382,7 @@ impl App {
self.update(); self.update();
} }
pub fn handle_input(&mut self, event: ::termion::event::Key) { pub fn handle_input(&mut self, event: Key) {
match self.input_mode { match self.input_mode {
InputMode::Normal => match event { InputMode::Normal => match event {
Key::Ctrl('c') | Key::Char('q') => self.should_quit = true, Key::Ctrl('c') | Key::Char('q') => self.should_quit = true,

View file

@ -7,16 +7,12 @@ mod util;
#[allow(dead_code)] #[allow(dead_code)]
mod app; mod app;
use crate::util::{Config, Event, Events}; use crate::util::{EventConfig, Event, Events, setup_terminal, destruct_terminal};
use std::time::Duration; use std::time::{Duration, Instant};
use std::{error::Error, io}; use std::error::Error;
use termion::{ use std::io::{stdout, Write};
event::Key, use std::io;
input::MouseTerminal, use tui::backend::Backend;
raw::{IntoRawMode, RawTerminal},
screen::AlternateScreen,
};
use tui::{backend::TermionBackend, Terminal};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use std::env; use std::env;
use std::process::Command; use std::process::Command;
@ -24,27 +20,16 @@ use std::process::Command;
use app::App; use app::App;
use app::InputMode; use app::InputMode;
type B = TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>>;
fn setup_terminal() -> Result<Terminal<B>, io::Error> {
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
Terminal::new(backend)
}
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
// Terminal initialization // Terminal initialization
let mut terminal = setup_terminal()?; let mut terminal = setup_terminal();
terminal.clear()?;
// Setup event handlers // Setup event handlers
let mut events = Events::with_config(Config { let events = Events::with_config(EventConfig {
exit_key: Key::Char('q'),
tick_rate: Duration::from_millis(250), tick_rate: Duration::from_millis(250),
}); });
events.disable_exit_key();
let mut app = App::new(); let mut app = App::new();
app.next(); app.next();
@ -59,6 +44,7 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
if app.should_quit { if app.should_quit {
destruct_terminal(terminal);
break break
} }
} }

View file

@ -1,96 +1,142 @@
#[cfg(feature = "termion")] use std::io::{stdout, Write};
use std::io;
use std::sync::mpsc; #[cfg(feature = "crossterm")]
use std::sync::{ use crossterm::event;
atomic::{AtomicBool, Ordering}, #[cfg(feature = "crossterm")]
Arc, use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}; };
use std::thread;
use std::time::Duration;
use termion::event::Key; #[cfg(feature = "crossterm")]
use termion::input::TermRead; use tui::{backend::CrosstermBackend, Terminal};
pub enum Event<I> { use std::{sync::mpsc, thread, time::Duration};
Input(I),
Tick,
}
/// A small event handler that wrap termion input and tick events. Each event #[derive(Debug, Clone, Copy)]
/// type is handled in its own thread and returned to a common `Receiver` pub enum Key {
pub struct Events { Backspace,
rx: mpsc::Receiver<Event<Key>>, Left,
input_handle: thread::JoinHandle<()>, Right,
ignore_exit_key: Arc<AtomicBool>, Up,
tick_handle: thread::JoinHandle<()>, Down,
Home,
End,
PageUp,
PageDown,
BackTab,
Delete,
Insert,
F(u8),
Char(char),
Alt(char),
Ctrl(char),
Null,
Esc,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Config { pub struct EventConfig {
pub exit_key: Key, pub tick_rate: Duration,
pub tick_rate: Duration,
} }
impl Default for Config { impl Default for EventConfig {
fn default() -> Config { fn default() -> EventConfig {
Config { EventConfig {
exit_key: Key::Char('q'), tick_rate: Duration::from_millis(5),
tick_rate: Duration::from_millis(250),
}
} }
}
}
/// An occurred event.
pub enum Event<I> {
/// An input event occurred.
Input(I),
/// An tick event occurred.
Tick,
}
#[cfg(feature = "crossterm")]
pub fn setup_terminal() -> Terminal<CrosstermBackend<std::io::Stdout>> {
enable_raw_mode().unwrap();
let mut stdout = stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
let backend = CrosstermBackend::new(stdout);
Terminal::new(backend).unwrap()
}
#[cfg(feature = "crossterm")]
pub fn destruct_terminal(mut terminal: Terminal<CrosstermBackend<std::io::Stdout>>) {
disable_raw_mode().unwrap();
execute!(stdout(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
terminal.show_cursor().unwrap();
}
pub struct Events {
rx: mpsc::Receiver<Event<Key>>,
tx: mpsc::Sender<Event<Key>>,
} }
impl Events { impl Events {
pub fn new() -> Events { /// Constructs an new instance of `Events` with the default config.
Events::with_config(Config::default()) pub fn new(tick_rate: u64) -> Events {
} Events::with_config(EventConfig {
tick_rate: Duration::from_millis(tick_rate),
..Default::default()
})
}
pub fn with_config(config: Config) -> Events { pub fn with_config(config: EventConfig) -> Events {
let (tx, rx) = mpsc::channel(); use crossterm::event::{KeyCode::*, KeyModifiers};
let ignore_exit_key = Arc::new(AtomicBool::new(false)); let (tx, rx) = mpsc::channel();
let input_handle = {
let tx = tx.clone(); let event_tx = tx.clone();
let ignore_exit_key = ignore_exit_key.clone(); thread::spawn(move || {
thread::spawn(move || { loop {
let stdin = io::stdin(); // poll for tick rate duration, if no event, sent tick event.
for evt in stdin.keys() { if event::poll(config.tick_rate).unwrap() {
if let Ok(key) = evt { if let event::Event::Key(key) = event::read().unwrap() {
if let Err(err) = tx.send(Event::Input(key)) { let key = match key.code {
eprintln!("{}", err); Backspace => Key::Backspace,
return; Enter => Key::Char('\n'),
} Left => Key::Left,
if !ignore_exit_key.load(Ordering::Relaxed) && key == config.exit_key { Right => Key::Right,
return; Up => Key::Up,
} Down => Key::Down,
} Home => Key::Home,
} End => Key::End,
}) PageUp => Key::PageUp,
}; PageDown => Key::PageDown,
let tick_handle = { Tab => Key::Char('\t'),
thread::spawn(move || loop { BackTab => Key::BackTab,
if tx.send(Event::Tick).is_err() { Delete => Key::Delete,
break; Insert => Key::Insert,
} F(k) => Key::F(k),
thread::sleep(config.tick_rate); Null => Key::Null,
}) Esc => Key::Esc,
}; Char(c) => match key.modifiers {
Events { KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c),
rx, KeyModifiers::CONTROL => Key::Ctrl(c),
ignore_exit_key, KeyModifiers::ALT => Key::Alt(c),
input_handle, _ => Key::Null,
tick_handle, },
};
event_tx.send(Event::Input(key)).unwrap();
}
} }
}
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> { event_tx.send(Event::Tick).unwrap();
self.rx.recv() }
} });
pub fn disable_exit_key(&mut self) { Events { rx, tx }
self.ignore_exit_key.store(true, Ordering::Relaxed); }
}
pub fn enable_exit_key(&mut self) { /// Attempts to read an event.
self.ignore_exit_key.store(false, Ordering::Relaxed); /// This function will block the current thread.
} pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
self.rx.recv()
}
} }