1use anyhow::Result;
2use comfy_table::presets::NOTHING;
3use comfy_table::Table;
4use std::path::Path;
5
6use crate::db;
7
8pub fn run(root: &Path, query: &str, json: bool) -> Result<()> {
9 let conn = db::open(root)?;
10 let pattern = format!("%{query}%");
11
12 let mut stmt = conn.prepare(
13 "SELECT id, title, description, type, priority, status, effort, parent, created, updated
14 FROM tasks
15 WHERE title LIKE ?1 OR description LIKE ?1",
16 )?;
17
18 let tasks: Vec<db::Task> = stmt
19 .query_map([&pattern], db::row_to_task)?
20 .collect::<rusqlite::Result<_>>()?;
21
22 if json {
23 let summary: Vec<serde_json::Value> = tasks
24 .iter()
25 .map(|t| {
26 serde_json::json!({
27 "id": t.id,
28 "title": t.title,
29 "status": t.status,
30 })
31 })
32 .collect();
33 println!("{}", serde_json::to_string(&summary)?);
34 } else {
35 let c = crate::color::stdout_theme();
36 let mut table = Table::new();
37 table.load_preset(NOTHING);
38 for t in &tasks {
39 table.add_row(vec![
40 format!("{}{}{}", c.bold, t.id, c.reset),
41 t.title.clone(),
42 ]);
43 }
44 if !tasks.is_empty() {
45 println!("{table}");
46 }
47 }
48
49 Ok(())
50}