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