Change start or stop to support errors

This commit is contained in:
Dheepak Krishnamurthy 2020-07-30 15:48:46 -06:00
parent e71c59e8b3
commit ca1444865a
2 changed files with 44 additions and 41 deletions

View file

@ -266,13 +266,9 @@ impl App {
let task_id = self.tasks[selected].id().unwrap_or_default(); let task_id = self.tasks[selected].id().unwrap_or_default();
let output = Command::new("task") let output = Command::new("task")
.arg(format!("{}", task_id)) .arg(format!("{}", task_id))
.output() .output();
.expect( match output {
&format!( Ok(output) => {
"Unable to show details for `task {}`. Check documentation for more information",
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()
@ -280,6 +276,9 @@ impl App {
.title(format!("Task {}", task_id)), .title(format!("Task {}", task_id)),
); );
f.render_widget(p, rect); f.render_widget(p, rect);
},
Err(_) => (),
}
} }
fn draw_task_report(&mut self, f: &mut Frame<impl Backend>, rect: Rect) { fn draw_task_report(&mut self, f: &mut Frame<impl Backend>, rect: Rect) {
@ -593,37 +592,37 @@ impl App {
} }
} }
pub fn task_virtual_tags(& self) -> String { pub fn task_virtual_tags(& self) -> Result<String, String> {
let selected = self.state.selected().unwrap_or_default(); let selected = self.state.selected().unwrap_or_default();
let task_id = self.tasks[selected].id().unwrap_or_default(); let task_id = self.tasks[selected].id().unwrap_or_default();
let output = Command::new("task") let output = Command::new("task")
.arg(format!("{}", task_id)) .arg(format!("{}", task_id))
.output() .output();
.expect(
&format!( match output {
"Cannot run `task {}`. Check documentation for more information", Ok(output) => {
task_id
)[..],
);
let data = String::from_utf8(output.stdout).unwrap(); let data = String::from_utf8(output.stdout).unwrap();
for line in data.split("\n") { for line in data.split("\n") {
if line.starts_with("Virtual tags") { if line.starts_with("Virtual tags") {
let line = line.to_string(); let line = line.to_string();
let line = line.replace("Virtual tags", ""); let line = line.replace("Virtual tags", "");
return line; return Ok(line);
} }
} }
"".to_string() return Err(format!("Cannot find any tags for `task {}`. Check documentation for more information", task_id))
},
Err(_) => Err(format!("Cannot run `task {}`. Check documentation for more information", task_id))
}
} }
pub fn task_start_or_stop(&mut self) { pub fn task_start_or_stop(&mut self) -> Result<(), String> {
if self.tasks.len() == 0 { if self.tasks.len() == 0 {
return return Ok(());
} }
let selected = self.state.selected().unwrap_or_default(); let selected = self.state.selected().unwrap_or_default();
let task_id = self.tasks[selected].id().unwrap_or_default(); let task_id = self.tasks[selected].id().unwrap_or_default();
let mut command = "start"; let mut command = "start";
for tag in self.task_virtual_tags().split(" ") { for tag in self.task_virtual_tags()?.split(" ") {
if tag == "ACTIVE" { if tag == "ACTIVE" {
command = "stop" command = "stop"
} }
@ -632,13 +631,11 @@ impl App {
let output = Command::new("task") let output = Command::new("task")
.arg(command) .arg(command)
.arg(format!("{}", task_id)) .arg(format!("{}", task_id))
.output() .output();
.expect( match output {
&format!( Ok(_) => Ok(()),
"Cannot run `task done` for task `{}`. Check documentation for more information", Err(_) => Err(format!("Cannot run `task done` for task `{}`. Check documentation for more information", task_id)),
task_id }
)[..],
);
} }
pub fn task_delete(&self) { pub fn task_delete(&self) {

View file

@ -56,7 +56,13 @@ fn main() -> Result<(), Box<dyn Error>> {
Key::Up | Key::Char('k') => app.previous(), Key::Up | Key::Char('k') => app.previous(),
Key::Char('d') => app.task_done(), Key::Char('d') => app.task_done(),
Key::Char('x') => app.task_delete(), Key::Char('x') => app.task_delete(),
Key::Char('s') => app.task_start_or_stop(), Key::Char('s') => match app.task_start_or_stop() {
Ok(_) => (),
Err(e) => {
app.mode = AppMode::TaskError;
app.error = e;
}
},
Key::Char('u') => app.task_undo(), Key::Char('u') => app.task_undo(),
Key::Char('e') => { Key::Char('e') => {
events.pause_event_loop(&mut terminal); events.pause_event_loop(&mut terminal);