shep 0.24.0

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
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
//! The env rows, and the pending section above them.
//!
//! A value never reaches the screen. A key is listed and its value renders
//! as `<set>` once it is stored or filed, so a shoulder looking at the
//! dashboard learns which keys exist and nothing else. While the shared
//! key/value editor is open on a row, its two fields draw side by side in
//! those same two columns, and only the field holding the cursor draws a
//! cursor. The value draws as one `█` per character until `Ctrl-r`.

use ratatui::style::Style;
use ratatui::text::{Line, Span};

use super::super::super::keyvalue::{Field, KeyValues};
use super::super::super::pane::{ConfigPane, PaneRow};
use super::super::super::textbox::TextBox;
use super::super::super::theme::Palette;
use super::super::flock::{fit, mark};
use super::chrome::pending_edit_line;
use super::field_row::section_header;
use super::layout::{body_width, widths};

/// The pending-edits and env sections that follow the active group's own
/// fields: every filed edit regardless of which group it belongs to, then the
/// sheep's own env key names and a hint to add one.
///
/// Appended rather than scrolled: both are short by construction, an operator
/// files a handful of edits and a Flockfile carries a handful of env keys, so
/// neither earns the `... N below` machinery the field list's 41 rows need.
pub(super) fn pending_and_env_lines(
    pane: &ConfigPane,
    palette: Palette,
    width: u16,
    budget: usize,
    show_lands: bool,
) -> Vec<Line<'static>> {
    let mut lines: Vec<Line<'static>> = Vec::new();
    if budget == 0 {
        return lines;
    }
    // The cursor's own row has to draw somewhere in this budget, the same rule
    // `body_from`/`cursor_only` hold for a field. Truncating from the bottom
    // would cut that row with every other row of the section, so at the floor
    // this draws it alone and nothing else.
    let pending_len = if pane.edits().is_empty() {
        0
    } else {
        2 + pane.edits().len()
    };
    let shows_env = pane.shows_env();
    let env_len = if shows_env {
        2 + pane.env_key_names().len() + 1
    } else {
        0
    };
    if pending_len + env_len > budget
        && let Some(PaneRow::Env(_) | PaneRow::AddEnv) = pane.cursor()
    {
        return vec![cursor_env_row_line(pane, width, palette, show_lands)];
    }
    let mut remaining = budget;

    if !pane.edits().is_empty() && remaining > 0 {
        lines.push(Line::default());
        remaining -= 1;
        if remaining > 0 {
            lines.push(section_header("pending edits", palette));
            remaining -= 1;
        }
        for (_, entry) in pane.edits().iter() {
            if remaining == 0 {
                break;
            }
            lines.push(pending_edit_line(pane, entry.edit(), width, palette));
            remaining -= 1;
        }
    }

    if !shows_env {
        return lines;
    }
    if remaining > 0 {
        lines.push(Line::default());
        remaining -= 1;
    }
    if remaining > 0 {
        lines.push(section_header("env", palette));
        remaining -= 1;
    }
    for (index, name) in pane.env_key_names().iter().enumerate() {
        if remaining == 0 {
            break;
        }
        let selected = pane.cursor() == Some(PaneRow::Env(index));
        lines.push(env_row_line(
            pane, name, selected, width, palette, show_lands,
        ));
        remaining -= 1;
    }
    if remaining > 0 {
        let selected = pane.cursor() == Some(PaneRow::AddEnv);
        lines.push(add_env_row_line(pane, selected, width, palette, show_lands));
    }
    lines
}

/// The cursor's own row, alone: [`pending_and_env_lines`]'s floor fallback,
/// [`super::body::cursor_only`]'s twin for the env rows below the field list.
/// Panics if the cursor is not on [`PaneRow::Env`] or [`PaneRow::AddEnv`];
/// every caller has already checked.
pub(super) fn cursor_env_row_line(
    pane: &ConfigPane,
    width: u16,
    palette: Palette,
    show_lands: bool,
) -> Line<'static> {
    match pane.cursor() {
        Some(PaneRow::Env(index)) => {
            let name = pane
                .env_key_names()
                .get(index)
                .expect("the cursor names a real env row");
            env_row_line(pane, name, true, width, palette, show_lands)
        }
        Some(PaneRow::AddEnv) => add_env_row_line(pane, true, width, palette, show_lands),
        Some(
            PaneRow::Field(_)
            | PaneRow::Table(_)
            | PaneRow::Entry(_)
            | PaneRow::Choice { .. }
            | PaneRow::Arm { .. }
            | PaneRow::Dog(_)
            | PaneRow::Under { .. },
        )
        | None => {
            unreachable!("callers check the cursor is on an env row first")
        }
    }
}

