dextui 0.1.0

A two-pane terminal UI for browsing and triaging dex tasks
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Turns dex's flat task array into a hierarchy, with search and status filtering.

use std::collections::{HashMap, HashSet};

use crate::dex::Task;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Filter {
    /// Everything, including completed. Mirrors `dex list --all`.
    All,
    /// Not yet completed. Mirrors the default `dex list`.
    Pending,
    /// Started but not completed. Mirrors `dex list --in-progress`.
    InProgress,
}

impl Filter {
    pub fn next(self) -> Filter {
        match self {
            Filter::Pending => Filter::InProgress,
            Filter::InProgress => Filter::All,
            Filter::All => Filter::Pending,
        }
    }

    /// Menu order, which is not cycle order: the menu reads widest-to-narrowest
    /// scope so it looks like a scale, while `next` steps from where you usually
    /// are. Both are deliberate and neither should be derived from the other.
    pub const MENU: [Filter; 3] = [Filter::All, Filter::Pending, Filter::InProgress];

    /// The name alone. It is also the whole menu's vocabulary -- `ui` draws the
    /// three of these and marks the current one, rather than being handed a
    /// pre-rendered string it cannot style or hit-test a part of.
    ///
    /// Lowercase: the current filter is marked with weight and colour, so
    /// shouting is no longer how you tell. A filter silently hiding tasks with
    /// nothing on screen saying so is the most confusing state this app has, and
    /// the mark is what prevents it.
    pub fn name(self) -> &'static str {
        match self {
            Filter::All => "all",
            Filter::InProgress => "active",
            Filter::Pending => "pending",
        }
    }
}

/// How siblings are ordered. Applied at every level, so the hierarchy, the
/// progress rollups and expand/collapse all keep working.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sort {
    /// priority, then creation time, then name.
    Priority,
    Updated,
    Created,
    Name,
}

impl Sort {
    pub fn next(self) -> Sort {
        match self {
            Sort::Priority => Sort::Updated,
            Sort::Updated => Sort::Created,
            Sort::Created => Sort::Name,
            Sort::Name => Sort::Priority,
        }
    }

    /// `reversed` flips each order's *natural* direction rather than meaning a
    /// blanket ascending/descending: newest-first is the useful default for
    /// timestamps, lowest-number-first for priority, A-Z for names.
    pub fn label(self, reversed: bool) -> &'static str {
        match (self, reversed) {
            (Sort::Priority, false) => "priority",
            (Sort::Priority, true) => "priority ↓",
            (Sort::Updated, false) => "updated",
            (Sort::Updated, true) => "stalest",
            (Sort::Created, false) => "newest",
            (Sort::Created, true) => "oldest",
            (Sort::Name, false) => "A-Z",
            (Sort::Name, true) => "Z-A",
        }
    }
}

#[derive(Debug, Clone)]
pub struct Node {
    pub task: Task,
    pub children: Vec<Node>,
    /// False when this node survived filtering only because a descendant matched,
    /// so the UI can dim pure scaffolding.
    pub is_match: bool,
}

impl Node {
    pub fn id(&self) -> &str {
        &self.task.id
    }
}

/// Builds the visible forest.
///
/// dex returns tasks sorted by id, which is meaningless to a reader, so siblings
/// are ordered by priority, then creation time, then name. Any task whose
/// descendant matches is kept, so a match is never orphaned from its path.
pub fn build(tasks: &[Task], query: &str, filter: Filter, sort: Sort, reversed: bool) -> Vec<Node> {
    let by_id: HashMap<&str, &Task> = tasks.iter().map(|t| (t.id.as_str(), t)).collect();

    let mut by_parent: HashMap<&str, Vec<&Task>> = HashMap::new();
    let mut roots: Vec<&Task> = Vec::new();

    for t in tasks {
        match t.parent_id.as_deref() {
            // A task whose parent is missing from the payload becomes a root
            // rather than vanishing from the view.
            Some(p) if by_id.contains_key(p) => by_parent.entry(p).or_default().push(t),
            _ => roots.push(t),
        }
    }

    let query = query.trim();
    let query = if query.is_empty() { None } else { Some(query) };

    order(&mut roots, sort, reversed);

    let mut visiting: HashSet<&str> = HashSet::new();
    roots
        .into_iter()
        .filter_map(|r| build_node(r, &by_parent, query, filter, sort, reversed, &mut visiting))
        .collect()
}

