sara-tasks 1.5.0

Sara — folder-aware task manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use anyhow::Result;
use chrono::{Local, Utc};
use rusqlite::Connection;

use crate::infrastructure::config::Config;
use crate::infrastructure::db;
use crate::infrastructure::model::{Priority, Task};
use crate::infrastructure::project::detect_current_project;

const RESET: &str = "\x1b[0m";
const BOLD: &str = "\x1b[1m";
const DIM: &str = "\x1b[2m";
const RED: &str = "\x1b[31m";
const YELLOW: &str = "\x1b[33m";
const GREEN: &str = "\x1b[32m";
const CYAN: &str = "\x1b[36m";
const GRAY: &str = "\x1b[90m";
const MAGENTA: &str = "\x1b[35m";

/// Resolve the project filter for a list request: `None` when `all`, an explicit
/// override when given, else the current git project.
fn resolve_filter(
    conn: &Connection,
    cfg: &Config,
    all: bool,
    project_filter: Option<&str>,
) -> Result<Option<String>> {
    if all {
        Ok(None)
    } else if let Some(p) = project_filter {
        Ok(Some(p.to_string()))
    } else {
        let (name, _) = detect_current_project(conn, cfg)?;
        Ok(Some(name))
    }
}

/// Serialize a slice of tasks into the `{ "tasks": [...] }` shape. Single source
/// of truth for the `--json` CLI path and the MCP `list` tool.
fn tasks_to_value(tasks: &[Task]) -> serde_json::Value {
    let arr: Vec<_> = tasks
        .iter()
        .map(|t| {
            serde_json::json!({
                "id": t.id,
                "uuid": t.uuid.to_string(),
                "description": t.description,
                "project": t.project,
                "priority": t.priority.as_ref().map(|p| p.label()),
                "due": t.due.map(|d| d.to_rfc3339()),
                "urgency": t.urgency,
                "status": t.status.to_string(),
                "tags": t.tags,
            })
        })
        .collect();
    serde_json::json!({ "tasks": arr })
}

/// Structured task list for the MCP `list` tool.
pub fn list_value(
    conn: &Connection,
    cfg: &Config,
    all: bool,
    project_filter: Option<&str>,
) -> Result<serde_json::Value> {
    let filter = resolve_filter(conn, cfg, all, project_filter)?;
    let tasks = db::list_tasks(conn, filter.as_deref())?;
    Ok(tasks_to_value(&tasks))
}

