mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-24 14:36:42 +02:00
Run cargo format
This commit is contained in:
parent
293f7f59b6
commit
412c63965b
3 changed files with 82 additions and 84 deletions
13
src/app.rs
13
src/app.rs
|
@ -14,7 +14,7 @@ use unicode_width::UnicodeWidthStr;
|
|||
use chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone};
|
||||
|
||||
use tui::{
|
||||
backend::{Backend},
|
||||
backend::Backend,
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
style::{Color, Modifier, Style},
|
||||
terminal::Frame,
|
||||
|
@ -23,7 +23,7 @@ use tui::{
|
|||
Terminal,
|
||||
};
|
||||
|
||||
use crate::util::{Key};
|
||||
use crate::util::Key;
|
||||
|
||||
pub fn cmp(t1: &Task, t2: &Task) -> Ordering {
|
||||
let urgency1 = match &t1.uda()["urgency"] {
|
||||
|
@ -140,11 +140,11 @@ impl App {
|
|||
.arg(format!("{}", task_id))
|
||||
.output()
|
||||
.expect(
|
||||
&format!(
|
||||
&format!(
|
||||
"Unable to show details for `task {}`. Check documentation for more information",
|
||||
task_id
|
||||
)[..],
|
||||
);
|
||||
);
|
||||
let data = String::from_utf8(output.stdout).unwrap();
|
||||
let p = Paragraph::new(Text::from(&data[..])).block(
|
||||
Block::default()
|
||||
|
@ -277,7 +277,7 @@ impl App {
|
|||
}
|
||||
pub fn next(&mut self) {
|
||||
if self.tasks.len() == 0 {
|
||||
return
|
||||
return;
|
||||
}
|
||||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
|
@ -293,7 +293,7 @@ impl App {
|
|||
}
|
||||
pub fn previous(&mut self) {
|
||||
if self.tasks.len() == 0 {
|
||||
return
|
||||
return;
|
||||
}
|
||||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
|
@ -406,7 +406,6 @@ impl App {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -7,21 +7,20 @@ mod util;
|
|||
#[allow(dead_code)]
|
||||
mod app;
|
||||
|
||||
use crate::util::{EventConfig, Event, Events, setup_terminal, destruct_terminal};
|
||||
use std::time::{Duration, Instant};
|
||||
use crate::util::{destruct_terminal, setup_terminal, Event, EventConfig, Events};
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::io::{stdout, Write};
|
||||
use std::io;
|
||||
use std::io::{stdout, Write};
|
||||
use std::process::Command;
|
||||
use std::time::{Duration, Instant};
|
||||
use tui::backend::Backend;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
||||
use app::App;
|
||||
use app::InputMode;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// Terminal initialization
|
||||
let mut terminal = setup_terminal();
|
||||
terminal.clear()?;
|
||||
|
@ -45,7 +44,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
if app.should_quit {
|
||||
destruct_terminal(terminal);
|
||||
break
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
140
src/util.rs
140
src/util.rs
|
@ -7,11 +7,10 @@ use crossterm::{
|
|||
#[cfg(feature = "crossterm")]
|
||||
use tui::{backend::CrosstermBackend, Terminal};
|
||||
|
||||
|
||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||
use termion::{
|
||||
event,
|
||||
input::{TermRead,MouseTerminal},
|
||||
input::{MouseTerminal, TermRead},
|
||||
raw::{IntoRawMode, RawTerminal},
|
||||
screen::AlternateScreen,
|
||||
};
|
||||
|
@ -45,21 +44,21 @@ pub enum Key {
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct EventConfig {
|
||||
pub tick_rate: Duration,
|
||||
pub tick_rate: Duration,
|
||||
}
|
||||
|
||||
impl Default for EventConfig {
|
||||
fn default() -> EventConfig {
|
||||
EventConfig {
|
||||
tick_rate: Duration::from_millis(5),
|
||||
fn default() -> EventConfig {
|
||||
EventConfig {
|
||||
tick_rate: Duration::from_millis(5),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Event<I> {
|
||||
Input(I),
|
||||
Tick,
|
||||
Input(I),
|
||||
Tick,
|
||||
}
|
||||
|
||||
#[cfg(feature = "crossterm")]
|
||||
|
@ -78,9 +77,9 @@ pub fn destruct_terminal(mut terminal: Terminal<CrosstermBackend<io::Stdout>>) {
|
|||
terminal.show_cursor().unwrap();
|
||||
}
|
||||
|
||||
|
||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||
pub fn setup_terminal() -> Terminal<TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>>> {
|
||||
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);
|
||||
|
@ -89,68 +88,69 @@ pub fn setup_terminal() -> Terminal<TermionBackend<AlternateScreen<MouseTerminal
|
|||
}
|
||||
|
||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||
pub fn destruct_terminal(terminal: Terminal<TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>>> ) {
|
||||
pub fn destruct_terminal(
|
||||
terminal: Terminal<TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>>>,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
pub struct Events {
|
||||
rx: mpsc::Receiver<Event<Key>>,
|
||||
rx: mpsc::Receiver<Event<Key>>,
|
||||
}
|
||||
|
||||
impl Events {
|
||||
/// 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()
|
||||
})
|
||||
}
|
||||
/// 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()
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "crossterm")]
|
||||
pub fn with_config(config: EventConfig) -> Events {
|
||||
use crossterm::event::{KeyCode::*, KeyModifiers};
|
||||
let (tx, rx) = mpsc::channel();
|
||||
#[cfg(feature = "crossterm")]
|
||||
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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
event_tx.send(Event::Tick).unwrap();
|
||||
}
|
||||
});
|
||||
Events { rx }
|
||||
}
|
||||
event_tx.send(Event::Tick).unwrap();
|
||||
}
|
||||
});
|
||||
Events { rx }
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||
pub fn with_config(config: EventConfig) -> Events {
|
||||
|
@ -199,12 +199,12 @@ impl Events {
|
|||
thread::sleep(config.tick_rate);
|
||||
})
|
||||
};
|
||||
Events {rx}
|
||||
Events { rx }
|
||||
}
|
||||
|
||||
/// Attempts to read an event.
|
||||
/// This function will block the current thread.
|
||||
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
|
||||
self.rx.recv()
|
||||
}
|
||||
/// 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