fn build_node<'a>(
    task: &'a Task,
    by_parent: &HashMap<&'a str, Vec<&'a Task>>,
    query: Option<&str>,
    filter: Filter,
    sort: Sort,
    reversed: bool,
    visiting: &mut HashSet<&'a str>,
) -> Option<Node> {
    // Guards against a malformed store where parent links form a cycle.
    if !visiting.insert(task.id.as_str()) {
        return None;
    }

    let mut children = Vec::new();
    if let Some(kids) = by_parent.get(task.id.as_str()) {
        let mut kids = kids.clone();
        order(&mut kids, sort, reversed);
        children = kids
            .into_iter()
            .filter_map(|k| build_node(k, by_parent, query, filter, sort, reversed, visiting))
            .collect();
    }

    visiting.remove(task.id.as_str());

    let is_match = matches(task, query, filter);
    if !is_match && children.is_empty() {
        return None;
    }

    Some(Node {
        task: task.clone(),
        children,
        is_match,
    })
}

fn order(tasks: &mut [&Task], sort: Sort, reversed: bool) {
    // Name is the final tiebreak everywhere, so ordering is stable and does not
    // jitter between refreshes when the primary key ties.
    let by_name = |a: &Task, b: &Task| a.name.to_lowercase().cmp(&b.name.to_lowercase());

    // Tasks missing a timestamp sort last rather than first, in either
    // direction: an absent date is not "oldest", it is unknown.
    let stamp = |t: &Task, key: fn(&Task) -> Option<&String>| key(t).cloned();

    tasks.sort_by(|a, b| match sort {
        Sort::Priority => a
            .priority
            .cmp(&b.priority)
            .then_with(|| a.created_at.cmp(&b.created_at))
            .then_with(|| by_name(a, b)),
        Sort::Updated => stamp(b, |t| t.updated_at.as_ref())
            .cmp(&stamp(a, |t| t.updated_at.as_ref()))
            .then_with(|| by_name(a, b)),
        Sort::Created => stamp(b, |t| t.created_at.as_ref())
            .cmp(&stamp(a, |t| t.created_at.as_ref()))
            .then_with(|| by_name(a, b)),
        Sort::Name => by_name(a, b),
    });

    if reversed {
        tasks.reverse();
    }
}

fn matches(t: &Task, query: Option<&str>, filter: Filter) -> bool {
    let status_ok = match filter {
        Filter::All => true,
        Filter::Pending => !t.completed,
        Filter::InProgress => t.is_in_progress(),
    };

    if !status_ok {
        return false;
    }

    match query {
        None => true,
        Some(q) => {
            let q = q.to_lowercase();
            t.name.to_lowercase().contains(&q)
                || t
                    .description
                    .as_deref()
                    .is_some_and(|d| d.to_lowercase().contains(&q))
        }
    }
}

/// Completed vs total descendants of a task.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Progress {
    pub done: usize,
    /// Started but not finished. Tracked separately so a parent whose children
    /// are all underway does not read as a completely empty bar.
    pub active: usize,
    pub total: usize,
}

/// Completion rolled up over every descendant, for every task that has any.
///
/// Deliberately computed from the *unfiltered* task list: a meter built from the
/// filtered tree would read 0/n as soon as a filter hid the completed children,
/// which is exactly when you most want to see the real number.
pub fn subtree_progress(tasks: &[Task]) -> HashMap<String, Progress> {
    let mut by_parent: HashMap<&str, Vec<&Task>> = HashMap::new();
    for t in tasks {
        if let Some(p) = t.parent_id.as_deref() {
            by_parent.entry(p).or_default().push(t);
        }
    }

    let mut out = HashMap::new();
    for t in tasks {
        let mut progress = Progress::default();
        let mut seen: HashSet<&str> = HashSet::new();
        accumulate(t.id.as_str(), &by_parent, &mut progress, &mut seen);
        if progress.total > 0 {
            out.insert(t.id.clone(), progress);
        }
    }
    out
}

