beady-eye 0.13.0

A tree of work in flight: bead graphs annotated with the live agents working them
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
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
//! What a row is drawn in: which of the palette's slots a bead's status and
//! the liveness of the row it sits on choose. The values are the palette's;
//! what is here is the rule that picks between them.

use ratatui::style::Style;

use crate::model::types::Status;
use crate::view::palette;
use crate::view::row::Row;

/// How live a row is, which is the one thing about a bead `bd list` has no
/// way to know — and so the one this scale is spent on.
///
/// | row | drawn |
/// |---|---|
/// | an agent is on it | the ground, and a weight |
/// | nobody on it, still going | the ground: the terminal's own foreground |
/// | finished, nobody on it | one rung under it, at the theme's colour 8 |
///
/// The ordinary row is the ground, and the scale steps up from it and down.
/// Colour buys one of those steps and no more: there is no slot a theme
/// reliably sets between its foreground and its colour 8, and none below
/// colour 8 that a reader could still make out, so a third tone would be a
/// third value the theme picked without reference to the other two. The step
/// up is a weight for that reason and not for emphasis.
///
/// Finished means what it means to `lines::split`: closed, no agent, no
/// anomaly. A closed bead whose pane is still alive is exactly the row worth
/// looking at, and dimming it is how it would be missed.
pub(super) fn tone(row: &Row) -> Style {
    let finished = row.status.is_closed() && row.agent.is_none() && row.anomalies.is_empty();
    if row.agent.is_some() {
        palette::TIER_STAFFED
    } else if finished {
        palette::TIER_FINISHED
    } else {
        palette::TIER_OPEN
    }
}

