1use anyhow::Result;
2use comfy_table::presets::NOTHING;
3use comfy_table::{Cell, Table};
4use std::path::Path;
5
6use crate::color::{cell_bold, stdout_use_color};
7use crate::db;
8
9pub fn run(root: &Path, query: &str, json: bool) -> Result<()> {
10 let store = db::open(root)?;
11 let q = query.to_lowercase();
12
13 let tasks: Vec<db::Task> = store
14 .list_tasks()?
15 .into_iter()
16 .filter(|t| {
17 t.title.to_lowercase().contains(&q) || t.description.to_lowercase().contains(&q)
18 })
19 .collect();
20
21 if json {
22 println!("{}", serde_json::to_string(&tasks)?);
23 } else {
24 let use_color = stdout_use_color();
25 let mut table = Table::new();
26 table.load_preset(NOTHING);
27 for t in &tasks {
28 table.add_row(vec![cell_bold(&t.id, use_color), Cell::new(&t.title)]);
29 }
30 if !tasks.is_empty() {
31 println!("{table}");
32 }
33 }
34
35 Ok(())
36}