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
//! What the operator had already done when the frame was taken: a row
//! picked, a pane opened, an overlay raised, a filter typed.
//!
//! Everything here runs BEFORE `scene_with`'s tick. State whose own
//! expiry is shorter than the 600s the gallery renders at has to wait
//! until after it, and lives in [`super::armed`] instead.
use shep_core::protocol::Response;
use crate::lookout::app::{App, KeyPress, Msg, Sent};
use crate::lookout::frames::fixtures::{
select_fold, select_group, select_id, sheep_pane_config_view,
};
use crate::lookout::frames::scene::Scene;
/// Replays the operator's own keys and selections onto `app`.
pub(super) fn prepare_scene(app: &mut App, which: Scene) {
// Every `Keymap*` scene uses the healthy flock fixture `super::flock`
// falls through to, and raises the overlay right after: `h` toggles
// `App::keymap_open` and nothing else, so running ahead of the
// selection, the host sample or the freeze does not matter.
if which.is_keymap() {
app.update(Msg::Key(KeyPress::Help));
}
// Selects `api` (id 2) so the panes below describe a fixed sheep,
// walked by id since the table sorts by name. Skipped where there is
// no flock, no pane below the table, or the cursor belongs elsewhere
// (`Grouped`, `Folds`, and the three settings scenes with no id 2).
if !matches!(
which,
Scene::Empty
| Scene::Narrow
| Scene::TooNarrow
| Scene::TableOnly
| Scene::Grouped
| Scene::Folds
| Scene::WithDogs
| Scene::MemCeiling
| Scene::CfgDrift
| Scene::SettingsDogs
| Scene::SettingsNarrow
| Scene::SettingsDogEnv
| Scene::SettingsDogEnvNarrow
| Scene::SettingsShort
| Scene::SheepPane
| Scene::SheepPaneCpuOnly
| Scene::SheepPaneSparklines
| Scene::SheepPaneShort
) {
select_id(app, 2);
}
// Onto `web`'s group header, which is the one row in the gallery that is
// not a sheep. Every pane below the table renders its own group state
// from it: the rollup line, the per-instance lamb sentence, and the
// feed's refusal to pick an instance to tail.
if which == Scene::Grouped {
select_group(app, "web");
}
// `Folds` presses `F` to switch the table into the fold view, collapses
// `batch` (the smallest fold, so `z`'s effect is visible without hiding
// much), and parks the cursor on `edge`'s own header, the fold that
// carries the grouped app.
if which == Scene::Folds {
app.update(Msg::Key(KeyPress::FoldView));
select_fold(app, "batch");
app.update(Msg::Key(KeyPress::Collapse));
select_fold(app, "edge");
}
// `LambsUnknown` wants `cron`, id 4, instead.
if which == Scene::LambsUnknown {
select_id(app, 4);
}
// `WithDogs` parks on the silent adopted dog, id 91, the row this
// scene exists to show.
if which == Scene::WithDogs {
select_id(app, 91);
}
// `MemCeiling` parks on `web-hot`, id 1, the row at 94% of its
// ceiling: the butter warning this scene exists to show.
if which == Scene::MemCeiling {
select_id(app, 1);
}
// `CfgDrift` parks on `web`, id 10, the row carrying the pending
// fields: the detail pane's own `cfg !2 pending` cell only renders
// for the selected sheep.
if which == Scene::CfgDrift {
select_id(app, 10);
}
// Every sheep-pane scene parks on `web`, id 20 (the only row in its
// own flock), opens the pane the same way the event loop does (`Enter`
// on the selected row), and answers the config request it fires so the
// config column has real fields to draw rather than "reading config…".
if matches!(
which,
Scene::SheepPane
| Scene::SheepPaneCpuOnly
| Scene::SheepPaneSparklines
| Scene::SheepPaneShort
) {
select_id(app, 20);
app.update(Msg::Key(KeyPress::Confirm));
app.update(Msg::Replied {
sent: Sent::SheepConfig {
name: "web".to_string(),
},
result: Ok(Response::SheepConfig(Box::new(sheep_pane_config_view()))),
});
}
match which {
Scene::FilterEditing | Scene::FilterNoMatch | Scene::FilterActive => {
app.update(Msg::Key(KeyPress::FilterStart));
let query = if which == Scene::FilterNoMatch {
"zzz"
} else {
"web"
};
for typed in query.chars() {
app.update(Msg::Key(KeyPress::TextChar(typed)));
}
if which == Scene::FilterActive {
app.update(Msg::Key(KeyPress::TextApply));
}
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use crate::lookout::frames::render::render_text;
use crate::lookout::frames::scene;
use crate::lookout::keymap::Group;
use super::*;
/// Each filter scene shows the query it is named for, at the stage of
/// typing it is named for.
#[test]
#[cfg(unix)] // inherited, not measured per test: see the `build` module's docs
fn every_filter_scene_shows_the_query_it_is_named_for() {
// FilterEditing: mid-type, table already narrowed.
let editing = render_text(&scene(Scene::FilterEditing).1);
assert_eq!(
editing
.lines()
.filter(|line| line.contains(" web "))
.count(),
2,
"two rows survived the query"
);
assert!(!editing.contains("billing"), "and the rest did not");
assert!(editing.contains("2 of 6 in the flock"), "got {editing:?}");
assert!(
editing.contains("filter web\u{258f}"),
"the query and the cursor"
);
for named in ["enter applies", "esc cancels", "ctrl-c quits"] {
assert!(editing.contains(named), "the box names {named}");
}
// FilterActive: the same query, box closed.
let active = render_text(&scene(Scene::FilterActive).1);
assert!(active.contains("filter \"web\""), "the box is closed");
assert!(!active.contains("enter applies"), "and its keys are gone");
assert!(active.contains("2 of 6 in the flock"), "still narrowed");
assert!(active.contains("/ edit") && active.contains("esc clear"));
// FilterNoMatch: names the query rather than claiming empty.
let none = render_text(&scene(Scene::FilterNoMatch).1);
assert!(none.contains("no sheep's name contains \"zzz\""));
assert!(!none.contains("the flock is empty"));
assert!(none.contains("0 of 6 in the flock"));
}
/// Each sheep-pane scene draws the columns its width leaves room for,
/// down to the pair that share a row at 99.
#[test]
#[cfg(unix)] // inherited, not measured per test: see the `build` module's docs
fn every_sheep_pane_scene_draws_the_columns_it_is_named_for() {
// SheepPane: both charts, the config column, and the feed, all at
// 160x48.
let sheep_pane = render_text(&scene(Scene::SheepPane).1);
assert!(
sheep_pane.contains("\u{2588}\u{2588} CPU")
&& sheep_pane.contains("\u{2588}\u{2588} MEM"),
"both charts draw: {sheep_pane:?}"
);
assert!(
sheep_pane.contains("\u{2588}\u{2588} CONFIG & ENV") && sheep_pane.contains("script"),
"the config column lists web's own fields: {sheep_pane:?}"
);
assert!(
sheep_pane.contains("\u{2588}\u{2588} BLEATS"),
"the embedded feed carries its own header: {sheep_pane:?}"
);
// SheepPaneCpuOnly: 139 columns, the CPU chart alone plus a
// one-line memory summary.
let cpu_only = render_text(&scene(Scene::SheepPaneCpuOnly).1);
assert!(
cpu_only.contains("\u{2588}\u{2588} CPU"),
"the CPU chart still draws: {cpu_only:?}"
);
assert!(
!cpu_only.contains("\u{2588}\u{2588} MEM"),
"the memory chart is gone: {cpu_only:?}"
);
assert!(
cpu_only.contains("rss "),
"replaced by a one-line rss summary: {cpu_only:?}"
);
// SheepPaneSparklines: 99 columns, 1a's own pair on one row.
let sparklines = render_text(&scene(Scene::SheepPaneSparklines).1);
assert!(
!sparklines.contains("\u{2588}\u{2588} CPU")
&& !sparklines.contains("\u{2588}\u{2588} MEM"),
"both charts are gone: {sparklines:?}"
);
assert!(
sparklines.contains("CPU 20s") && sparklines.contains("MEM/CEIL"),
"1a's own pair draws instead: {sparklines:?}"
);
// SheepPaneShort: 160x25, the memory chart gone, the CPU chart and
// the config column still up.
let short_pane = render_text(&scene(Scene::SheepPaneShort).1);
assert!(
short_pane.contains("\u{2588}\u{2588} CPU"),
"the CPU chart still draws: {short_pane:?}"
);
assert!(
!short_pane.contains("\u{2588}\u{2588} MEM"),
"the memory chart is gone under 26 rows: {short_pane:?}"
);
assert!(
short_pane.contains("\u{2588}\u{2588} CONFIG & ENV"),
"the config column, which gives ground last, is still up: {short_pane:?}"
);
}
/// Each width scene draws the column count it exists to show, counted
/// off the heading row's own occupied starts rather than inferred from
/// a word being present. A test that only looked for `MOVING` would
/// pass at every width in the table, since `MOVING` draws at every
/// width the overlay ever renders.
#[test]
fn each_keymap_width_scene_draws_its_own_column_count() {
for (which, wanted) in [
(Scene::Keymap, 4),
(Scene::KeymapFloor, 4),
(Scene::KeymapBorderlessWide, 4),
(Scene::KeymapNarrow, 3),
(Scene::KeymapTwoColumn, 2),
] {
let rendered = render_text(&scene(which).1);
let first_bank = rendered
.lines()
// `Group::Moving.heading()`, not the literal "MOVING": the
// count below derives from `heading()`, so a rename would
// leave this `find` hunting a word nothing draws rather than
// failing as a rename.
.find(|row| row.contains(Group::Moving.heading()))
.expect("no heading row");
let drawn = Group::DRAWN
.iter()
.filter(|group| first_bank.contains(group.heading()))
.count();
assert_eq!(drawn, wanted, "{}: {first_bank:?}", which.label());
}
}
/// Driven off the scene label rather than a hand-kept list, so a new
/// `Keymap*` scene joins this test by being named and a variant dropped
/// from the builder's `matches!` list fails it.
///
/// The count is the other half: it catches a keymap scene whose label
/// does not start with `keymap`, which this test would otherwise skip
/// in silence.
#[test]
fn every_keymap_scene_raises_the_overlay() {
let mut checked = 0;
for which in Scene::ALL {
if !which.label().starts_with("keymap") {
continue;
}
checked += 1;
let rendered = render_text(&scene(*which).1);
assert!(
rendered.contains(Group::Moving.heading()),
"{}: the overlay did not draw",
which.label()
);
}
assert_eq!(checked, 8, "the keymap scenes, counted by label");
}
}