From 73ffc9d02680fd0e8c2ef7f59c2fcfc5131f4af8 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Mon, 10 Aug 2020 00:46:04 -0600 Subject: [PATCH] Support annotations --- src/app.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 2c9b3ba..f0783e2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -115,6 +115,7 @@ pub enum AppMode { Report, Filter, AddTask, + AnnotateTask, LogTask, ModifyTask, HelpPopup, @@ -201,6 +202,10 @@ impl TTApp { .split(rects[0]); self.draw_task_report(f, task_rects[0]); self.draw_task_details(f, task_rects[1]); + let selected = self.state.selected().unwrap_or_default(); + let task_id = self.tasks.lock().unwrap()[selected] + .id() + .unwrap_or_default(); match self.mode { AppMode::Report => self.draw_command(f, rects[1], &self.filter[..], "Filter Tasks"), AppMode::Filter => { @@ -211,13 +216,18 @@ impl TTApp { AppMode::ModifyTask => { f.set_cursor(rects[1].x + self.cursor_location as u16 + 1, rects[1].y + 1); f.render_widget(Clear, rects[1]); - self.draw_command(f, rects[1], &self.modify[..], "Modify Task"); + self.draw_command(f, rects[1], &self.modify[..], format!("Modify Task {}", task_id).as_str()); } AppMode::LogTask => { f.set_cursor(rects[1].x + self.cursor_location as u16 + 1, rects[1].y + 1); f.render_widget(Clear, rects[1]); self.draw_command(f, rects[1], &self.command[..], "Log Task"); } + AppMode::AnnotateTask => { + f.set_cursor(rects[1].x + self.cursor_location as u16 + 1, rects[1].y + 1); + f.render_widget(Clear, rects[1]); + self.draw_command(f, rects[1], &self.command[..], format!("Annotate Task {}", task_id).as_str()); + } AppMode::AddTask => { f.set_cursor(rects[1].x + self.cursor_location as u16 + 1, rects[1].y + 1); f.render_widget(Clear, rects[1]); @@ -753,6 +763,38 @@ impl TTApp { } } + pub fn task_annotate(&mut self) -> Result<(), String> { + if self.tasks.lock().unwrap().is_empty() { + return Ok(()); + } + let selected = self.state.selected().unwrap_or_default(); + let task_id = self.tasks.lock().unwrap()[selected] + .id() + .unwrap_or_default(); + let mut command = Command::new("task"); + command.arg(format!("{}", task_id)).arg("annotate"); + + match shlex::split(&self.command) { + Some(cmd) => { + for s in cmd { + command.arg(&s); + } + let output = command.output(); + match output { + Ok(_) => { + self.command = "".to_string(); + Ok(()) + } + Err(_) => Err( + "Cannot run `task annotate`. Check documentation for more information" + .to_string(), + ), + } + } + None => Err(format!("Unable to run `task add` with `{}`", &self.command)), + } + } + pub fn task_add(&mut self) -> Result<(), String> { let mut command = Command::new("task"); command.arg("add"); diff --git a/src/main.rs b/src/main.rs index 4d5a5bd..6021b08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,6 +117,10 @@ fn tui_main(_config: &str) -> Result<(), Box> { app.mode = AppMode::AddTask; app.cursor_location = app.command.chars().count(); } + Key::Char('A') => { + app.mode = AppMode::AnnotateTask; + app.cursor_location = app.command.chars().count(); + } Key::Char('?') => { app.mode = AppMode::HelpPopup; } @@ -218,6 +222,49 @@ fn tui_main(_config: &str) -> Result<(), Box> { } _ => {} }, + AppMode::AnnotateTask => match input { + Key::Char('\n') => match app.task_annotate() { + Ok(_) => { + app.mode = AppMode::Report; + app.update(); + } + Err(e) => { + app.mode = AppMode::TaskError; + app.error = e; + } + }, + Key::Esc => { + app.command = "".to_string(); + app.mode = AppMode::Report; + } + Key::Right => { + if app.cursor_location < app.command.chars().count() { + app.cursor_location += 1; + } + } + Key::Left => { + if app.cursor_location > 0 { + app.cursor_location -= 1; + } + } + Key::Char(c) => { + if app.cursor_location < app.command.chars().count() { + app.command.insert_str(app.cursor_location, &c.to_string()); + } else { + app.command.push(c); + } + app.cursor_location += 1; + } + Key::Backspace => { + if app.cursor_location > 0 { + app.cursor_location -= 1; + let mut cs = app.command.chars().collect::>(); + cs.remove(app.cursor_location); + app.command = cs.into_iter().collect(); + } + } + _ => {} + }, AppMode::AddTask => match input { Key::Char('\n') => match app.task_add() { Ok(_) => {