/// The editor open on this row, if it is open on this row.
///
/// `under` is the key the row was opened under, so an editor opened on
/// `DB_HOST` keeps drawing on `DB_HOST` while its KEY field is being renamed
/// to something else: the row the operator chose does not move out from under
/// them halfway through naming it.
fn editor_on<'a>(pane: &'a ConfigPane, under: &str) -> Option<&'a KeyValues> {
    // The borrow is `pane`'s, not `under`'s: `under` is only compared.
    pane.env_typing()
        .filter(|editor| editor.opened_under() == Some(under))
}

/// One field of the open editor, drawn for a cell `width` wide.
///
/// The cursor is drawn only in the field that holds it, which is how the row
/// says which of the two the operator is typing into: the pane has no box, no
/// highlight and no colour to spend on the difference, and the ANSI gallery
/// renders foregrounds only. An env value is a secret being typed, so
/// `secret` draws it as blocks until the operator reveals it.
fn field_cell(buffer: &TextBox, focused: bool, secret: bool, width: u16) -> String {
    if focused {
        buffer.drawn_for(secret, width.saturating_sub(1))
    } else {
        buffer.shown(secret)
    }
}

/// One env key's own row: the selection mark, the key, and `(set)`.
///
/// Every key reads `(set)`, never its own value: `SheepConfigView::new` clears
/// `config.env` before this pane ever sees it, so no value for any key reaches
/// here, and this is the one word every row can honestly draw. With the editor
/// open the two columns hold its KEY and VALUE fields instead.
pub(super) fn env_row_line(
    pane: &ConfigPane,
    name: &str,
    selected: bool,
    width: u16,
    palette: Palette,
    show_lands: bool,
) -> Line<'static> {
    let (key_w, value_w, _) = widths(body_width(width), show_lands);
    let editor = selected.then(|| editor_on(pane, name)).flatten();
    let (key_cell, value_cell) = match editor {
        None => (name.to_owned(), "(set)".to_owned()),
        Some(editor) => (
            field_cell(editor.key(), editor.focus() == Field::Key, false, key_w),
            field_cell(
                editor.value(),
                editor.focus() == Field::Value,
                true,
                value_w,
            ),
        ),
    };
    let ground = if selected {
        palette.ground()
    } else {
        Style::default()
    };
    let mark_style = if selected {
        palette.attention().patch(ground)
    } else {
        Style::default()
    };
    let mark_span = Span::styled(mark(selected), mark_style);
    // A single blank standing in for `field_line`'s lock glyph: `GUTTER`
    // already spent that column out of `body_width`. The same holds for
    // `field_line`'s flag reservation inside the key cell, or this row's
    // key text starts one column left of every field's.
    let mut text = String::from(" ");
    text.push_str(&fit(&format!(" {key_cell}"), key_w));
    if value_w > 0 {
        text.push_str("  ");
        text.push_str(&fit(&value_cell, value_w));
    }
    Line::from(vec![mark_span, Span::styled(text, ground)])
}

/// The `+ add a key` row: the selection mark, and either the caption or, while
/// the editor is open, its KEY field in the key column and its VALUE field in
/// the value column.
pub(super) fn add_env_row_line(
    pane: &ConfigPane,
    selected: bool,
    width: u16,
    palette: Palette,
    show_lands: bool,
) -> Line<'static> {
    let (key_w, value_w, _) = widths(body_width(width), show_lands);
    let editor = selected.then(|| pane.env_typing()).flatten();
    let ground = if selected {
        palette.ground()
    } else {
        Style::default()
    };
    let mark_style = if selected {
        palette.attention().patch(ground)
    } else {
        Style::default()
    };
    let mark_span = Span::styled(mark(selected), mark_style);
    let (caption, muted, value) = match editor {
        None => ("+ add a key".to_owned(), true, String::new()),
        Some(editor) => (
            field_cell(editor.key(), editor.focus() == Field::Key, false, key_w),
            false,
            field_cell(
                editor.value(),
                editor.focus() == Field::Value,
                true,
                value_w,
            ),
        ),
    };
    // Same key-cell reservation as [`env_row_line`]: a leading space stands in
    // for `field_line`'s flag character so `+ add a key` and every field above
    // it start their text in the same column.
    let mut text = String::from(" ");
    text.push_str(&fit(&format!(" {caption}"), key_w));
    if value_w > 0 {
        text.push_str("  ");
        text.push_str(&fit(&value, value_w));
    }
    let style = if muted {
        palette.muted()
    } else {
        Style::default()
    };
    Line::from(vec![mark_span, Span::styled(text, style.patch(ground))])
}