pub fn run(
    conn: &Connection,
    cfg: &Config,
    all: bool,
    project_filter: Option<&str>,
    as_json: bool,
    by_issue: bool,
) -> Result<()> {
    let no_color = std::env::var("NO_COLOR").is_ok();

    let filter = resolve_filter(conn, cfg, all, project_filter)?;

    let tasks = db::list_tasks(conn, filter.as_deref())?;
    let link_flags = db::link_flags_by_task(conn).unwrap_or_default();
    let github_synced = db::github_synced_task_uuids(conn).unwrap_or_default();
    let dep_info = db::dep_info_by_task(conn).unwrap_or_default();

    if as_json {
        println!("{}", serde_json::to_string_pretty(&tasks_to_value(&tasks))?);
        return Ok(());
    }

    if by_issue {
        return print_by_issue(conn, &tasks, no_color);
    }

    if tasks.is_empty() {
        let scope = filter
            .as_deref()
            .map(|p| format!("project '{p}'"))
            .unwrap_or_else(|| "any project".to_string());
        println!("No pending tasks for {scope}.");
        if !all && filter.is_some() {
            let all_tasks = db::list_tasks(conn, None)?;
            if !all_tasks.is_empty() {
                println!(
                    "Tip: run `sara list -a` to see all {} pending tasks.",
                    all_tasks.len()
                );
            }
        }
        return Ok(());
    }

    // Header
    let header = format!(
        "    {id:>3}  {pri:<4}  {proj:<16}  {due:<12}  {urg:>6}  {dep:<16}  {desc}",
        id = "ID",
        pri = "PRI",
        proj = "PROJECT",
        due = "DUE",
        urg = "URG",
        dep = "DEPS",
        desc = "DESCRIPTION",
    );
    if no_color {
        println!("{header}");
        println!("{}", "".repeat(header.len()));
    } else {
        println!("{BOLD}{header}{RESET}");
        println!("{DIM}{}{RESET}", "".repeat(80));
    }

    for task in &tasks {
        let id_str = task
            .id
            .map(|i| i.to_string())
            .unwrap_or_else(|| "-".to_string());
        let pri_str = task
            .priority
            .as_ref()
            .map(|p| p.label().to_string())
            .unwrap_or_else(|| "-".to_string());
        let due_str = task
            .due
            .map(|d| d.with_timezone(&Local).format("%Y-%m-%d").to_string())
            .unwrap_or_else(|| "-".to_string());
        let urg_str = format!("{:.1}", task.urgency);

        // Truncate project and description for display
        let proj_display = truncate(&task.project, 16);
        let desc_display = truncate(&task.description, 60);

        let active_marker = if task.is_active() { "" } else { " " };

        // PR / link / recur indicators for an at-a-glance scan.
        let flags = link_flags
            .get(&task.uuid.to_string())
            .copied()
            .unwrap_or_default();
        let recur_mark = if task.recur.is_some() { "" } else { " " };

        // Dependency state: ⊘ = blocked by an unfinished task, ⛓ = blocks others.
        // The glyph is a quick-scan gutter marker; the DEPS column spells it out.
        let dep = dep_info.get(&task.uuid.to_string());
        let dep_mark = match dep {
            Some(d) if d.is_blocked() => "",
            Some(d) if d.blocking > 0 => "",
            _ => " ",
        };
        let dep_text = truncate(&dep_column_text(dep), DEP_COL_W);
        let is_synced = github_synced.contains(&task.uuid.to_string());
        let link_badge = LinkBadge::from_flags(flags, is_synced);
        let pr_badge_plain = match link_badge {
            LinkBadge::Pr => "[PR] ",
            LinkBadge::Issue => "[ISSUE] ",
            LinkBadge::Link => "[link] ",
            LinkBadge::None => "",
        };

        // Colorize
        if no_color {
            println!(
                "{active}{recur}{dep} {id:>3}  {pri:<4}  {proj:<16}  {due:<12}  {urg:>6}  {deptext:<width$}  {pr}{desc}",
                active = active_marker,
                recur = recur_mark,
                dep = dep_mark,
                id = id_str,
                pri = pri_str,
                proj = proj_display,
                due = due_str,
                urg = urg_str,
                deptext = dep_text,
                width = DEP_COL_W,
                pr = pr_badge_plain,
                desc = desc_display,
            );
        } else {
            let pri_colored = match &task.priority {
                Some(Priority::H) => format!("{RED}{BOLD}{pri_str:<4}{RESET}"),
                Some(Priority::M) => format!("{YELLOW}{pri_str:<4}{RESET}"),
                Some(Priority::L) => format!("{GREEN}{pri_str:<4}{RESET}"),
                None => format!("{GRAY}{pri_str:<4}{RESET}"),
            };
            let due_colored = color_due(task, &due_str, no_color);
            let active_col = if task.is_active() {
                format!("{GREEN}{RESET}")
            } else {
                " ".to_string()
            };
            let recur_col = if task.recur.is_some() {
                format!("{CYAN}{RESET}")
            } else {
                " ".to_string()
            };
            let dep_col = match dep {
                Some(d) if d.is_blocked() => format!("{RED}{RESET}"),
                Some(d) if d.blocking > 0 => format!("{CYAN}{RESET}"),
                _ => " ".to_string(),
            };
            let dep_padded = format!("{:<width$}", dep_text, width = DEP_COL_W);
            let dep_text_col = match dep {
                Some(d) if d.is_blocked() => format!("{RED}{dep_padded}{RESET}"),
                Some(d) if d.blocking > 0 => format!("{GRAY}{dep_padded}{RESET}"),
                _ => dep_padded,
            };
            let pr_badge = match link_badge {
                LinkBadge::Pr => format!("{MAGENTA}{BOLD}PR{RESET} "),
                LinkBadge::Issue => format!("{GREEN}{BOLD}ISS{RESET} "),
                LinkBadge::Link => format!("{CYAN}{RESET} "),
                LinkBadge::None => String::new(),
            };
            println!(
                "{active}{recur}{dep} {CYAN}{id:>3}{RESET}  {pri}  {GRAY}{proj:<16}{RESET}  {due:<12}  {GRAY}{urg:>6}{RESET}  {deptext}  {pr}{desc}",
                active = active_col,
                recur = recur_col,
                dep = dep_col,
                id = id_str,
                pri = pri_colored,
                proj = proj_display,
                due = due_colored,
                urg = urg_str,
                deptext = dep_text_col,
                pr = pr_badge,
                desc = desc_display,
            );
        }
    }

    println!();
    let summary = format!(
        "Showing {} task{}{}",
        tasks.len(),
        if tasks.len() == 1 { "" } else { "s" },
        filter
            .as_deref()
            .map(|p| format!(" for project '{p}'"))
            .unwrap_or_default()
    );
    if no_color {
        println!("{summary}");
    } else {
        println!("{DIM}{summary}{RESET}");
    }

    Ok(())
}

