gregg 1.0.0

Compact keyboard-first terminal monitor that polls greggd endpoints and renders each system in four rows.
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
#![allow(dead_code)]

//! Raw input and signal events for the TUI event loop.
//!
//! Events represent external occurrences that the TUI loop receives and
//! translates into [`Action`](crate::action::Action)s before applying
//! them to [`AppState`](crate::state::AppState). Separating raw events
//! from actions keeps the translation logic testable and the state
//! reducer pure.

use crate::poller::PollBatch;

/// A raw event from the terminal, OS signals, or internal sources.
///
/// The TUI loop receives events and maps them to typed
/// [`Action`](crate::action::Action)s. This indirection lets the
/// state engine remain independent of input handling details.
pub enum Event {
    /// A key was pressed on the terminal.
    KeyInput(KeyEvent),
    /// The terminal was resized.
    Resize {
        /// New width in columns.
        width: u16,
        /// New height in rows.
        height: u16,
    },
    /// An OS signal was received.
    Signal(SignalKind),
    /// A poll batch arrived from the scheduler.
    BatchReceived(PollBatch),
    /// The configuration file changed on disk.
    ConfigChanged,
}

/// A key event from the terminal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyEvent {
    /// The key that was pressed.
    pub key: Key,
    /// Whether the Control modifier was held.
    pub ctrl: bool,
    /// Whether the Alt/Option modifier was held.
    pub alt: bool,
    /// Whether the Shift modifier was held.
    pub shift: bool,
}

/// A terminal key.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Key {
    /// A Unicode character.
    Char(char),
    /// The Enter/Return key.
    Enter,
    /// The Escape key.
    Esc,
    /// The Tab key.
    Tab,
    /// The Backspace key.
    Backspace,
    /// The Delete key.
    Delete,
    /// Arrow up.
    Up,
    /// Arrow down.
    Down,
    /// Arrow left.
    Left,
    /// Arrow right.
    Right,
    /// Home key.
    Home,
    /// End key.
    End,
    /// Page Up key.
    PageUp,
    /// Page Down key.
    PageDown,
}

/// An OS signal that the TUI loop handles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignalKind {
    /// SIGHUP — reload configuration.
    Hangup,
    /// SIGWINCH — terminal resized (on platforms that deliver it).
    WindowChange,
    /// SIGTERM / SIGINT — graceful shutdown.
    Terminate,
}

/// Translate a [`KeyEvent`] into an [`Action`](crate::action::Action).
///
/// Returns `None` for keys that do not map to an action.
#[must_use]
pub fn key_to_action(event: KeyEvent) -> Option<crate::action::Action> {
    use crate::action::Action;

    if event.ctrl {
        return match event.key {
            Key::Char('c') => Some(Action::Quit),
            Key::Char('r') => Some(Action::RefreshNow),
            _ => None,
        };
    }

    match event.key {
        Key::Char('j') | Key::Down => Some(Action::SelectNext),
        Key::Char('k') | Key::Up => Some(Action::SelectPrevious),
        Key::Char('g') if !event.shift => Some(Action::SelectFirst),
        Key::Char('G') => Some(Action::SelectLast),
        Key::PageDown | Key::Char('f') if !event.ctrl => Some(Action::PageDown),
        Key::PageUp | Key::Char('b') if !event.ctrl => Some(Action::PageUp),
        Key::Char('q') | Key::Esc => Some(Action::Quit),
        _ => None,
    }
}