fn accumulate<'a>(
    id: &'a str,
    by_parent: &HashMap<&'a str, Vec<&'a Task>>,
    progress: &mut Progress,
    seen: &mut HashSet<&'a str>,
) {
    // Guards against a cyclic store, same as build().
    if !seen.insert(id) {
        return;
    }
    if let Some(kids) = by_parent.get(id) {
        for k in kids {
            progress.total += 1;
            if k.completed {
                progress.done += 1;
            } else if k.started_at.is_some() {
                progress.active += 1;
            }
            accumulate(k.id.as_str(), by_parent, progress, seen);
        }
    }
}

/// Every node in the forest, depth-first, ignoring expansion.
pub fn flatten(nodes: &[Node]) -> Vec<&Node> {
    let mut out = Vec::new();
    collect(nodes, &mut out);
    out
}

fn collect<'a>(nodes: &'a [Node], out: &mut Vec<&'a Node>) {
    for n in nodes {
        out.push(n);
        collect(&n.children, out);
    }
}

/// One rendered line: the node, its tree-drawing prefix, and enough state for
/// the renderer to pick an expand/collapse glyph.
///
/// The marker is deliberately NOT baked into `prefix`: which glyph to use is a
/// presentation decision that depends on the icon tier, and belongs in `ui`.
pub struct Row<'a> {
    pub node: &'a Node,
    /// Indentation plus the branch character, e.g. `"│ ├"`.
    pub prefix: String,
    pub has_children: bool,
    pub is_open: bool,
}

/// Flattens to only what is currently visible, honouring `expanded`, and builds
/// the box-drawing prefix for each row.
pub fn visible_rows<'a>(nodes: &'a [Node], expanded: &HashSet<String>) -> Vec<Row<'a>> {
    let mut out = Vec::new();
    walk(nodes, expanded, &mut String::new(), &mut out);
    out
}

fn walk<'a>(
    nodes: &'a [Node],
    expanded: &HashSet<String>,
    indent: &mut String,
    out: &mut Vec<Row<'a>>,
) {
    for (i, node) in nodes.iter().enumerate() {
        let last = i + 1 == nodes.len();
        let has_kids = !node.children.is_empty();
        let is_open = expanded.contains(node.id());

        let branch = if last { "" } else { "" };
        out.push(Row {
            node,
            prefix: format!("{indent}{branch}"),
            has_children: has_kids,
            is_open,
        });

        if has_kids && is_open {
            let added = if last { "  " } else { "" };
            indent.push_str(added);
            walk(&node.children, expanded, indent, out);
            indent.truncate(indent.len() - added.len());
        }
    }
}

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

    fn task(id: &str, parent: Option<&str>) -> Task {
        Task {
            id: id.to_string(),
            parent_id: parent.map(str::to_string),
            name: id.to_string(),
            created_at: Some("2026-01-01T00:00:00Z".to_string()),
            ..Default::default()
        }
    }

    /// The default ordering, so existing tests keep asserting sibling order
    /// under Sort::Priority without restating it everywhere.
    fn build(tasks: &[Task], query: &str, filter: Filter) -> Vec<Node> {
        super::build(tasks, query, filter, Sort::Priority, false)
    }

    fn named(id: &str, parent: Option<&str>, name: &str) -> Task {
        Task { name: name.to_string(), ..task(id, parent) }
    }

    #[test]
    fn nests_children_under_their_parent() {
        let tasks = vec![
            task("root", None),
            task("child", Some("root")),
            task("grandchild", Some("child")),
        ];

        let roots = build(&tasks, "", Filter::All);

        assert_eq!(roots.len(), 1);
        assert_eq!(roots[0].id(), "root");
        assert_eq!(roots[0].children[0].id(), "child");
        assert_eq!(roots[0].children[0].children[0].id(), "grandchild");
    }

    #[test]
    fn orders_siblings_by_priority_then_creation_time() {
        // dex returns tasks sorted by id, which is meaningless to a reader.
        let mut a = task("a", Some("r"));
        a.priority = 2;
        a.created_at = Some("2026-01-01T00:00:00Z".into());
        let mut b = task("b", Some("r"));
        b.created_at = Some("2026-01-01T00:00:50Z".into());
        let mut c = task("c", Some("r"));
        c.created_at = Some("2026-01-01T00:00:10Z".into());

        let tasks = vec![a, b, c, task("r", None)];
        let roots = build(&tasks, "", Filter::All);

        let ids: Vec<&str> = roots[0].children.iter().map(|n| n.id()).collect();
        assert_eq!(ids, vec!["c", "b", "a"]);
    }

    #[test]
    fn promotes_orphans_to_roots_instead_of_dropping_them() {
        // Parent absent from the payload; the child must stay visible.
        let tasks = vec![task("orphan", Some("missing"))];
        let roots = build(&tasks, "", Filter::All);

        assert_eq!(roots.len(), 1);
        assert_eq!(roots[0].id(), "orphan");
    }

    #[test]
    fn survives_a_parent_cycle() {
        // A malformed store must not take the whole TUI down.
        let tasks = vec![task("a", Some("b")), task("b", Some("a"))];
        let _ = build(&tasks, "", Filter::All);
    }

    #[test]
    fn pending_filter_hides_completed_tasks() {
        let mut done = task("done", None);
        done.completed = true;
        let tasks = vec![done, task("todo", None)];

        let roots = build(&tasks, "", Filter::Pending);
        assert_eq!(roots.len(), 1);
        assert_eq!(roots[0].id(), "todo");
    }

    #[test]
    fn in_progress_filter_shows_only_started_incomplete_tasks() {
        let mut started = task("started", None);
        started.started_at = Some("2026-01-01T00:00:00Z".into());
        let mut finished = task("finished", None);
        finished.started_at = Some("2026-01-01T00:00:00Z".into());
        finished.completed = true;

        let tasks = vec![started, task("not-started", None), finished];
        let roots = build(&tasks, "", Filter::InProgress);

        assert_eq!(roots.len(), 1);
        assert_eq!(roots[0].id(), "started");
    }

    #[test]
    fn a_matching_child_keeps_its_non_matching_ancestors_as_scaffolding() {
        let tasks = vec![
            named("parent", None, "unrelated"),
            named("child", Some("parent"), "login bug"),
        ];

        let roots = build(&tasks, "login", Filter::All);

        assert_eq!(roots.len(), 1);
        // Kept only to lead to the match, so the UI can dim it.
        assert!(!roots[0].is_match);
        assert!(roots[0].children[0].is_match);
    }

    #[test]
    fn query_matches_description_and_ignores_case() {
        let mut t = named("a", None, "nothing");
        t.description = Some("mentions LOGIN here".into());

        assert_eq!(build(&[t], "login", Filter::All).len(), 1);
    }

    #[test]
    fn query_that_matches_nothing_yields_an_empty_tree() {
        let tasks = vec![named("a", None, "alpha"), named("b", None, "beta")];
        assert!(build(&tasks, "zzz", Filter::All).is_empty());
    }

    #[test]
    fn visible_rows_honour_expansion() {
        let tasks = vec![task("r", None), task("kid", Some("r"))];
        let forest = build(&tasks, "", Filter::All);

        let collapsed = visible_rows(&forest, &HashSet::new());
        assert_eq!(collapsed.len(), 1, "children hidden while collapsed");

        let expanded: HashSet<String> = ["r".to_string()].into_iter().collect();
        assert_eq!(visible_rows(&forest, &expanded).len(), 2);
    }

    #[test]
    fn subtree_progress_counts_all_descendants_not_just_children() {
        let mut done = task("leaf", Some("mid"));
        done.completed = true;
        let tasks = vec![task("root", None), task("mid", Some("root")), done];

        let p = subtree_progress(&tasks);

        // root sees the grandchild too.
        assert_eq!(p["root"], Progress { done: 1, active: 0, total: 2 });
        assert_eq!(p["mid"], Progress { done: 1, active: 0, total: 1 });
    }

    #[test]
    fn subtree_progress_separates_in_flight_from_done() {
        let mut started = task("b", Some("root"));
        started.started_at = Some("2026-01-01T00:00:00Z".into());
        let mut finished = task("c", Some("root"));
        finished.completed = true;

        let tasks = vec![task("root", None), task("a", Some("root")), started, finished];

        assert_eq!(
            subtree_progress(&tasks)["root"],
            Progress { done: 1, active: 1, total: 3 }
        );
    }

    #[test]
    fn leaves_get_no_rollup_at_all() {
        // A meter on a task with no children would be meaningless.
        let tasks = vec![task("solo", None)];
        assert!(subtree_progress(&tasks).is_empty());
    }

    #[test]
    fn subtree_progress_survives_a_cycle() {
        let tasks = vec![task("a", Some("b")), task("b", Some("a"))];
        let _ = subtree_progress(&tasks);
    }

    fn stamped(id: &str, created: &str, updated: &str) -> Task {
        Task {
            created_at: Some(created.to_string()),
            updated_at: Some(updated.to_string()),
            ..task(id, None)
        }
    }

    fn ids(nodes: &[Node]) -> Vec<&str> {
        nodes.iter().map(|n| n.id()).collect()
    }

    fn sorted(tasks: &[Task], sort: Sort, reversed: bool) -> Vec<String> {
        super::build(tasks, "", Filter::All, sort, reversed)
            .iter()
            .map(|n| n.id().to_string())
            .collect()
    }

    #[test]
    fn updated_sort_puts_the_most_recent_first() {
        let tasks = vec![
            stamped("old", "2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"),
            stamped("new", "2026-01-01T00:00:00Z", "2026-03-01T00:00:00Z"),
            stamped("mid", "2026-01-01T00:00:00Z", "2026-02-01T00:00:00Z"),
        ];
        assert_eq!(sorted(&tasks, Sort::Updated, false), ["new", "mid", "old"]);
        assert_eq!(sorted(&tasks, Sort::Updated, true), ["old", "mid", "new"]);
    }

    #[test]
    fn created_sort_is_newest_first_and_reverses_to_oldest() {
        let tasks = vec![
            stamped("first", "2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"),
            stamped("third", "2026-03-01T00:00:00Z", "2026-03-01T00:00:00Z"),
            stamped("second", "2026-02-01T00:00:00Z", "2026-02-01T00:00:00Z"),
        ];
        assert_eq!(sorted(&tasks, Sort::Created, false), ["third", "second", "first"]);
        assert_eq!(sorted(&tasks, Sort::Created, true), ["first", "second", "third"]);
    }

    #[test]
    fn name_sort_ignores_case() {
        let tasks = vec![named("b", None, "beta"), named("a", None, "Alpha")];
        assert_eq!(sorted(&tasks, Sort::Name, false), ["a", "b"]);
        assert_eq!(sorted(&tasks, Sort::Name, true), ["b", "a"]);
    }

    #[test]
    fn sorting_applies_at_every_level_not_just_the_roots() {
        let tasks = vec![
            task("root", None),
            Task { name: "zulu".into(), ..task("z", Some("root")) },
            Task { name: "alpha".into(), ..task("a", Some("root")) },
        ];
        let roots = super::build(&tasks, "", Filter::All, Sort::Name, false);
        assert_eq!(ids(&roots[0].children), ["a", "z"]);
    }

    #[test]
    fn tasks_without_a_timestamp_sort_last_in_both_directions() {
        // An absent date is unknown, not "oldest"; floating it to the top under
        // one direction would be actively misleading.
        let mut missing = task("missing", None);
        missing.updated_at = None;
        let tasks = vec![
            missing,
            stamped("has", "2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"),
        ];

        assert_eq!(sorted(&tasks, Sort::Updated, false), ["has", "missing"]);
        assert_eq!(sorted(&tasks, Sort::Updated, true).last().unwrap(), "has");
    }

    #[test]
    fn ties_break_on_name_so_order_does_not_jitter_between_refreshes() {
        let tasks = vec![
            stamped("b", "2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"),
            stamped("a", "2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"),
        ];
        // Identical timestamps: name decides, and does so consistently.
        assert_eq!(sorted(&tasks, Sort::Updated, false), ["a", "b"]);
    }

    #[test]
    fn the_sort_cycle_returns_to_where_it_started() {
        let mut s = Sort::Priority;
        for _ in 0..4 {
            s = s.next();
        }
        assert_eq!(s, Sort::Priority);
    }

    #[test]
    fn every_sort_and_direction_has_a_distinct_label() {
        let mut labels = Vec::new();
        let mut s = Sort::Priority;
        for _ in 0..4 {
            labels.push(s.label(false));
            labels.push(s.label(true));
            s = s.next();
        }
        let count = labels.len();
        labels.sort_unstable();
        labels.dedup();
        assert_eq!(labels.len(), count, "two orders render the same label");
    }
}