/// `sara list --by-issue`: trace tasks back to the GitHub issue they link to,
/// so a broken-down issue and its concrete tasks read together at a glance.
fn print_by_issue(conn: &Connection, tasks: &[Task], no_color: bool) -> Result<()> {
    let (groups, ungrouped) = db::group_tasks_by_issue(conn, tasks)?;

    if groups.is_empty() && ungrouped.is_empty() {
        println!("No pending tasks.");
        return Ok(());
    }

    let print_group_rows = |tasks: &[Task]| {
        for task in tasks {
            let id_str = task
                .id
                .map(|i| i.to_string())
                .unwrap_or_else(|| "-".to_string());
            println!("   {:>3}  {}", id_str, truncate(&task.description, 60));
        }
    };

    for group in &groups {
        let header = format!("Issue #{} · {}", group.number, group.owner_repo);
        if no_color {
            println!("{header}");
        } else {
            println!("{BOLD}{CYAN}{header}{RESET}");
        }
        print_group_rows(&group.tasks);
        println!();
    }

    if !ungrouped.is_empty() {
        if no_color {
            println!("No linked issue");
        } else {
            println!("{DIM}No linked issue{RESET}");
        }
        print_group_rows(&ungrouped);
        println!();
    }

    Ok(())
}

/// Which link badge a task should show at a glance, in priority order.
/// PR outranks Issue, which outranks a plain/generic link.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LinkBadge {
    Pr,
    Issue,
    Link,
    None,
}

impl LinkBadge {
    /// `synced` means the task itself was imported by `sara sync` (has GitHub
    /// provenance) — not merely that it links back to an issue for
    /// traceability, which every task in a broken-down issue does.
    fn from_flags(flags: db::LinkFlags, synced: bool) -> Self {
        if flags.pr {
            LinkBadge::Pr
        } else if flags.issue && synced {
            LinkBadge::Issue
        } else if flags.any {
            LinkBadge::Link
        } else {
            LinkBadge::None
        }
    }
}

/// Width of the DEPS column in the task list.
const DEP_COL_W: usize = 16;

fn fmt_id_list(ids: &[i64]) -> String {
    ids.iter()
        .map(|i| i.to_string())
        .collect::<Vec<_>>()
        .join(",")
}

/// Plain text for the DEPS column: what the task is waiting on or blocking.
fn dep_column_text(dep: Option<&db::DepInfo>) -> String {
    match dep {
        Some(d) if d.is_blocked() => format!("blocked by {}", fmt_id_list(&d.blocked_by)),
        Some(d) if d.blocking > 0 => format!(
            "blocks {} task{}",
            d.blocking,
            if d.blocking == 1 { "" } else { "s" }
        ),
        _ => String::new(),
    }
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        let mut out: String = s.chars().take(max - 1).collect();
        out.push('');
        out
    }
}

fn color_due(task: &Task, due_str: &str, no_color: bool) -> String {
    if no_color || task.due.is_none() {
        return format!("{due_str:<12}");
    }
    let now = Utc::now();
    let days = task.due.map(|d| (d - now).num_days()).unwrap_or(999);
    let color = if days < 0 {
        RED
    } else if days <= 1 {
        YELLOW
    } else {
        RESET
    };
    format!("{color}{due_str:<12}{RESET}")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn link_badge_precedence_pr_beats_issue_beats_generic_link() {
        assert_eq!(
            LinkBadge::from_flags(
                db::LinkFlags {
                    any: true,
                    pr: true,
                    issue: true,
                },
                true,
            ),
            LinkBadge::Pr
        );
        assert_eq!(
            LinkBadge::from_flags(
                db::LinkFlags {
                    any: true,
                    pr: false,
                    issue: true,
                },
                true,
            ),
            LinkBadge::Issue
        );
        assert_eq!(
            LinkBadge::from_flags(
                db::LinkFlags {
                    any: true,
                    pr: false,
                    issue: false,
                },
                true,
            ),
            LinkBadge::Link
        );
        assert_eq!(
            LinkBadge::from_flags(db::LinkFlags::default(), true),
            LinkBadge::None
        );
    }

    #[test]
    fn link_badge_issue_link_without_sync_provenance_is_generic_link() {
        // A task that merely links back to an issue for traceability (e.g. a
        // subtask generated while breaking down an issue) is not itself an
        // imported task, so it should not wear the ISS badge.
        assert_eq!(
            LinkBadge::from_flags(
                db::LinkFlags {
                    any: true,
                    pr: false,
                    issue: true,
                },
                false,
            ),
            LinkBadge::Link
        );
    }
}