/// Translate an [`Event`] into zero or more [`Action`](crate::action::Action)s.
///
/// Most events produce a single action. Some (like a signal) may produce
/// none if the caller handles them directly.
#[must_use]
pub fn translate_event(event: &Event) -> Option<crate::action::Action> {
    match event {
        Event::KeyInput(key_event) => key_to_action(*key_event),
        Event::Resize { width, height } => Some(crate::action::Action::Resize {
            width: *width,
            height: *height,
        }),
        Event::Signal(SignalKind::Terminate) => Some(crate::action::Action::Quit),
        Event::Signal(SignalKind::Hangup | SignalKind::WindowChange)
        | Event::BatchReceived(_)
        | Event::ConfigChanged => None,
    }
}

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

    #[test]
    fn key_variants_exist() {
        let keys = [
            Key::Char('a'),
            Key::Enter,
            Key::Esc,
            Key::Tab,
            Key::Backspace,
            Key::Delete,
            Key::Up,
            Key::Down,
            Key::Left,
            Key::Right,
            Key::Home,
            Key::End,
            Key::PageUp,
            Key::PageDown,
        ];
        assert_eq!(keys.len(), 14);
        assert_eq!(keys[0], Key::Char('a'));
        assert_eq!(keys[6], Key::Up);
    }

    #[test]
    fn event_variants_exist() {
        let events = [
            Event::KeyInput(KeyEvent {
                key: Key::Char('j'),
                ctrl: false,
                alt: false,
                shift: false,
            }),
            Event::Resize {
                width: 80,
                height: 24,
            },
            Event::Signal(SignalKind::Hangup),
            Event::BatchReceived(crate::poller::PollBatch {
                generation: 1,
                started_at: std::time::Instant::now(),
                completed_at: std::time::Instant::now(),
                results: vec![],
            }),
            Event::ConfigChanged,
        ];
        assert_eq!(events.len(), 5);
    }

    #[test]
    fn key_to_action_j_and_k() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('j'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::SelectNext)));

        let action = key_to_action(KeyEvent {
            key: Key::Char('k'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(
            action,
            Some(crate::action::Action::SelectPrevious)
        ));
    }

    #[test]
    fn key_to_action_arrow_keys() {
        let action = key_to_action(KeyEvent {
            key: Key::Down,
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::SelectNext)));

        let action = key_to_action(KeyEvent {
            key: Key::Up,
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(
            action,
            Some(crate::action::Action::SelectPrevious)
        ));
    }

    #[test]
    fn key_to_action_ctrl_c_quits() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('c'),
            ctrl: true,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::Quit)));
    }

    #[test]
    fn key_to_action_ctrl_r_refreshes() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('r'),
            ctrl: true,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::RefreshNow)));
    }

    #[test]
    fn key_to_action_gg_selects_first() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('g'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::SelectFirst)));
    }

    #[test]
    fn key_to_action_shift_g_selects_last() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('G'),
            ctrl: false,
            alt: false,
            shift: true,
        });
        assert!(matches!(action, Some(crate::action::Action::SelectLast)));
    }

    #[test]
    fn key_to_action_page_down_and_up() {
        let action = key_to_action(KeyEvent {
            key: Key::PageDown,
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::PageDown)));

        let action = key_to_action(KeyEvent {
            key: Key::PageUp,
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::PageUp)));
    }

    #[test]
    fn key_to_action_f_and_b() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('f'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::PageDown)));

        let action = key_to_action(KeyEvent {
            key: Key::Char('b'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::PageUp)));
    }

    #[test]
    fn key_to_action_q_quits() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('q'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::Quit)));
    }

    #[test]
    fn key_to_action_esc_quits() {
        let action = key_to_action(KeyEvent {
            key: Key::Esc,
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(matches!(action, Some(crate::action::Action::Quit)));
    }

    #[test]
    fn key_to_action_unmapped_returns_none() {
        let action = key_to_action(KeyEvent {
            key: Key::Char('x'),
            ctrl: false,
            alt: false,
            shift: false,
        });
        assert!(action.is_none());
    }

    #[test]
    fn translate_event_resize() {
        let event = Event::Resize {
            width: 120,
            height: 40,
        };
        let action = translate_event(&event);
        assert!(matches!(
            action,
            Some(crate::action::Action::Resize {
                width: 120,
                height: 40
            })
        ));
    }

    #[test]
    fn translate_event_signal_terminate() {
        let event = Event::Signal(SignalKind::Terminate);
        let action = translate_event(&event);
        assert!(matches!(action, Some(crate::action::Action::Quit)));
    }

    #[test]
    fn translate_event_signal_hangup_returns_none() {
        let event = Event::Signal(SignalKind::Hangup);
        let action = translate_event(&event);
        assert!(action.is_none());
    }

    #[test]
    fn translate_event_batch_returns_none() {
        let event = Event::BatchReceived(crate::poller::PollBatch {
            generation: 1,
            started_at: std::time::Instant::now(),
            completed_at: std::time::Instant::now(),
            results: vec![],
        });
        let action = translate_event(&event);
        assert!(action.is_none());
    }

    #[test]
    fn signal_kind_variants() {
        assert_eq!(SignalKind::Hangup, SignalKind::Hangup);
        assert_eq!(SignalKind::WindowChange, SignalKind::WindowChange);
        assert_eq!(SignalKind::Terminate, SignalKind::Terminate);
        assert_ne!(SignalKind::Hangup, SignalKind::Terminate);
    }

    #[test]
    fn key_event_modifiers() {
        let event = KeyEvent {
            key: Key::Char('a'),
            ctrl: true,
            alt: true,
            shift: true,
        };
        assert!(event.ctrl);
        assert!(event.alt);
        assert!(event.shift);
    }
}