/// What a bead's status is drawn in: `bd`'s own colour for it, or nothing
/// where `bd` sends no escape and the glyph should take the brightness of the
/// row it sits on.
///
/// Colour is the second channel and never the only one: the glyph already says
/// the status, so a terminal with no colour loses nothing.
pub(crate) fn status_style(status: &Status) -> Style {
    match status {
        Status::InProgress => palette::STATUS_IN_PROGRESS,
        Status::Blocked => palette::STATUS_BLOCKED,
        Status::Closed => palette::STATUS_CLOSED,
        Status::Deferred => palette::STATUS_DEFERRED,
        Status::Open => palette::STATUS_OPEN,
        // The one status `bd` has no colour for, because it has no such
        // status. It takes the colour of the note already beside it.
        Status::Other(_) => palette::ATTENTION,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use ratatui::style::{Color, Modifier};

    use crate::model::anomaly::Anomaly;
    use crate::view::draw::bead::{bead_line, elided_run};
    use crate::view::draw::project::project_line;
    use crate::view::draw::tests::*;
    use crate::view::painted::Run;
    use crate::view::row::{self, Layout, Row, AGENT, WARNING};

    // ---- styling ---------------------------------------------------------

    /// One of each status, so a loop over them covers the set. The compiler
    /// holds `status_style` total; this list is only what a test walks.
    fn every_status() -> [Status; 6] {
        [
            Status::InProgress,
            Status::Blocked,
            Status::Open,
            Status::Deferred,
            Status::Closed,
            Status::Other(String::new()),
        ]
    }

    /// Colour is the second channel and never the only one: the glyph already
    /// says the status, so a terminal that drops colour must lose nothing.
    #[test]
    fn no_status_is_told_apart_by_colour_alone() {
        for status in every_status() {
            let node = node("smt-4kd3p.1", "a bead", status.clone());
            let drawn = Painted::of(
                bead_line(&row(&node), BRANCH, &ids(3), &Layout::default()),
                40,
                1,
            )
            .rows();

            assert!(
                drawn[0].contains(row::status_glyph(&status)),
                "{status:?} lost its glyph: {drawn:?}"
            );
        }
    }

    /// Two statuses sharing a colour would tell one story between them. Only
    /// `open` may arrive without one at all: `bd` sends no escape for it, and
    /// the glyph already says which status it is.
    #[test]
    fn no_colour_is_given_to_two_statuses_and_only_open_goes_without_one() {
        let coloured: Vec<Color> = every_status()
            .iter()
            .filter_map(|status| status_style(status).fg)
            .collect();

        for (nth, colour) in coloured.iter().enumerate() {
            assert!(
                !coloured[nth + 1..].contains(colour),
                "{colour:?} is drawn for two statuses"
            );
        }
        assert_eq!(
            coloured.len(),
            every_status().len() - 1,
            "one status goes without a colour and it is open"
        );
        assert_eq!(status_style(&Status::Open).fg, None);
    }

    // ---- bd's palette, and the brightness only bdi can draw ---------------

    /// Read off `bd` 1.2.2's own output. A reader coming from `bd list` has
    /// already learned these, and a status drawn in a colour `bd` gives to a
    /// different one would be worse than no colour at all.
    #[test]
    fn a_status_glyph_is_painted_the_colour_bd_paints_it() {
        let bds = [
            (Status::InProgress, Color::Rgb(255, 180, 84)),
            (Status::Blocked, Color::Rgb(242, 109, 120)),
            (Status::Closed, Color::Rgb(128, 144, 160)),
            (Status::Deferred, Color::Rgb(108, 118, 128)),
        ];

        for (status, colour) in bds {
            let bead = node("smt-4kd3p.1", "a bead", status.clone());
            let painted = Painted::of(
                bead_line(&row(&bead), BRANCH, &ids(3), &Layout::default()),
                60,
                1,
            )
            .row(0);

            assert!(
                painted[1].said.starts_with(row::status_glyph(&status)),
                "{status:?}: {painted:?}"
            );
            assert_eq!(painted[1].style.fg, Some(colour), "{status:?}: {painted:?}");
        }
    }

    /// `bd` sends no escape at all for an open bead's glyph, and inheriting is
    /// what lets the row's own treatment reach it. A glyph pinned to the
    /// terminal's default would leave a staffed row reading as two weights.
    ///
    /// Asked of a staffed row because the ground is also what the box-drawing
    /// beside it holds: on an ordinary row a glyph that inherits and a glyph
    /// pinned to the default are the same bytes, and the assertion would pass
    /// whichever it was.
    #[test]
    fn an_open_glyph_takes_the_treatment_of_the_row_it_sits_on() {
        let mut staffed = node("smt-4kd3p.1", "a bead", Status::Open);
        staffed.agent = Some(a_pane());

        let painted = Painted::of(
            bead_line(&row(&staffed), BRANCH, &ids(3), &Layout::default()),
            90,
            1,
        )
        .row(0);

        assert!(painted[1].said.starts_with(''), "{painted:?}");
        assert_eq!(
            painted[1].style.add_modifier,
            Modifier::BOLD,
            "the glyph took the row's weight: {painted:?}"
        );
        assert_eq!(
            painted[0].style.add_modifier,
            Modifier::empty(),
            "and the box-drawing beside it did not: {painted:?}"
        );
    }

    /// The tier that earns the screen. `bd list` has no notion of a live
    /// agent, so it has no way to say which row is the one you came for.
    ///
    /// It is a weight rather than a colour because there is no colour above
    /// the ground to reach for: a theme's own foreground is routinely the
    /// same value as its colour 7 and its colour 15, so a staffed row painted
    /// either of those is a staffed row painted like every other. A weight
    /// steps off the colour axis altogether and cannot land on it.
    #[test]
    fn a_row_with_an_agent_on_it_is_the_ground_and_a_weight_and_one_without_is_the_ground() {
        let mut staffed = node("smt-4kd3p.1", "a bead", Status::Open);
        staffed.agent = Some(a_pane());

        let bright = Painted::of(
            bead_line(&row(&staffed), BRANCH, &ids(3), &Layout::default()),
            90,
            1,
        )
        .row(0);
        let plain = Painted::of(
            bead_line(
                &row(&node("smt-4kd3p.1", "a bead", Status::Open)),
                BRANCH,
                &ids(3),
                &Layout::default(),
            ),
            90,
            1,
        )
        .row(0);

        assert_eq!(
            the_words(&bright).style.fg,
            Some(Color::Reset),
            "an agent on it, so the row is the terminal's own: {bright:?}"
        );
        assert_eq!(
            the_words(&bright).style.add_modifier,
            Modifier::BOLD,
            "and a weight is what puts it above the ground: {bright:?}"
        );
        assert_eq!(
            the_words(&plain).style.fg,
            Some(Color::Reset),
            "nobody on it, so the row is the ground itself: {plain:?}"
        );
        assert_eq!(
            the_words(&plain).style.add_modifier,
            Modifier::empty(),
            "untreated, which is what makes it the ground: {plain:?}"
        );
    }

    /// The run a row's own words are drawn in. Found by what it says rather
    /// than where it falls, because a staffed row's box-drawing shares its
    /// style and merges into it while an unworked row's stands apart.
    fn the_words(painted: &[Run]) -> &Run {
        painted
            .iter()
            .find(|run| run.said.contains("a bead"))
            .expect("the title is drawn")
    }

    /// What `bd` already does to a closed row, arrived at from the other
    /// side: a finished branch nobody is on falls back into the page. It goes
    /// to the theme's colour 8 rather than to a grey of `bdi`'s own, because
    /// the distance a rung keeps from the ground is only fixed where the
    /// theme chooses both.
    #[test]
    fn a_finished_row_nobody_is_on_drops_to_the_one_rung_under_the_ground() {
        let painted = Painted::of(
            bead_line(
                &row(&node("smt-4kd3p.1", "a bead", Status::Closed)),
                BRANCH,
                &ids(3),
                &Layout::default(),
            ),
            60,
            1,
        )
        .row(0);

        assert_eq!(
            painted[1].style.fg,
            Some(Color::Rgb(128, 144, 160)),
            "the glyph keeps its own status colour: {painted:?}"
        );
        assert_eq!(painted[2].style.fg, Some(Color::DarkGray), "{painted:?}");
    }

    /// Exactly the row worth looking at, and dimming it is how it would be
    /// missed. `lines::split` leaves it out of a run for the same reason.
    #[test]
    fn a_closed_bead_whose_pane_is_still_alive_is_not_dimmed() {
        let mut alive = node("smt-4kd3p.1", "a bead", Status::Closed);
        alive.agent = Some(a_pane());
        alive.anomalies = vec![Anomaly::StalePane];

        let painted = Painted::of(
            bead_line(&row(&alive), BRANCH, &ids(3), &Layout::default()),
            110,
            1,
        )
        .row(0);

        let words = painted
            .iter()
            .find(|run| run.said.contains("a bead"))
            .expect("the row says its title");
        assert_eq!(words.style.fg, Some(Color::Reset), "{painted:?}");
        assert_eq!(words.style.add_modifier, Modifier::BOLD, "{painted:?}");
    }

    /// Finished means what it means in `lines::split` — closed, no agent, no
    /// anomaly — so an anomaly alone is enough to keep a row out of the dim.
    /// Nobody is on it, so it sits on the ground rather than above it.
    #[test]
    fn a_closed_bead_with_an_anomaly_against_it_is_not_dimmed() {
        let mut odd = node("smt-4kd3p.1", "a bead", Status::Closed);
        odd.anomalies = vec![Anomaly::StalePane];

        let painted = Painted::of(
            bead_line(&row(&odd), BRANCH, &ids(3), &Layout::default()),
            110,
            1,
        )
        .row(0);

        assert_eq!(painted[2].style.fg, Some(Color::Reset), "{painted:?}");
        assert_eq!(
            painted[2].style.add_modifier,
            Modifier::empty(),
            "{painted:?}"
        );
    }

    // ---- what holds the scale apart --------------------------------------

    /// The whole scale, so a claim about the tiers together is made over all
    /// of them rather than over the two whoever wrote it had in mind.
    fn every_tier() -> [(&'static str, Style); 3] {
        [
            ("staffed", palette::TIER_STAFFED),
            ("open", palette::TIER_OPEN),
            ("finished", palette::TIER_FINISHED),
        ]
    }

    /// Every other assertion about the scale pins one tier to one value, and
    /// a value nobody compares is one a theme can quietly close the distance
    /// to. These two say what has to hold of the tiers *together*, which is
    /// the part no assertion about a single value reaches.
    ///
    /// This one: a tier `bdi` resolves itself keeps a distance from its
    /// neighbours that is a property of this code rather than of the reader's
    /// terminal — and a resolved tier and a deferred one cannot be held any
    /// distance apart at all, because only one of them moves when the theme
    /// does.
    #[test]
    fn no_tier_is_a_value_the_readers_theme_cannot_move() {
        for (tier, drawn) in every_tier() {
            assert!(
                !matches!(drawn.fg, Some(Color::Rgb(..)) | Some(Color::Indexed(..))),
                "the {tier} tier is pinned to {:?}",
                drawn.fg
            );
        }
    }

    /// And this one: colour buys the scale one interval and no more, so a
    /// third tone would be a third value the theme picked without reference to
    /// the other two. No theme reliably sets a slot between its foreground and
    /// its colour 8, and none under colour 8 that a reader could still make
    /// out — so the tier that is not a tone is a weight, which is a step off
    /// the colour axis and cannot land back on it however a theme is written.
    #[test]
    fn the_scale_spends_colour_once_and_carries_its_other_tier_on_a_weight() {
        assert_eq!(
            palette::TIER_OPEN.add_modifier,
            Modifier::empty(),
            "the ordinary row is untreated, which is what makes it the ground"
        );
        assert_eq!(
            palette::TIER_STAFFED.fg,
            palette::TIER_OPEN.fg,
            "the two tiers still going stand on that ground"
        );
        assert_ne!(
            palette::TIER_STAFFED.add_modifier,
            palette::TIER_OPEN.add_modifier,
            "and a weight is the whole of what tells them apart"
        );
        assert_ne!(
            palette::TIER_OPEN.fg,
            palette::TIER_FINISHED.fg,
            "leaving one colour interval, the one under the ground"
        );
    }

    // ---- what the scale may not be the only carrier of --------------------

    /// Every row `tone` can tell one from another, holding still everything it
    /// does not read. It reads three things — whether an agent is on the row,
    /// whether the bead is closed, whether anything is wrong with it — so the
    /// corpus is those three over each status, under one id and one title.
    fn every_liveness_row() -> Vec<Row> {
        let mut rows = Vec::new();
        for status in every_status() {
            for staffed in [false, true] {
                for odd in [false, true] {
                    let mut bead = node("smt-4kd3p.1", "a bead", status.clone());
                    bead.agent = staffed.then(a_pane);
                    if odd {
                        bead.anomalies = vec![Anomaly::StaleClaim { days: 58 }];
                    }
                    rows.push(row(&bead));
                }
            }
        }
        rows
    }

    /// A floor on the harm and not a detector of the fault. It says a reader
    /// can still tell the liveness states apart once the tones have run
    /// together; it does not say they have not run together, and nothing this
    /// project runs does. Neither `bdi-sw4` nor `bdi-kbd2` would have gone red
    /// here — `◍` and `✓` were on those rows the whole time while the tones
    /// collapsed — so a green board here is no evidence the scale is
    /// separated, and a reader who wants that has to go on looking for it.
    ///
    /// The claim is that a row's tier is a function of its words: two rows
    /// that read the same are drawn the same, so no rung carries a meaning by
    /// itself. Nothing here names a colour, which is what lets the scale move
    /// underneath it.
    #[test]
    fn no_liveness_state_is_told_apart_by_its_tone_alone() {
        let mut read: Vec<(String, Style)> = Vec::new();

        for row in every_liveness_row() {
            let tone = tone(&row);
            let words = Painted::of(bead_line(&row, BRANCH, &ids(3), &Layout::default()), 200, 1)
                .rows()
                .swap_remove(0);

            if let Some((_, already)) = read.iter().find(|(said, _)| *said == words) {
                assert_eq!(
                    *already, tone,
                    "two liveness states read the same: {words:?}"
                );
            }
            read.push((words, tone));
        }

        // The one way this goes quietly vacuous is a corpus that stopped
        // reaching the states it means to. Said of the corpus rather than of
        // the tones it drew: a count of the distinct tones would be this test
        // asserting the rungs are apart, which is the claim `visual-language`
        // establishes cannot be made.
        assert_eq!(
            read.len(),
            every_status().len() * 4,
            "every status, against both of the other two things `tone` reads"
        );
    }

    /// The box-drawing says how the tree is shaped, not how a bead is going,
    /// so it is held on the ground while the row around it steps off it in
    /// either direction. `bd list` leaves its own tree prefix undimmed on a
    /// closed row too.
    #[test]
    fn the_box_drawing_a_row_hangs_under_never_takes_the_rows_tier() {
        let mut staffed = node("smt-4kd3p.1", "a bead", Status::Open);
        staffed.agent = Some(a_pane());
        let unworked = node("smt-4kd3p.1", "a bead", Status::Open);
        let finished = node("smt-4kd3p.1", "a bead", Status::Closed);

        for bead in [staffed, unworked, finished] {
            let painted = Painted::of(
                bead_line(&row(&bead), BRANCH, &ids(3), &Layout::default()),
                90,
                1,
            )
            .row(0);

            assert!(painted[0].said.starts_with(BRANCH), "{painted:?}");
            assert_eq!(painted[0].style.fg, Some(Color::Reset), "{painted:?}");
            assert_eq!(
                painted[0].style.add_modifier,
                Modifier::empty(),
                "{painted:?}"
            );
        }
    }

    /// A run stands for finished rows and is drawn as one of them, so the two
    /// cannot fall out of step and the palette holds one grey, not two.
    #[test]
    fn an_elided_run_is_dimmed_the_same_grey_a_finished_row_is() {
        let run = Painted::of(elided_run(BRANCH, 4), 60, 1).row(0);
        let finished = Painted::of(
            bead_line(
                &row(&node("smt-4kd3p.1", "a bead", Status::Closed)),
                BRANCH,
                &ids(3),
                &Layout::default(),
            ),
            60,
            1,
        )
        .row(0);

        assert_eq!(run[0].said, BRANCH, "{run:?}");
        assert_eq!(run[0].style.fg, Some(Color::Reset), "{run:?}");
        assert_eq!(run[1].style.fg, finished[1].style.fg, "the glyph: {run:?}");
        assert_eq!(
            run[2].style.fg, finished[2].style.fg,
            "what follows it: {run:?}"
        );
    }

    /// A project line's counts are its whole project's and not any one bead's,
    /// so the rule that decides a row's tier cannot be asked of it without
    /// quietly changing what it means. It stays off the scale. A root does
    /// not: it is a bead row, and the rule is asked of it like any other.
    #[test]
    fn a_project_line_is_left_off_the_scale_a_bead_row_is_on() {
        let quiet = project("meadow", counts(7, 7, 0, 0));

        let painted = Painted::of(project_line(&quiet, OPEN, None, drawn_at()), 60, 1).row(0);

        assert!(
            painted.iter().all(|run| run.style.fg == Some(Color::Reset)),
            "{painted:?}"
        );
    }

    /// `bd`'s hues belong to `bd`'s concepts. The live agent and the anomaly
    /// are the two things it cannot say, so they are a different axis and
    /// keep a different colour system whatever the row around them does.
    #[test]
    fn the_cells_bd_cannot_draw_keep_their_own_colours_however_bright_the_row() {
        let mut staffed = node("smt-4kd3p.1", "a bead", Status::InProgress);
        staffed.agent = Some(a_pane());
        staffed.anomalies = vec![Anomaly::StaleClaim { days: 58 }];

        let painted = Painted::of(
            bead_line(&row(&staffed), BRANCH, &ids(3), &Layout::default()),
            120,
            1,
        )
        .row(0);

        assert!(
            painted
                .iter()
                .any(|run| run.said.contains(AGENT) && run.style.fg == palette::AGENT.fg),
            "{painted:?}"
        );
        assert!(
            painted
                .iter()
                .any(|run| run.said.contains(WARNING) && run.style.fg == palette::ATTENTION.fg),
            "{painted:?}"
        );
    }

    /// The one status `bd` has no colour for, because it has no such status.
    /// It takes the colour of the note already beside it on the row.
    #[test]
    fn a_status_bd_never_had_is_painted_the_colour_of_the_note_beside_it() {
        let odd = node("smt-4kd3p.1", "a bead", Status::Other("triage".into()));

        let painted = Painted::of(
            bead_line(&row(&odd), BRANCH, &ids(3), &Layout::default()),
            120,
            1,
        )
        .row(0);

        assert!(painted[1].said.starts_with('?'), "{painted:?}");
        assert_eq!(painted[1].style.fg, palette::ATTENTION.fg, "{painted:?}");
    }
}