1use anyhow::Result;
2use comfy_table::presets::NOTHING;
3use comfy_table::{Cell, Color, Table};
4use std::path::Path;
5
6use crate::color::{cell_bold, cell_fg, stdout_use_color};
7use crate::db;
8
9pub fn run(root: &Path, json: bool) -> Result<()> {
10 let store = db::open(root)?;
11
12 let mut tasks = Vec::new();
13 for task in store.list_tasks()? {
14 if task.status != db::Status::Open {
15 continue;
16 }
17 let blockers = db::partition_blockers(&store, &task.blockers)?;
18 if blockers.open.is_empty() {
19 tasks.push(task);
20 }
21 }
22
23 tasks.sort_by_key(|t| (t.priority.score(), t.created_at.clone()));
24
25 if json {
26 println!("{}", serde_json::to_string(&tasks)?);
27 } else {
28 let use_color = stdout_use_color();
29 let mut table = Table::new();
30 table.load_preset(NOTHING);
31 table.set_header(vec!["ID", "PRIORITY", "EFFORT", "TITLE"]);
32 for t in &tasks {
33 table.add_row(vec![
34 cell_bold(&t.id, use_color),
35 cell_fg(db::priority_label(t.priority), Color::Red, use_color),
36 cell_fg(db::effort_label(t.effort), Color::Blue, use_color),
37 Cell::new(&t.title),
38 ]);
39 }
40 if !tasks.is_empty() {
41 println!("{table}");
42 }
43 }
44
45 Ok(())
46}