search.rs

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