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
use super::super::*;
impl App {
/// What a landed settings write answers with: re-read `shep.toml`, unless
/// the screen has an edit of its own in flight.
///
/// [`Msg::Settings`] rebuilds the whole screen, so a re-read raised by a
/// reply the screen was no longer waiting on would throw away the edit it
/// is waiting on instead. That edit's own reply re-reads a moment later,
/// and carries both writes' work with it.
pub(in crate::lookout::app) fn reread_settings(&self) -> Effect {
match self.settings() {
Some(settings) if settings.pending.is_some() => Effect::None,
_ => Effect::LoadSettings,
}
}
/// The settings screen's own keymap, in force while [`Self::settings`] is
/// `Some`. Everything not named here is ignored, an action key included.
pub(in crate::lookout::app) fn on_settings_key(&mut self, key: KeyPress) -> Effect {
self.notice = None;
match key {
KeyPress::Quit => return Effect::Quit,
// Both close, but an armed confirm eats the first one, the
// cancel-before-act rule the dashboard follows. `Escape` closing
// rather than quitting is where this screen swaps that cascade.
KeyPress::Settings | KeyPress::Escape => {
if !self.disarm_settings_candidate() {
self.body = Body::FlockTable;
}
}
// An armed candidate eats the first movement key rather than also
// moving: the next reflexive Enter would otherwise apply an edit to
// a row the operator lost track of. `Sent` is untouched.
KeyPress::SelectUp
| KeyPress::SelectDown
| KeyPress::SelectFirst
| KeyPress::SelectLast => {
if !self.disarm_settings_candidate()
&& let Some(settings) = self.settings_mut()
{
match key {
KeyPress::SelectUp => settings.move_by(-1),
KeyPress::SelectDown => settings.move_by(1),
KeyPress::SelectFirst => settings.move_to_first(),
KeyPress::SelectLast => settings.move_to_last(),
_ => unreachable!(),
}
}
}
KeyPress::Cycle => return self.cycle_setting(),
KeyPress::Confirm => return self.confirm_setting(),
// Re-reads `shep.toml`, so another process's write shows up, and
// the cursor survives. An armed candidate eats this key too.
KeyPress::Refresh => {
if self.disarm_settings_candidate() {
return Effect::None;
}
return Effect::LoadSettings;
}
// The probe, not the open, same reasoning as `on_key`'s `e`:
// the pane shows the dog's real schema and section or nothing.
// An armed candidate eats it first, like every other key here.
KeyPress::Edit => {
if self.disarm_settings_candidate() {
return Effect::None;
}
return self.probe_dog_schema();
}
// An armed candidate eats this too, on the same terms as
// `Refresh` and `Edit` above: a prompt left standing behind the
// overlay is a value the operator cannot see to confirm or
// cancel, so `h` cancels it and is consumed rather than also
// opening the overlay.
KeyPress::Help => {
if self.disarm_settings_candidate() {
return Effect::None;
}
return self.open_keymap();
}
// Unreachable from here, named so a new variant cannot fall
// silently into an arm that ignores it.
KeyPress::Action(_)
| KeyPress::FilterStart
| KeyPress::TextChar(_)
| KeyPress::TextBackspace
| KeyPress::TextApply
| KeyPress::TextAbandon
| KeyPress::Remove
| KeyPress::StepUp
| KeyPress::StepDown
| KeyPress::FoldView
| KeyPress::Secrets
| KeyPress::Reveal
| KeyPress::Copy
| KeyPress::TabPrev
| KeyPress::TabNext
| KeyPress::SecretDelete
| KeyPress::Collapse => {}
KeyPress::StreamCycle
| KeyPress::LevelCycle
| KeyPress::PageDown
| KeyPress::PageUp
| KeyPress::FollowToggle
| KeyPress::WrapToggle
| KeyPress::MatchNext
| KeyPress::MatchPrev
| KeyPress::Bleats
// `NextGroup`/`PrevGroup`/`Group`/`Undo`/`Continue` belong to the config
// pane: no other screen has groups to walk, a filed edit set
// to undo, or a close dialog to answer.
| KeyPress::NextGroup
| KeyPress::PrevGroup
| KeyPress::Group(_)
| KeyPress::Undo
| KeyPress::Continue => {}
}
Effect::None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lookout::view::fixtures;
#[test]
fn s_asks_for_the_file_before_the_screen_opens() {
let mut app = fixtures::full_app();
assert_eq!(
app.update(Msg::Key(KeyPress::Settings)),
Effect::LoadSettings
);
assert!(
app.settings().is_none(),
"nothing opens until the read lands"
);
}
#[test]
fn the_screen_opens_when_the_read_lands() {
let mut app = fixtures::full_app();
let _ = app.update(Msg::Key(KeyPress::Settings));
let _ = app.update(Msg::Settings {
result: Ok(fixtures::settings_snapshot()),
});
assert!(app.settings().is_some());
}
#[test]
fn a_read_that_failed_says_so_and_leaves_the_dashboard_up() {
let mut app = fixtures::full_app();
let _ = app.update(Msg::Key(KeyPress::Settings));
let _ = app.update(Msg::Settings {
result: Err("no such file".into()),
});
assert!(app.settings().is_none());
let notice = app.notice().expect("a failed read has to say so");
assert!(notice.is_grave());
assert!(notice.to_string().contains("no such file"));
}
#[test]
fn s_closes_the_screen_again() {
let mut app = fixtures::app_in_settings();
let _ = app.update(Msg::Key(KeyPress::Settings));
assert!(app.settings().is_none());
}
/// From the dashboard with no filter `Esc` quits; from here it must not.
#[test]
fn escape_closes_the_screen_and_never_quits() {
let mut app = fixtures::app_in_settings();
assert_eq!(app.update(Msg::Key(KeyPress::Escape)), Effect::None);
assert!(app.settings().is_none());
}
#[test]
fn the_flock_cursor_and_the_filter_survive_the_swap() {
let mut app = fixtures::full_app();
let _ = app.update(Msg::Key(KeyPress::FilterStart));
for c in "web".chars() {
let _ = app.update(Msg::Key(KeyPress::TextChar(c)));
}
let _ = app.update(Msg::Key(KeyPress::TextApply));
let selected = app.selected();
let filter = app.filter().to_string();
let _ = app.update(Msg::Key(KeyPress::Settings));
let _ = app.update(Msg::Settings {
result: Ok(fixtures::settings_snapshot()),
});
let _ = app.update(Msg::Key(KeyPress::Settings));
assert_eq!(app.selected(), selected);
assert_eq!(app.filter(), filter);
}
#[test]
fn the_settings_cursor_starts_at_the_first_row_on_every_open() {
let mut app = fixtures::app_in_settings();
let _ = app.update(Msg::Key(KeyPress::SelectDown));
let _ = app.update(Msg::Key(KeyPress::SelectDown));
let _ = app.update(Msg::Key(KeyPress::Settings));
let _ = app.update(Msg::Key(KeyPress::Settings));
let _ = app.update(Msg::Settings {
result: Ok(fixtures::settings_snapshot()),
});
let first = app.settings().unwrap().rows()[0];
assert_eq!(app.settings().unwrap().cursor(), Some(first));
}
#[test]
fn the_cursor_moves_through_the_scalars_and_into_the_dogs() {
let mut app = fixtures::app_in_settings();
let rows = app.settings().unwrap().rows();
for _ in 0..rows.len() - 1 {
let _ = app.update(Msg::Key(KeyPress::SelectDown));
}
assert_eq!(
app.settings().unwrap().cursor(),
Some(*rows.last().unwrap())
);
// and it stops rather than wrapping
let _ = app.update(Msg::Key(KeyPress::SelectDown));
assert_eq!(
app.settings().unwrap().cursor(),
Some(*rows.last().unwrap())
);
}
#[test]
fn an_action_key_from_the_dashboard_is_unreachable_while_the_screen_is_up() {
let mut app = fixtures::app_in_settings_with_control();
// `x` is the stop key on the dashboard. In here it is not an action.
let _ = app.update(Msg::Key(KeyPress::Action(ActionVerb::Stop)));
assert!(app.action().is_none(), "no sheep confirm can arm from here");
}
/// Every key sequence that ends in a write, not one key: a gate guarding
/// `space` alone leaves the free-text editor's route reaching
/// `WriteSetting` on a read-only lookout.
///
/// The refusal is checked per keypress rather than at the end, because
/// `on_settings_key` clears `self.notice` on every key.
#[test]
fn no_key_route_writes_the_config_while_the_gate_is_closed() {
use KeyPress::{Confirm, Cycle, SelectDown, TextApply, TextChar};
// `Settings::rows` puts the six scalars first, then the fixture's two
// dogs: two `SelectDown`s reach `socket`, six the first dog row.
let routes: &[(&str, &[KeyPress])] = &[
("space on a cycled scalar", &[Cycle, Confirm]),
(
"the socket editor",
&[
SelectDown,
SelectDown,
Confirm,
TextChar('/'),
TextChar('x'),
TextApply,
Confirm,
],
),
(
"the max_cron_sleep editor",
&[
SelectDown,
SelectDown,
SelectDown,
Confirm,
TextChar('9'),
TextChar('s'),
TextApply,
Confirm,
],
),
(
"the whistle gate",
&[
SelectDown, SelectDown, SelectDown, SelectDown, Cycle, Confirm,
],
),
(
"space on a dog row",
&[
SelectDown, SelectDown, SelectDown, SelectDown, SelectDown, SelectDown, Cycle,
Confirm,
],
),
];
for (what, keys) in routes {
let mut app = fixtures::app_in_settings(); // Control::ReadOnly
let mut refused = false;
for key in *keys {
let effect = app.update(Msg::Key(*key));
assert!(
!matches!(
effect,
Effect::WriteSetting { .. } | Effect::WriteDog { .. }
),
"{what}: a read-only lookout reached {effect:?}"
);
refused |= app.notice().is_some_and(Notice::is_grave);
}
assert!(refused, "{what}: the refusal has to say why");
assert!(
app.settings().unwrap().pending().is_none(),
"{what}: nothing is left armed"
);
assert!(
app.settings().unwrap().typing().is_none(),
"{what}: no editor is left open"
);
}
}
/// The half that keeps the closed-gate test honest: a gate that refused
/// everything would pass it and be useless.
#[test]
fn every_one_of_those_routes_writes_once_the_gate_is_open() {
use KeyPress::{Confirm, Cycle, SelectDown, TextApply, TextChar};
let routes: &[(&str, &[KeyPress])] = &[
("space on a cycled scalar", &[Cycle, Confirm]),
(
"the socket editor",
&[
SelectDown,
SelectDown,
Confirm,
TextChar('/'),
TextChar('x'),
TextApply,
Confirm,
],
),
(
"space on a dog row",
&[
SelectDown, SelectDown, SelectDown, SelectDown, SelectDown, SelectDown, Cycle,
Confirm,
],
),
];
for (what, keys) in routes {
let mut app = fixtures::app_in_settings_with_control();
let mut wrote = false;
for key in *keys {
let effect = app.update(Msg::Key(*key));
wrote |= matches!(
effect,
Effect::WriteSetting { .. } | Effect::WriteDog { .. }
);
}
assert!(wrote, "{what}: an open gate has to reach the write");
}
}
#[test]
fn a_read_only_lookout_opens_the_screen_and_refuses_the_edit_key() {
let mut app = fixtures::app_in_settings(); // Control::ReadOnly
assert!(app.settings().is_some(), "reading shep.toml is not gated");
let _ = app.update(Msg::Key(KeyPress::Cycle));
let notice = app.notice().expect("the refusal has to say why");
assert!(notice.is_grave());
}
/// `s` raises `Effect::LoadSettings` while `body` is still `Body::FlockTable`,
/// so `x` reaches `arm()`. Once the read lands, `on_settings_key` no-ops
/// `Confirm`, so nothing could resolve the armed action.
#[test]
fn opening_the_screen_clears_an_action_armed_while_the_read_was_in_flight() {
let mut app = fixtures::allowed_app();
let _ = app.update(Msg::Key(KeyPress::Settings));
let _ = app.update(Msg::Key(KeyPress::Action(ActionVerb::Stop)));
assert!(
app.action().is_some(),
"the arm must still succeed before the read lands"
);
let _ = app.update(Msg::Settings {
result: Ok(fixtures::settings_snapshot()),
});
assert!(
app.action().is_none(),
"no armed action may survive the screen opening"
);
}
}