#[cfg(test)]
mod tests {
    use super::super::fixtures::{text_of, web_pane};
    use super::super::pane_lines;
    use super::*;
    use crate::lookout::app::KeyPress;
    use crate::lookout::view::MIN_TERM_WIDTH;
    use crate::lookout::view::fixtures;
    use crate::output::width::visible_width;

    /// Types `text` into whichever field of the pane's open editor holds the
    /// cursor.
    fn type_into(pane: &mut ConfigPane, text: &str) {
        for typed in text.chars() {
            pane.type_env_text(KeyPress::TextChar(typed));
        }
    }

    /// The folded env rows at a comfortable width: both of the fixture's keys
    /// are listed with `(set)` rather than a value, and `+ add a key` is last.
    ///
    /// Asserts on the env rows alone via
    /// [`fixtures::config_pane_env_rows_for_tests`], not on the whole rendered
    /// frame: a search over the joined frame can match any row, field or env,
    /// once folding put both in the same screen.
    #[test]
    fn the_env_rows_draw_at_a_comfortable_width() {
        let pane = web_pane();
        let rows = fixtures::config_pane_env_rows_for_tests(&pane);
        assert!(rows.iter().any(|row| row.contains("DB_HOST")), "{rows:?}");
        assert!(rows.iter().any(|row| row.contains("LOG_LEVEL")), "{rows:?}");
        assert!(
            rows.iter()
                .all(|row| row.contains("(set)") || row.contains("+ add a key")),
            "{rows:?}"
        );
        assert!(
            rows.iter().any(|row| row.contains("+ add a key")),
            "{rows:?}"
        );
    }

    /// A filed env write draws nothing at all on this screen, and above all not
    /// the value that was typed into it.
    #[test]
    fn a_filed_env_write_never_reaches_the_screen() {
        let mut pane = web_pane();
        pane.move_to_last();
        assert_eq!(pane.cursor(), Some(PaneRow::AddEnv));
        pane.begin_env_typing();
        type_into(&mut pane, "NEW_KEY");
        pane.step_env_field(false);
        type_into(&mut pane, "hunter2");
        pane.submit_env_typing();
        let text = text_of(&pane_lines(&pane, fixtures::plain(), 120, 0));
        assert!(!text.join("\n").contains("hunter2"), "{text:?}");
    }

    /// The shepherd sends keys with no values at all, so a rendered value could
    /// only have been invented.
    ///
    /// Asserts on the env rows alone via
    /// [`fixtures::config_pane_env_rows_for_tests`]: a frame-wide search can
    /// match any row, field or env, once folding put both in the same screen.
    #[test]
    fn every_env_value_renders_as_set_and_never_as_itself() {
        let pane = ConfigPane::sheep({
            let mut config = shep_core::config::AppConfig {
                name: "web".to_string(),
                ..Default::default()
            };
            config
                .env
                .insert("DB_PASSWORD".to_string(), "hunter2".to_string());
            shep_core::protocol::SheepConfigView::new(config, Vec::new(), Vec::new())
        });
        let rows = fixtures::config_pane_env_rows_for_tests(&pane);
        assert!(
            rows.iter().any(|row| row.contains("DB_PASSWORD")),
            "{rows:?}"
        );
        assert!(
            rows.iter().all(|row| !row.contains("hunter2")),
            "an env value reached the pane: {rows:?}"
        );
        assert!(rows.iter().any(|row| row.contains("(set)")), "{rows:?}");
    }

    #[test]
    fn every_env_line_fits_the_width_it_was_drawn_for() {
        let mut pane = web_pane();
        pane.move_to_last();
        assert_eq!(pane.cursor(), Some(PaneRow::AddEnv));
        pane.begin_env_typing();
        type_into(&mut pane, "A_VERY_LONG_ENV_KEY_NAME");
        pane.step_env_field(false);
        type_into(&mut pane, "and a value longer still");
        for width in MIN_TERM_WIDTH..=200 {
            for line in text_of(&pane_lines(&pane, fixtures::plain(), width, 0)) {
                assert!(
                    visible_width(&line) <= usize::from(width),
                    "width {width} drew {}: {line:?}",
                    visible_width(&line)
                );
            }
        }
    }

    /// The pane's own rows as text, at a width where the whole list fits.
    fn rows_of(pane: &ConfigPane) -> Vec<String> {
        text_of(&pane_lines(pane, fixtures::plain(), 160, 0))
    }

