mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-24 23:46:41 +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 chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone};
|
||||||
|
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::{Backend},
|
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,7 +23,7 @@ use tui::{
|
||||||
Terminal,
|
Terminal,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::util::{Key};
|
use crate::util::Key;
|
||||||
|
|
||||||
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"] {
|
||||||
|
@ -140,11 +140,11 @@ impl App {
|
||||||
.arg(format!("{}", task_id))
|
.arg(format!("{}", task_id))
|
||||||
.output()
|
.output()
|
||||||
.expect(
|
.expect(
|
||||||
&format!(
|
&format!(
|
||||||
"Unable to show details for `task {}`. Check documentation for more information",
|
"Unable to show details for `task {}`. Check documentation for more information",
|
||||||
task_id
|
task_id
|
||||||
)[..],
|
)[..],
|
||||||
);
|
);
|
||||||
let data = String::from_utf8(output.stdout).unwrap();
|
let data = String::from_utf8(output.stdout).unwrap();
|
||||||
let p = Paragraph::new(Text::from(&data[..])).block(
|
let p = Paragraph::new(Text::from(&data[..])).block(
|
||||||
Block::default()
|
Block::default()
|
||||||
|
@ -277,7 +277,7 @@ impl App {
|
||||||
}
|
}
|
||||||
pub fn next(&mut self) {
|
pub fn next(&mut self) {
|
||||||
if self.tasks.len() == 0 {
|
if self.tasks.len() == 0 {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
let i = match self.state.selected() {
|
let i = match self.state.selected() {
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
|
@ -293,7 +293,7 @@ impl App {
|
||||||
}
|
}
|
||||||
pub fn previous(&mut self) {
|
pub fn previous(&mut self) {
|
||||||
if self.tasks.len() == 0 {
|
if self.tasks.len() == 0 {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
let i = match self.state.selected() {
|
let i = match self.state.selected() {
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
|
@ -406,7 +406,6 @@ impl App {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -7,21 +7,20 @@ mod util;
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
mod app;
|
mod app;
|
||||||
|
|
||||||
use crate::util::{EventConfig, Event, Events, setup_terminal, destruct_terminal};
|
use crate::util::{destruct_terminal, setup_terminal, Event, EventConfig, Events};
|
||||||
use std::time::{Duration, Instant};
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::{stdout, Write};
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::io::{stdout, Write};
|
||||||
|
use std::process::Command;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
use tui::backend::Backend;
|
use tui::backend::Backend;
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
use std::env;
|
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
use app::App;
|
use app::App;
|
||||||
use app::InputMode;
|
use app::InputMode;
|
||||||
|
|
||||||
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()?;
|
terminal.clear()?;
|
||||||
|
@ -45,7 +44,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
if app.should_quit {
|
if app.should_quit {
|
||||||
destruct_terminal(terminal);
|
destruct_terminal(terminal);
|
||||||
break
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
140
src/util.rs
140
src/util.rs
|
@ -7,11 +7,10 @@ use crossterm::{
|
||||||
#[cfg(feature = "crossterm")]
|
#[cfg(feature = "crossterm")]
|
||||||
use tui::{backend::CrosstermBackend, Terminal};
|
use tui::{backend::CrosstermBackend, Terminal};
|
||||||
|
|
||||||
|
|
||||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
use termion::{
|
use termion::{
|
||||||
event,
|
event,
|
||||||
input::{TermRead,MouseTerminal},
|
input::{MouseTerminal, TermRead},
|
||||||
raw::{IntoRawMode, RawTerminal},
|
raw::{IntoRawMode, RawTerminal},
|
||||||
screen::AlternateScreen,
|
screen::AlternateScreen,
|
||||||
};
|
};
|
||||||
|
@ -45,21 +44,21 @@ pub enum Key {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct EventConfig {
|
pub struct EventConfig {
|
||||||
pub tick_rate: Duration,
|
pub tick_rate: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for EventConfig {
|
impl Default for EventConfig {
|
||||||
fn default() -> EventConfig {
|
fn default() -> EventConfig {
|
||||||
EventConfig {
|
EventConfig {
|
||||||
tick_rate: Duration::from_millis(5),
|
tick_rate: Duration::from_millis(5),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum Event<I> {
|
pub enum Event<I> {
|
||||||
Input(I),
|
Input(I),
|
||||||
Tick,
|
Tick,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "crossterm")]
|
#[cfg(feature = "crossterm")]
|
||||||
|
@ -78,9 +77,9 @@ pub fn destruct_terminal(mut terminal: Terminal<CrosstermBackend<io::Stdout>>) {
|
||||||
terminal.show_cursor().unwrap();
|
terminal.show_cursor().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
#[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 = io::stdout().into_raw_mode().unwrap();
|
||||||
let stdout = MouseTerminal::from(stdout);
|
let stdout = MouseTerminal::from(stdout);
|
||||||
let stdout = AlternateScreen::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")))]
|
#[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 {
|
pub struct Events {
|
||||||
rx: mpsc::Receiver<Event<Key>>,
|
rx: mpsc::Receiver<Event<Key>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Events {
|
impl Events {
|
||||||
/// Constructs an new instance of `Events` with the default config.
|
/// Constructs an new instance of `Events` with the default config.
|
||||||
pub fn new(tick_rate: u64) -> Events {
|
pub fn new(tick_rate: u64) -> Events {
|
||||||
Events::with_config(EventConfig {
|
Events::with_config(EventConfig {
|
||||||
tick_rate: Duration::from_millis(tick_rate),
|
tick_rate: Duration::from_millis(tick_rate),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "crossterm")]
|
#[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();
|
||||||
|
|
||||||
let event_tx = tx.clone();
|
let event_tx = tx.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
loop {
|
loop {
|
||||||
// poll for tick rate duration, if no event, sent tick event.
|
// poll for tick rate duration, if no event, sent tick event.
|
||||||
if event::poll(config.tick_rate).unwrap() {
|
if event::poll(config.tick_rate).unwrap() {
|
||||||
if let event::Event::Key(key) = event::read().unwrap() {
|
if let event::Event::Key(key) = event::read().unwrap() {
|
||||||
let key = match key.code {
|
let key = match key.code {
|
||||||
Backspace => Key::Backspace,
|
Backspace => Key::Backspace,
|
||||||
Enter => Key::Char('\n'),
|
Enter => Key::Char('\n'),
|
||||||
Left => Key::Left,
|
Left => Key::Left,
|
||||||
Right => Key::Right,
|
Right => Key::Right,
|
||||||
Up => Key::Up,
|
Up => Key::Up,
|
||||||
Down => Key::Down,
|
Down => Key::Down,
|
||||||
Home => Key::Home,
|
Home => Key::Home,
|
||||||
End => Key::End,
|
End => Key::End,
|
||||||
PageUp => Key::PageUp,
|
PageUp => Key::PageUp,
|
||||||
PageDown => Key::PageDown,
|
PageDown => Key::PageDown,
|
||||||
Tab => Key::Char('\t'),
|
Tab => Key::Char('\t'),
|
||||||
BackTab => Key::BackTab,
|
BackTab => Key::BackTab,
|
||||||
Delete => Key::Delete,
|
Delete => Key::Delete,
|
||||||
Insert => Key::Insert,
|
Insert => Key::Insert,
|
||||||
F(k) => Key::F(k),
|
F(k) => Key::F(k),
|
||||||
Null => Key::Null,
|
Null => Key::Null,
|
||||||
Esc => Key::Esc,
|
Esc => Key::Esc,
|
||||||
Char(c) => match key.modifiers {
|
Char(c) => match key.modifiers {
|
||||||
KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c),
|
KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c),
|
||||||
KeyModifiers::CONTROL => Key::Ctrl(c),
|
KeyModifiers::CONTROL => Key::Ctrl(c),
|
||||||
KeyModifiers::ALT => Key::Alt(c),
|
KeyModifiers::ALT => Key::Alt(c),
|
||||||
_ => Key::Null,
|
_ => Key::Null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
event_tx.send(Event::Input(key)).unwrap();
|
event_tx.send(Event::Input(key)).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
event_tx.send(Event::Tick).unwrap();
|
event_tx.send(Event::Tick).unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Events { rx }
|
Events { rx }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
|
||||||
pub fn with_config(config: EventConfig) -> Events {
|
pub fn with_config(config: EventConfig) -> Events {
|
||||||
|
@ -199,12 +199,12 @@ impl Events {
|
||||||
thread::sleep(config.tick_rate);
|
thread::sleep(config.tick_rate);
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
Events {rx}
|
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> {
|
||||||
self.rx.recv()
|
self.rx.recv()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue