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,20 +266,19 @@ impl App {
let task_id = self.tasks[selected].id().unwrap_or_default();
let output = Command::new("task")
.arg(format!("{}", task_id))
.output()
.expect(
&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()
.borders(Borders::ALL)
.title(format!("Task {}", task_id)),
);
f.render_widget(p, rect);
.output();
match output {
Ok(output) => {
let data = String::from_utf8(output.stdout).unwrap();
let p = Paragraph::new(Text::from(&data[..])).block(
Block::default()
.borders(Borders::ALL)
.title(format!("Task {}", task_id)),
);
f.render_widget(p, rect);
},
Err(_) => (),
}
}
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 task_id = self.tasks[selected].id().unwrap_or_default();
let output = Command::new("task")
.arg(format!("{}", task_id))
.output()
.expect(
&format!(
"Cannot run `task {}`. Check documentation for more information",
task_id
)[..],
);
let data = String::from_utf8(output.stdout).unwrap();
for line in data.split("\n") {
if line.starts_with("Virtual tags") {
let line = line.to_string();
let line = line.replace("Virtual tags", "");
return line;
}
.output();
match output {
Ok(output) => {
let data = String::from_utf8(output.stdout).unwrap();
for line in data.split("\n") {
if line.starts_with("Virtual tags") {
let line = line.to_string();
let line = line.replace("Virtual tags", "");
return Ok(line);
}
}
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))
}
"".to_string()
}
pub fn task_start_or_stop(&mut self) {
pub fn task_start_or_stop(&mut self) -> Result<(), String> {
if self.tasks.len() == 0 {
return
return Ok(());
}
let selected = self.state.selected().unwrap_or_default();
let task_id = self.tasks[selected].id().unwrap_or_default();
let mut command = "start";
for tag in self.task_virtual_tags().split(" ") {
for tag in self.task_virtual_tags()?.split(" ") {
if tag == "ACTIVE" {
command = "stop"
}
@ -632,13 +631,11 @@ impl App {
let output = Command::new("task")
.arg(command)
.arg(format!("{}", task_id))
.output()
.expect(
&format!(
"Cannot run `task done` for task `{}`. Check documentation for more information",
task_id
)[..],
);
.output();
match output {
Ok(_) => Ok(()),
Err(_) => Err(format!("Cannot run `task done` for task `{}`. Check documentation for more information", task_id)),
}
}
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::Char('d') => app.task_done(),
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('e') => {
events.pause_event_loop(&mut terminal);