    /// The row `+ add a key` is drawn on, found while it still says so.
    /// [`fixtures::config_pane_env_rows_for_tests`] finds the add row by its
    /// caption, so it cannot name one busy being typed.
    ///
    /// # Panics
    ///
    /// If the pane draws no `+ add a key` row.
    #[track_caller]
    fn add_row_at(pane: &ConfigPane) -> usize {
        rows_of(pane)
            .iter()
            .position(|row| row.contains("add a key"))
            .expect("the pane draws a + add a key row")
    }

    /// The two fields draw side by side, in the same two columns a stored row
    /// uses for its key and its `(set)`, and the cursor is on one of them.
    #[test]
    fn the_open_editor_draws_key_and_value_side_by_side_on_the_add_row() {
        let mut pane = web_pane();
        pane.move_to_last();
        assert_eq!(pane.cursor(), Some(PaneRow::AddEnv));
        let at = add_row_at(&pane);

        pane.begin_env_typing();
        type_into(&mut pane, "NEW_KEY");
        let rows = rows_of(&pane);
        let row = &rows[at];
        assert!(row.contains("NEW_KEY"), "KEY is drawn: {row:?}");
        assert!(row.contains('\u{258f}'), "the cursor is on KEY: {row:?}");
        assert!(
            !row.contains("add a key"),
            "the caption gives way to what is being typed: {row:?}"
        );

        pane.step_env_field(false);
        type_into(&mut pane, "hunter2");
        let rows = rows_of(&pane);
        let row = &rows[at];
        assert!(row.contains("NEW_KEY"), "KEY is still drawn: {row:?}");
        let blocks = "\u{2588}".repeat(7);
        assert!(!row.contains("hunter2"), "VALUE leaked: {row:?}");
        assert!(row.contains(&blocks), "VALUE draws beside it: {row:?}");
        assert!(
            row.find(&blocks).expect("value") > row.find("NEW_KEY").expect("key"),
            "VALUE draws in its own column, after the key column: {row:?}"
        );
        assert!(
            row.find('\u{258f}').expect("cursor") > row.find(&blocks).expect("value"),
            "and the cursor moved with the field: {row:?}"
        );
    }

    #[test]
    fn an_env_value_being_typed_is_masked() {
        let mut pane = web_pane();
        pane.move_to_last();
        let at = add_row_at(&pane);
        pane.begin_env_typing();
        type_into(&mut pane, "NEW_KEY");
        pane.step_env_field(false);
        type_into(&mut pane, "hunter2");
        let row = |pane: &ConfigPane| rows_of(pane)[at].clone();
        assert!(!row(&pane).contains("hunter2"), "{:?}", row(&pane));
        assert!(row(&pane).contains("NEW_KEY"), "the key is not a secret");

        // Back on KEY, the unfocused value is still masked.
        pane.step_env_field(true);
        assert!(!row(&pane).contains("hunter2"), "{:?}", row(&pane));
        assert!(
            row(&pane).contains(&"\u{2588}".repeat(7)),
            "{:?}",
            row(&pane)
        );

        pane.step_env_field(false);
        pane.type_env_text(KeyPress::TextEdit(
            crate::lookout::textbox::TextEdit::Reveal,
        ));
        assert!(row(&pane).contains("hunter2\u{258f}"), "{:?}", row(&pane));
    }

    /// A row the editor is open on keeps drawing where it is while the KEY
    /// field is being renamed, so the row the operator chose does not move out
    /// from under them halfway through naming it.
    #[test]
    fn a_rename_keeps_drawing_the_row_it_was_opened_on() {
        let mut pane = web_pane();
        pane.move_to_last();
        pane.move_by(-1);
        assert_eq!(pane.cursor(), Some(PaneRow::Env(1)));
        let before = rows_of(&pane);
        let at = before
            .iter()
            .position(|row| row.contains("LOG_LEVEL"))
            .expect("LOG_LEVEL is drawn on its own row");

        pane.begin_env_typing();
        pane.step_env_field(true);
        pane.type_env_text(KeyPress::TextEdit(
            crate::lookout::textbox::TextEdit::DeleteToStart,
        ));
        type_into(&mut pane, "LOG_VERBOSITY");

        let after = rows_of(&pane);
        assert_eq!(after.len(), before.len(), "no row appeared or went away");
        assert!(
            after[at].contains("LOG_VERBOSITY"),
            "the rename draws on the row it started on: before {:?}, after {:?}",
            before[at],
            after[at]
        );
        assert_eq!(
            pane.env_typing().expect("open").opened_under(),
            Some("LOG_LEVEL")
        );
    }
}