mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-25 08:47:18 +02:00
Add crossterm support
This commit is contained in:
parent
d9f2e0b5b7
commit
7ad5483a92
5 changed files with 158 additions and 257 deletions
|
@ -14,7 +14,7 @@ use unicode_width::UnicodeWidthStr;
|
|||
use chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone};
|
||||
|
||||
use tui::{
|
||||
backend::{Backend, TermionBackend},
|
||||
backend::{Backend},
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
style::{Color, Modifier, Style},
|
||||
terminal::Frame,
|
||||
|
@ -23,9 +23,9 @@ use tui::{
|
|||
Terminal,
|
||||
};
|
||||
|
||||
use termion::event::Key;
|
||||
use crate::util::{Config, Event, Events};
|
||||
use crate::util::{Key};
|
||||
|
||||
use crossterm::event::KeyCode;
|
||||
|
||||
pub fn cmp(t1: &Task, t2: &Task) -> Ordering {
|
||||
let urgency1 = match &t1.uda()["urgency"] {
|
||||
|
@ -382,7 +382,7 @@ impl App {
|
|||
self.update();
|
||||
}
|
||||
|
||||
pub fn handle_input(&mut self, event: ::termion::event::Key) {
|
||||
pub fn handle_input(&mut self, event: Key) {
|
||||
match self.input_mode {
|
||||
InputMode::Normal => match event {
|
||||
Key::Ctrl('c') | Key::Char('q') => self.should_quit = true,
|
||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -7,16 +7,12 @@ mod util;
|
|||
#[allow(dead_code)]
|
||||
mod app;
|
||||
|
||||
use crate::util::{Config, Event, Events};
|
||||
use std::time::Duration;
|
||||
use std::{error::Error, io};
|
||||
use termion::{
|
||||
event::Key,
|
||||
input::MouseTerminal,
|
||||
raw::{IntoRawMode, RawTerminal},
|
||||
screen::AlternateScreen,
|
||||
};
|
||||
use tui::{backend::TermionBackend, Terminal};
|
||||
use crate::util::{EventConfig, Event, Events, setup_terminal, destruct_terminal};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::error::Error;
|
||||
use std::io::{stdout, Write};
|
||||
use std::io;
|
||||
use tui::backend::Backend;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
@ -24,27 +20,16 @@ use std::process::Command;
|
|||
use app::App;
|
||||
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>> {
|
||||
|
||||
// Terminal initialization
|
||||
let mut terminal = setup_terminal()?;
|
||||
let mut terminal = setup_terminal();
|
||||
terminal.clear()?;
|
||||
|
||||
// Setup event handlers
|
||||
let mut events = Events::with_config(Config {
|
||||
exit_key: Key::Char('q'),
|
||||
let events = Events::with_config(EventConfig {
|
||||
tick_rate: Duration::from_millis(250),
|
||||
});
|
||||
events.disable_exit_key();
|
||||
|
||||
let mut app = App::new();
|
||||
app.next();
|
||||
|
@ -59,6 +44,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
}
|
||||
|
||||
if app.should_quit {
|
||||
destruct_terminal(terminal);
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
200
src/util.rs
200
src/util.rs
|
@ -1,96 +1,142 @@
|
|||
#[cfg(feature = "termion")]
|
||||
use std::io;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
use std::io::{stdout, Write};
|
||||
|
||||
#[cfg(feature = "crossterm")]
|
||||
use crossterm::event;
|
||||
#[cfg(feature = "crossterm")]
|
||||
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;
|
||||
use termion::input::TermRead;
|
||||
#[cfg(feature = "crossterm")]
|
||||
use tui::{backend::CrosstermBackend, Terminal};
|
||||
|
||||
pub enum Event<I> {
|
||||
Input(I),
|
||||
Tick,
|
||||
}
|
||||
use std::{sync::mpsc, thread, time::Duration};
|
||||
|
||||
/// 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<()>,
|
||||
ignore_exit_key: Arc<AtomicBool>,
|
||||
tick_handle: thread::JoinHandle<()>,
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Key {
|
||||
Backspace,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Down,
|
||||
Home,
|
||||
End,
|
||||
PageUp,
|
||||
PageDown,
|
||||
BackTab,
|
||||
Delete,
|
||||
Insert,
|
||||
F(u8),
|
||||
Char(char),
|
||||
Alt(char),
|
||||
Ctrl(char),
|
||||
Null,
|
||||
Esc,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Config {
|
||||
pub exit_key: Key,
|
||||
pub tick_rate: Duration,
|
||||
pub struct EventConfig {
|
||||
pub tick_rate: Duration,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Config {
|
||||
Config {
|
||||
exit_key: Key::Char('q'),
|
||||
tick_rate: Duration::from_millis(250),
|
||||
}
|
||||
impl Default for EventConfig {
|
||||
fn default() -> EventConfig {
|
||||
EventConfig {
|
||||
tick_rate: Duration::from_millis(5),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
pub fn new() -> Events {
|
||||
Events::with_config(Config::default())
|
||||
}
|
||||
/// Constructs an new instance of `Events` with the default config.
|
||||
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 {
|
||||
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() {
|
||||
if let Ok(key) = evt {
|
||||
if let Err(err) = tx.send(Event::Input(key)) {
|
||||
eprintln!("{}", err);
|
||||
return;
|
||||
}
|
||||
if !ignore_exit_key.load(Ordering::Relaxed) && key == config.exit_key {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
let tick_handle = {
|
||||
thread::spawn(move || loop {
|
||||
if tx.send(Event::Tick).is_err() {
|
||||
break;
|
||||
}
|
||||
thread::sleep(config.tick_rate);
|
||||
})
|
||||
};
|
||||
Events {
|
||||
rx,
|
||||
ignore_exit_key,
|
||||
input_handle,
|
||||
tick_handle,
|
||||
pub fn with_config(config: EventConfig) -> Events {
|
||||
use crossterm::event::{KeyCode::*, KeyModifiers};
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
let event_tx = tx.clone();
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
// poll for tick rate duration, if no event, sent tick event.
|
||||
if event::poll(config.tick_rate).unwrap() {
|
||||
if let event::Event::Key(key) = event::read().unwrap() {
|
||||
let key = match key.code {
|
||||
Backspace => Key::Backspace,
|
||||
Enter => Key::Char('\n'),
|
||||
Left => Key::Left,
|
||||
Right => Key::Right,
|
||||
Up => Key::Up,
|
||||
Down => Key::Down,
|
||||
Home => Key::Home,
|
||||
End => Key::End,
|
||||
PageUp => Key::PageUp,
|
||||
PageDown => Key::PageDown,
|
||||
Tab => Key::Char('\t'),
|
||||
BackTab => Key::BackTab,
|
||||
Delete => Key::Delete,
|
||||
Insert => Key::Insert,
|
||||
F(k) => Key::F(k),
|
||||
Null => Key::Null,
|
||||
Esc => Key::Esc,
|
||||
Char(c) => match key.modifiers {
|
||||
KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c),
|
||||
KeyModifiers::CONTROL => Key::Ctrl(c),
|
||||
KeyModifiers::ALT => Key::Alt(c),
|
||||
_ => Key::Null,
|
||||
},
|
||||
};
|
||||
event_tx.send(Event::Input(key)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
|
||||
self.rx.recv()
|
||||
}
|
||||
event_tx.send(Event::Tick).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
pub fn disable_exit_key(&mut self) {
|
||||
self.ignore_exit_key.store(true, Ordering::Relaxed);
|
||||
}
|
||||
Events { rx, tx }
|
||||
}
|
||||
|
||||
pub fn enable_exit_key(&mut self) {
|
||||
self.ignore_exit_key.store(false, Ordering::Relaxed);
|
||||
}
|
||||
/// Attempts to read an event.
|
||||
/// This function will block the current thread.
|
||||
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
|
||||
self.rx.recv()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue