frame 0.1.5

A markdown task tracker with a terminal UI for humans and a CLI for agents
Documentation
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
use serde::Serialize;

use crate::model::task::{Metadata, Task, TaskState};
use crate::model::track::Track;
use crate::ops::track_ops::TrackStats;

// ---------------------------------------------------------------------------
// JSON output structs
// ---------------------------------------------------------------------------

#[derive(Serialize)]
pub struct TaskJson {
    pub id: Option<String>,
    pub title: String,
    pub state: TaskState,
    pub tags: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub deps: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub spec: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub refs: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub added: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub subtasks: Vec<TaskJson>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub ancestors: Vec<TaskJson>,
}

#[derive(Serialize)]
pub struct TaskListJson {
    pub track: String,
    pub tasks: Vec<TaskJson>,
}

#[derive(Serialize)]
pub struct ReadyJson {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub focus_track: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc_only: Option<bool>,
    pub tasks: Vec<TaskWithTrackJson>,
}

#[derive(Serialize)]
pub struct TaskWithTrackJson {
    pub track: String,
    #[serde(flatten)]
    pub task: TaskJson,
}

#[derive(Serialize)]
pub struct TrackInfoJson {
    pub id: String,
    pub name: String,
    pub state: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc_focus: Option<bool>,
    pub stats: TrackStatsJson,
}

#[derive(Serialize)]
pub struct TrackStatsJson {
    pub active: usize,
    pub blocked: usize,
    pub todo: usize,
    pub parked: usize,
    pub done: usize,
}

#[derive(Serialize)]
pub struct InboxItemJson {
    pub index: usize,
    pub title: String,
    pub tags: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
}

#[derive(Serialize)]
pub struct StatsJson {
    pub tracks: Vec<TrackStatsEntryJson>,
    pub totals: TrackStatsJson,
}

#[derive(Serialize)]
pub struct TrackStatsEntryJson {
    pub id: String,
    pub name: String,
    pub stats: TrackStatsJson,
}

#[derive(Serialize)]
pub struct SearchHitJson {
    pub track: String,
    pub task_id: String,
    pub title: String,
    pub field: String,
}

// ---------------------------------------------------------------------------
// Conversions
// ---------------------------------------------------------------------------

pub fn task_to_json(task: &Task) -> TaskJson {
    let mut deps = Vec::new();
    let mut refs = Vec::new();
    let mut spec = None;
    let mut note = None;
    let mut added = None;
    let mut resolved = None;

    for m in &task.metadata {
        match m {
            Metadata::Dep(d) => deps.extend(d.iter().cloned()),
            Metadata::Ref(r) => refs.extend(r.iter().cloned()),
            Metadata::Spec(s) => spec = Some(s.clone()),
            Metadata::Note(n) => note = Some(n.clone()),
            Metadata::Added(a) => added = Some(a.clone()),
            Metadata::Resolved(r) => resolved = Some(r.clone()),
        }
    }

    TaskJson {
        id: task.id.clone(),
        title: task.title.clone(),
        state: task.state,
        tags: task.tags.clone(),
        deps,
        spec,
        refs,
        note,
        added,
        resolved,
        subtasks: task.subtasks.iter().map(task_to_json).collect(),
        ancestors: Vec::new(),
    }
}

pub fn stats_to_json(stats: &TrackStats) -> TrackStatsJson {
    TrackStatsJson {
        active: stats.active,
        blocked: stats.blocked,
        todo: stats.todo,
        parked: stats.parked,
        done: stats.done,
    }
}

// ---------------------------------------------------------------------------
// Human-readable formatting
// ---------------------------------------------------------------------------

fn state_char(state: TaskState) -> char {
    state.checkbox_char()
}

/// Format a single task as a one-line summary
pub fn format_task_line(task: &Task) -> String {
    let sc = state_char(task.state);
    let id_str = task
        .id
        .as_ref()
        .map(|id| format!("{} ", id))
        .unwrap_or_default();
    let tags_str = if task.tags.is_empty() {
        String::new()
    } else {
        format!(
            " {}",
            task.tags
                .iter()
                .map(|t| format!("#{}", t))
                .collect::<Vec<_>>()
                .join(" ")
        )
    };
    format!("[{}] {}{}{}", sc, id_str, task.title, tags_str)
}

/// Format a task with its subtasks, indented
pub fn format_task_tree(task: &Task, indent: usize) -> Vec<String> {
    let mut lines = Vec::new();
    let prefix = "  ".repeat(indent);
    lines.push(format!("{}{}", prefix, format_task_line(task)));

    for sub in &task.subtasks {
        lines.extend(format_task_tree(sub, indent + 1));
    }
    lines
}

/// Format detailed task view
pub fn format_task_detail(task: &Task) -> Vec<String> {
    let mut lines = Vec::new();

    // Header
    let sc = state_char(task.state);
    let id_str = task
        .id
        .as_ref()
        .map(|id| format!("{} ", id))
        .unwrap_or_default();
    lines.push(format!("[{}] {}{}", sc, id_str, task.title));

    // Tags
    if !task.tags.is_empty() {
        lines.push(format!(
            "tags: {}",
            task.tags
                .iter()
                .map(|t| format!("#{}", t))
                .collect::<Vec<_>>()
                .join(" ")
        ));
    }

    // Metadata
    for m in &task.metadata {
        match m {
            Metadata::Added(d) => lines.push(format!("added: {}", d)),
            Metadata::Resolved(d) => lines.push(format!("resolved: {}", d)),
            Metadata::Dep(deps) => lines.push(format!("dep: {}", deps.join(", "))),
            Metadata::Spec(s) => lines.push(format!("spec: {}", s)),
            Metadata::Ref(refs) => {
                for r in refs {
                    lines.push(format!("ref: {}", r));
                }
            }
            Metadata::Note(n) => {
                lines.push("note:".to_string());
                for line in n.lines() {
                    lines.push(format!("  {}", line));
                }
            }
        }
    }

    // Subtasks
    if !task.subtasks.is_empty() {
        lines.push(String::new());
        lines.push("subtasks:".to_string());
        for sub in &task.subtasks {
            for line in format_task_tree(sub, 1) {
                lines.push(line);
            }
        }
    }

    lines
}

/// Format a separator line for context display
fn format_context_separator(label: &str, task: &Task) -> String {
    let id_str = task
        .id
        .as_ref()
        .map(|id| format!("{} ", id))
        .unwrap_or_default();
    format!("── {} ── {}{}", label, id_str, task.title)
}

/// Format task detail with ancestor context (--context flag)
pub fn format_task_detail_with_context(ancestors: &[&Task], task: &Task) -> Vec<String> {
    let mut lines = Vec::new();

    for ancestor in ancestors {
        lines.push(format_context_separator("Parent", ancestor));
        lines.extend(format_context_fields(ancestor));
        lines.push(String::new());
    }

    lines.push(format_context_separator("Task", task));
    lines.extend(format_context_fields(task));

    // Subtasks
    if !task.subtasks.is_empty() {
        lines.push(String::new());
        lines.push("subtasks:".to_string());
        for sub in &task.subtasks {
            for line in format_task_tree(sub, 1) {
                lines.push(line);
            }
        }
    }

    lines
}

/// Format the fields of a task for context display (indented, no header)
fn format_context_fields(task: &Task) -> Vec<String> {
    let mut lines = Vec::new();

    let state_str = match task.state {
        TaskState::Todo => "todo",
        TaskState::Active => "active",
        TaskState::Blocked => "blocked",
        TaskState::Done => "done",
        TaskState::Parked => "parked",
    };
    lines.push(format!("  state: {}", state_str));

    if !task.tags.is_empty() {
        lines.push(format!(
            "  tags: {}",
            task.tags
                .iter()
                .map(|t| format!("#{}", t))
                .collect::<Vec<_>>()
                .join(" ")
        ));
    }

    for m in &task.metadata {
        match m {
            Metadata::Added(d) => lines.push(format!("  added: {}", d)),
            Metadata::Resolved(d) => lines.push(format!("  resolved: {}", d)),
            Metadata::Dep(deps) => lines.push(format!("  dep: {}", deps.join(", "))),
            Metadata::Spec(s) => lines.push(format!("  spec: {}", s)),
            Metadata::Ref(refs) => {
                for r in refs {
                    lines.push(format!("  ref: {}", r));
                }
            }
            Metadata::Note(n) => {
                lines.push("  note:".to_string());
                for line in n.lines() {
                    lines.push(format!("    {}", line));
                }
            }
        }
    }

    lines
}

/// Format a track listing header
pub fn format_track_header(track_id: &str, track: &Track) -> String {
    format!("== {} ({}) ==", track.title, track_id)
}

/// Format a track's task listing
pub fn format_track_listing(
    track_id: &str,
    track: &Track,
    state_filter: Option<TaskState>,
    tag_filter: Option<&str>,
) -> Vec<String> {
    let mut lines = Vec::new();
    lines.push(format_track_header(track_id, track));
    lines.push(String::new());

    let backlog = track.backlog();
    let parked = track.parked();

    let filter = |task: &&Task| -> bool {
        if let Some(sf) = state_filter
            && task.state != sf
        {
            return false;
        }
        if let Some(tf) = tag_filter
            && !task.tags.iter().any(|t| t == tf)
        {
            return false;
        }
        true
    };

    let filtered_backlog: Vec<_> = backlog.iter().filter(filter).collect();
    let filtered_parked: Vec<_> = parked.iter().filter(filter).collect();

    for task in &filtered_backlog {
        for line in format_task_tree(task, 0) {
            lines.push(line);
        }
    }

    if !filtered_parked.is_empty() {
        if !filtered_backlog.is_empty() {
            lines.push(String::new());
        }
        lines.push("-- Parked --".to_string());
        for task in &filtered_parked {
            for line in format_task_tree(task, 0) {
                lines.push(line);
            }
        }
    }

    lines
}

/// Parse a state string into TaskState
pub fn parse_task_state(s: &str) -> Result<TaskState, String> {
    match s {
        "todo" => Ok(TaskState::Todo),
        "active" => Ok(TaskState::Active),
        "blocked" => Ok(TaskState::Blocked),
        "done" => Ok(TaskState::Done),
        "parked" => Ok(TaskState::Parked),
        _ => Err(format!(
            "unknown state '{}' (expected: todo, active, blocked, done, parked)",
            s
        )),
    }
}