mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-24 23:46:41 +02:00
Support both crossterm and termion
This commit is contained in:
parent
7ad5483a92
commit
c3a87b4f3f
3 changed files with 85 additions and 19 deletions
|
@ -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 = ["crossterm-backend"]
|
default = ["termion-backend"]
|
||||||
termion-backend = ["tui/termion", "termion"]
|
termion-backend = ["tui/termion", "termion"]
|
||||||
crossterm-backend = ["tui/crossterm", "crossterm"]
|
crossterm-backend = ["tui/crossterm", "crossterm"]
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,6 @@ use tui::{
|
||||||
|
|
||||||
use crate::util::{Key};
|
use crate::util::{Key};
|
||||||
|
|
||||||
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"] {
|
||||||
UDAValue::Str(_) => 0.0,
|
UDAValue::Str(_) => 0.0,
|
||||||
|
|
100
src/util.rs
100
src/util.rs
|
@ -1,17 +1,24 @@
|
||||||
use std::io::{stdout, Write};
|
|
||||||
|
|
||||||
#[cfg(feature = "crossterm")]
|
|
||||||
use crossterm::event;
|
|
||||||
#[cfg(feature = "crossterm")]
|
#[cfg(feature = "crossterm")]
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
event::{DisableMouseCapture, EnableMouseCapture, KeyCode},
|
event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode},
|
||||||
execute,
|
execute,
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "crossterm")]
|
#[cfg(feature = "crossterm")]
|
||||||
use tui::{backend::CrosstermBackend, Terminal};
|
use tui::{backend::CrosstermBackend, Terminal};
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
|
use termion::{
|
||||||
|
event,
|
||||||
|
input::{TermRead,MouseTerminal},
|
||||||
|
raw::{IntoRawMode, RawTerminal},
|
||||||
|
screen::AlternateScreen,
|
||||||
|
};
|
||||||
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
|
use tui::{backend::TermionBackend, Terminal};
|
||||||
|
|
||||||
|
use std::io::{self, Write};
|
||||||
use std::{sync::mpsc, thread, time::Duration};
|
use std::{sync::mpsc, thread, time::Duration};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
@ -49,34 +56,45 @@ impl Default for EventConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An occurred event.
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum Event<I> {
|
pub enum Event<I> {
|
||||||
/// An input event occurred.
|
|
||||||
Input(I),
|
Input(I),
|
||||||
/// An tick event occurred.
|
|
||||||
Tick,
|
Tick,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "crossterm")]
|
#[cfg(feature = "crossterm")]
|
||||||
pub fn setup_terminal() -> Terminal<CrosstermBackend<std::io::Stdout>> {
|
pub fn setup_terminal() -> Terminal<CrosstermBackend<io::Stdout>> {
|
||||||
enable_raw_mode().unwrap();
|
enable_raw_mode().unwrap();
|
||||||
let mut stdout = stdout();
|
let mut stdout = io::stdout();
|
||||||
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
|
||||||
let backend = CrosstermBackend::new(stdout);
|
let backend = CrosstermBackend::new(stdout);
|
||||||
Terminal::new(backend).unwrap()
|
Terminal::new(backend).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "crossterm")]
|
#[cfg(feature = "crossterm")]
|
||||||
pub fn destruct_terminal(mut terminal: Terminal<CrosstermBackend<std::io::Stdout>>) {
|
pub fn destruct_terminal(mut terminal: Terminal<CrosstermBackend<io::Stdout>>) {
|
||||||
disable_raw_mode().unwrap();
|
disable_raw_mode().unwrap();
|
||||||
execute!(stdout(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
|
execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
|
||||||
terminal.show_cursor().unwrap();
|
terminal.show_cursor().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
|
pub fn setup_terminal() -> Terminal<TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>>> {
|
||||||
|
let stdout = io::stdout().into_raw_mode().unwrap();
|
||||||
|
let stdout = MouseTerminal::from(stdout);
|
||||||
|
let stdout = AlternateScreen::from(stdout);
|
||||||
|
let backend = TermionBackend::new(stdout);
|
||||||
|
Terminal::new(backend).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
|
pub fn destruct_terminal(terminal: Terminal<TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>>> ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct Events {
|
pub struct Events {
|
||||||
rx: mpsc::Receiver<Event<Key>>,
|
rx: mpsc::Receiver<Event<Key>>,
|
||||||
tx: mpsc::Sender<Event<Key>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Events {
|
impl Events {
|
||||||
|
@ -88,6 +106,7 @@ impl Events {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "crossterm")]
|
||||||
pub fn with_config(config: EventConfig) -> Events {
|
pub fn with_config(config: EventConfig) -> Events {
|
||||||
use crossterm::event::{KeyCode::*, KeyModifiers};
|
use crossterm::event::{KeyCode::*, KeyModifiers};
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
@ -130,10 +149,59 @@ impl Events {
|
||||||
event_tx.send(Event::Tick).unwrap();
|
event_tx.send(Event::Tick).unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Events { rx }
|
||||||
Events { rx, tx }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
|
pub fn with_config(config: EventConfig) -> Events {
|
||||||
|
use termion::event::Key::*;
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
let input_handle = {
|
||||||
|
let tx = tx.clone();
|
||||||
|
thread::spawn(move || {
|
||||||
|
let stdin = io::stdin();
|
||||||
|
for evt in stdin.keys() {
|
||||||
|
if let Ok(key) = evt {
|
||||||
|
let key = match key {
|
||||||
|
Backspace => Key::Backspace,
|
||||||
|
Left => Key::Left,
|
||||||
|
Right => Key::Right,
|
||||||
|
Up => Key::Up,
|
||||||
|
Down => Key::Down,
|
||||||
|
Home => Key::Home,
|
||||||
|
End => Key::End,
|
||||||
|
PageUp => Key::PageUp,
|
||||||
|
PageDown => Key::PageDown,
|
||||||
|
BackTab => Key::BackTab,
|
||||||
|
Delete => Key::Delete,
|
||||||
|
Insert => Key::Insert,
|
||||||
|
F(c) => Key::F(c),
|
||||||
|
Char(c) => Key::Char(c),
|
||||||
|
Alt(c) => Key::Alt(c),
|
||||||
|
Ctrl(c) => Key::Ctrl(c),
|
||||||
|
Null => Key::Null,
|
||||||
|
Esc => Key::Esc,
|
||||||
|
_ => Key::Null,
|
||||||
|
};
|
||||||
|
if let Err(err) = tx.send(Event::Input(key)) {
|
||||||
|
eprintln!("{}", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let tick_handle = {
|
||||||
|
thread::spawn(move || loop {
|
||||||
|
if tx.send(Event::Tick).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
thread::sleep(config.tick_rate);
|
||||||
|
})
|
||||||
|
};
|
||||||
|
Events {rx}
|
||||||
|
}
|
||||||
|
|
||||||
/// Attempts to read an event.
|
/// Attempts to read an event.
|
||||||
/// This function will block the current thread.
|
/// This function will block the current thread.
|
||||||
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
|
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue