mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-25 08:47:18 +02:00
Add add functionality
This commit is contained in:
parent
f7217aee8e
commit
7edbc28960
2 changed files with 83 additions and 10 deletions
73
src/app.rs
73
src/app.rs
|
@ -19,7 +19,7 @@ use tui::{
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
terminal::Frame,
|
terminal::Frame,
|
||||||
text::Text,
|
text::Text,
|
||||||
widgets::{BarChart, Block, Borders, Paragraph, Row, Table, TableState},
|
widgets::{BarChart, Clear, Block, Borders, Paragraph, Row, Table, TableState},
|
||||||
Terminal,
|
Terminal,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -58,15 +58,45 @@ pub fn vague_format_date_time(dt: &Date) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||||
|
let popup_layout = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints(
|
||||||
|
[
|
||||||
|
Constraint::Percentage((100 - percent_y) / 2),
|
||||||
|
Constraint::Percentage(percent_y),
|
||||||
|
Constraint::Percentage((100 - percent_y) / 2),
|
||||||
|
]
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
.split(r);
|
||||||
|
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints(
|
||||||
|
[
|
||||||
|
Constraint::Percentage((100 - percent_x) / 2),
|
||||||
|
Constraint::Percentage(percent_x),
|
||||||
|
Constraint::Percentage((100 - percent_x) / 2),
|
||||||
|
]
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
.split(popup_layout[1])[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub enum AppMode {
|
pub enum AppMode {
|
||||||
Report,
|
Report,
|
||||||
Filter,
|
Filter,
|
||||||
|
AddTask,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub should_quit: bool,
|
pub should_quit: bool,
|
||||||
pub state: TableState,
|
pub state: TableState,
|
||||||
pub filter: String,
|
pub filter: String,
|
||||||
|
pub command: String,
|
||||||
pub tasks: Vec<Task>,
|
pub tasks: Vec<Task>,
|
||||||
pub task_report_labels: Vec<String>,
|
pub task_report_labels: Vec<String>,
|
||||||
pub task_report_columns: Vec<String>,
|
pub task_report_columns: Vec<String>,
|
||||||
|
@ -82,6 +112,7 @@ impl App {
|
||||||
task_report_labels: vec![],
|
task_report_labels: vec![],
|
||||||
task_report_columns: vec![],
|
task_report_columns: vec![],
|
||||||
filter: "status:pending ".to_string(),
|
filter: "status:pending ".to_string(),
|
||||||
|
command: "".to_string(),
|
||||||
mode: AppMode::Report,
|
mode: AppMode::Report,
|
||||||
};
|
};
|
||||||
app.update();
|
app.update();
|
||||||
|
@ -101,24 +132,31 @@ impl App {
|
||||||
.split(f.size());
|
.split(f.size());
|
||||||
self.draw_task_report(f, rects[0]);
|
self.draw_task_report(f, rects[0]);
|
||||||
self.draw_task_details(f, rects[1]);
|
self.draw_task_details(f, rects[1]);
|
||||||
self.draw_command(f, rects[2]);
|
|
||||||
match self.mode {
|
match self.mode {
|
||||||
AppMode::Report => (),
|
AppMode::Report => self.draw_command(f, rects[2], &self.filter[..], "Filter"),
|
||||||
AppMode::Filter => {
|
AppMode::Filter => {
|
||||||
// Make the cursor visible and ask tui-rs to put it at the specified coordinates after rendering
|
f.set_cursor(
|
||||||
|
rects[2].x + self.filter.width() as u16 + 1,
|
||||||
|
rects[2].y + 1,
|
||||||
|
);
|
||||||
|
self.draw_command(f, rects[2], &self.filter[..], "Filter");
|
||||||
|
},
|
||||||
|
AppMode::AddTask => {
|
||||||
f.set_cursor(
|
f.set_cursor(
|
||||||
// Put cursor past the end of the input text
|
// Put cursor past the end of the input text
|
||||||
rects[2].x + self.filter.width() as u16 + 1,
|
rects[2].x + self.command.width() as u16 + 1,
|
||||||
// Move one line down, from the border to the input line
|
// Move one line down, from the border to the input line
|
||||||
rects[2].y + 1,
|
rects[2].y + 1,
|
||||||
)
|
);
|
||||||
}
|
f.render_widget(Clear, rects[2]);
|
||||||
|
self.draw_command(f, rects[2], &self.command[..], "Add Task");
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_command(&mut self, f: &mut Frame<impl Backend>, rect: Rect) {
|
fn draw_command(&self, f: &mut Frame<impl Backend>, rect: Rect, text: &str, title: &str) {
|
||||||
let p = Paragraph::new(Text::from(&self.filter[..]))
|
let p = Paragraph::new(Text::from(text))
|
||||||
.block(Block::default().borders(Borders::ALL).title("Filter"));
|
.block(Block::default().borders(Borders::ALL).title(title));
|
||||||
f.render_widget(p, rect);
|
f.render_widget(p, rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,6 +416,20 @@ impl App {
|
||||||
self.update();
|
self.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn task_add(&mut self) {
|
||||||
|
if self.tasks.len() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = Command::new("task")
|
||||||
|
.arg("add")
|
||||||
|
.arg(format!("{}", self.command))
|
||||||
|
.output()
|
||||||
|
.expect("Cannot run `task add`. Check documentation for more information");
|
||||||
|
|
||||||
|
self.command = "".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn task_done(&mut self) {
|
pub fn task_done(&mut self) {
|
||||||
if self.tasks.len() == 0 {
|
if self.tasks.len() == 0 {
|
||||||
return
|
return
|
||||||
|
@ -394,6 +446,7 @@ impl App {
|
||||||
task_id
|
task_id
|
||||||
)[..],
|
)[..],
|
||||||
);
|
);
|
||||||
|
self.previous()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn task_undo(&self) {
|
pub fn task_undo(&self) {
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -61,11 +61,31 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
app.task_edit();
|
app.task_edit();
|
||||||
events.resume_event_loop(&mut terminal);
|
events.resume_event_loop(&mut terminal);
|
||||||
},
|
},
|
||||||
|
Key::Char('a') => {
|
||||||
|
app.mode = AppMode::AddTask;
|
||||||
|
}
|
||||||
Key::Char('/') => {
|
Key::Char('/') => {
|
||||||
app.mode = AppMode::Filter;
|
app.mode = AppMode::Filter;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
|
AppMode::AddTask => match input {
|
||||||
|
Key::Char('\n') => {
|
||||||
|
app.task_add();
|
||||||
|
app.mode = AppMode::Report;
|
||||||
|
}
|
||||||
|
Key::Esc => {
|
||||||
|
app.command = "".to_string();
|
||||||
|
app.mode = AppMode::Report;
|
||||||
|
}
|
||||||
|
Key::Char(c) => {
|
||||||
|
app.command.push(c);
|
||||||
|
}
|
||||||
|
Key::Backspace => {
|
||||||
|
app.command.pop();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
AppMode::Filter => match input {
|
AppMode::Filter => match input {
|
||||||
Key::Char('\n') | Key::Esc => {
|
Key::Char('\n') | Key::Esc => {
|
||||||
app.mode = AppMode::Report;
|
app.mode = AppMode::Report;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue