dae 0.1.1

A Linux daemon management TUI powered by scrin, aisling, and scrin-widgets concepts.
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
use std::time::{Duration, Instant};

use anyhow::Result;
use clap::Parser;
use crossterm::event::{
    self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton as CrosstermButton,
    MouseEvent, MouseEventKind,
};
use scrin::interaction::{MouseButton, PointerEvent, PointerEventKind, UiEvent};
use scrin::{PresentStrategy, Terminal, TerminalOptions, WidgetId};

use crate::daemon::{self, Daemon, DaemonAction};

#[derive(Clone, Debug, Parser)]
#[command(
    name = "dae",
    author = "Trevor Knott, Knott Dynamics",
    version,
    about = "A scrin, aisling, and scrin-widgets Linux daemon management TUI",
    long_about = "dae is a Linux daemon cockpit for systemd services. It uses scrin interaction metadata, scrin-widgets surfaces, and Aisling effects to view daemon state, journal history, anomaly flags, and quick management actions."
)]
pub struct Cli {
    /// Journal lookback window in hours.
    #[arg(short = 'b', long, default_value_t = 24)]
    pub lookback_hours: u64,

    /// Journal lines loaded for the selected service.
    #[arg(short = 'n', long, default_value_t = 240)]
    pub journal_lines: usize,

    /// Background refresh interval in seconds.
    #[arg(short = 'r', long, default_value_t = 5)]
    pub refresh_seconds: u64,

    /// Disable automatic list refresh.
    #[arg(long)]
    pub no_auto_refresh: bool,

    /// Initial service filter.
    #[arg(short, long)]
    pub filter: Option<String>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum DetailTab {
    Journal,
    Status,
}

impl DetailTab {
    pub(crate) fn label(self) -> &'static str {
        match self {
            Self::Journal => "journal",
            Self::Status => "status",
        }
    }
}

#[derive(Clone, Debug, Default)]
pub(crate) struct DetailCache {
    pub unit: String,
    pub journal: Vec<String>,
    pub status: Vec<String>,
}

#[derive(Debug)]
pub(crate) struct App {
    pub cli: Cli,
    pub daemons: Vec<Daemon>,
    pub detail: DetailCache,
    pub detail_tab: DetailTab,
    pub selected: usize,
    pub table_scroll: u16,
    pub detail_scroll: u16,
    pub table_rows_hint: u16,
    pub hovered_row: Option<usize>,
    pub mouse_position: Option<(u16, u16)>,
    pub filter: String,
    pub filter_mode: bool,
    pub message: String,
    pub last_refresh: Instant,
    pub tick: u64,
    pub running: bool,
}

impl App {
    pub fn new(cli: Cli) -> Self {
        let filter = cli.filter.clone().unwrap_or_default();
        Self {
            cli,
            daemons: Vec::new(),
            detail: DetailCache::default(),
            detail_tab: DetailTab::Journal,
            selected: 0,
            table_scroll: 0,
            detail_scroll: 0,
            table_rows_hint: 18,
            hovered_row: None,
            mouse_position: None,
            filter,
            filter_mode: false,
            message: "loading daemons".to_string(),
            last_refresh: Instant::now(),
            tick: 0,
            running: true,
        }
    }

    pub fn run(mut self) -> Result<()> {
        self.refresh_daemons();
        self.refresh_detail();

        let mut terminal = Terminal::init_with(TerminalOptions {
            mouse_capture: true,
            bracketed_paste: true,
            ..TerminalOptions::default()
        })?;
        let result = self.run_terminal(&mut terminal);
        terminal.restore()?;
        result
    }

    pub(crate) fn visible_indices(&self) -> Vec<usize> {
        let filter = self.filter.trim().to_ascii_lowercase();
        self.daemons
            .iter()
            .enumerate()
            .filter_map(|(idx, daemon)| {
                if filter.is_empty()
                    || daemon.unit.to_ascii_lowercase().contains(&filter)
                    || daemon.description.to_ascii_lowercase().contains(&filter)
                    || daemon.state_label().to_ascii_lowercase().contains(&filter)
                    || daemon
                        .anomaly_summary()
                        .to_ascii_lowercase()
                        .contains(&filter)
                {
                    Some(idx)
                } else {
                    None
                }
            })
            .collect()
    }

    pub(crate) fn visible_daemon_at(&self, visible_row: usize) -> Option<&Daemon> {
        self.visible_indices()
            .get(visible_row)
            .and_then(|idx| self.daemons.get(*idx))
    }

    pub(crate) fn selected_daemon(&self) -> Option<&Daemon> {
        self.visible_daemon_at(self.selected)
    }

    pub(crate) fn selected_unit(&self) -> Option<String> {
        self.selected_daemon().map(|daemon| daemon.unit.clone())
    }

    pub(crate) fn visible_count(&self) -> usize {
        self.visible_indices().len()
    }

    pub(crate) fn anomaly_count(&self) -> usize {
        self.daemons
            .iter()
            .filter(|daemon| !daemon.anomalies.is_empty())
            .count()
    }

    pub(crate) fn running_count(&self) -> usize {
        self.daemons
            .iter()
            .filter(|daemon| daemon.active == "active" && daemon.sub == "running")
            .count()
    }

    pub(crate) fn detail_lines(&self) -> &[String] {
        match self.detail_tab {
            DetailTab::Journal => &self.detail.journal,
            DetailTab::Status => &self.detail.status,
        }
    }

    fn run_terminal(&mut self, terminal: &mut Terminal) -> Result<()> {
        while self.running {
            self.auto_refresh_if_due();
            terminal.draw_with_present_strategy(PresentStrategy::MarkedDirty, |frame| {
                crate::ui::render(frame, self);
            })?;

            if event::poll(Duration::from_millis(50))? {
                match event::read()? {
                    Event::Key(key) if key.kind == KeyEventKind::Press => self.handle_key(key),
                    Event::Mouse(mouse) => self.handle_mouse(terminal, mouse),
                    Event::Resize(_, _) => {}
                    _ => {}
                }
            }
            self.tick = self.tick.wrapping_add(1);
        }
        Ok(())
    }

    fn handle_key(&mut self, key: KeyEvent) {
        if key.modifiers.contains(KeyModifiers::CONTROL)
            && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('q'))
        {
            self.running = false;
            return;
        }

        if self.filter_mode {
            self.handle_filter_key(key);
            return;
        }

        match key.code {
            KeyCode::Char('q') | KeyCode::Esc => self.running = false,
            KeyCode::Char('/') => {
                self.filter_mode = true;
                self.message =
                    "filter mode: type to narrow services, Enter applies, Esc clears".to_string();
            }
            KeyCode::Char('c') => {
                self.filter.clear();
                self.selected = 0;
                self.table_scroll = 0;
                self.refresh_detail();
            }
            KeyCode::Down | KeyCode::Char('j') => self.select_next(),
            KeyCode::Up | KeyCode::Char('k') => self.select_prev(),
            KeyCode::PageDown => self.page_down(),
            KeyCode::PageUp => self.page_up(),
            KeyCode::Home => self.select_first(),
            KeyCode::End => self.select_last(),
            KeyCode::Tab => self.toggle_detail_tab(),
            KeyCode::Char('[') => self.scroll_detail_up(),
            KeyCode::Char(']') => self.scroll_detail_down(),
            KeyCode::Char('1') => self.set_lookback(1),
            KeyCode::Char('6') => self.set_lookback(6),
            KeyCode::Char('2') => self.set_lookback(24),
            KeyCode::Char('7') => self.set_lookback(168),
            KeyCode::Char('R') => self.refresh_all(),
            KeyCode::Char('s') => self.run_action(DaemonAction::Start),
            KeyCode::Char('x') => self.run_action(DaemonAction::Stop),
            KeyCode::Char('r') => self.run_action(DaemonAction::Restart),
            KeyCode::Char('l') => self.run_action(DaemonAction::Reload),
            KeyCode::Char('e') => self.run_action(DaemonAction::Enable),
            KeyCode::Char('d') => self.run_action(DaemonAction::Disable),
            KeyCode::Char('t') => self.run_action(DaemonAction::KillTerm),
            KeyCode::Char('K') => self.run_action(DaemonAction::KillKill),
            _ => {}
        }
    }

    fn handle_filter_key(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Esc => {
                self.filter_mode = false;
                self.filter.clear();
                self.selected = 0;
                self.table_scroll = 0;
                self.refresh_detail();
            }
            KeyCode::Enter => {
                self.filter_mode = false;
                self.clamp_selection();
                self.refresh_detail();
            }
            KeyCode::Backspace => {
                self.filter.pop();
                self.selected = self.selected.min(self.visible_count().saturating_sub(1));
                self.ensure_selected_visible();
            }
            KeyCode::Char(ch) => {
                if !ch.is_control() {
                    self.filter.push(ch);
                    self.selected = self.selected.min(self.visible_count().saturating_sub(1));
                    self.ensure_selected_visible();
                }
            }
            _ => {}
        }
    }

    fn handle_mouse(&mut self, terminal: &mut Terminal, mouse: MouseEvent) {
        self.mouse_position = Some((mouse.column, mouse.row));

        match mouse.kind {
            MouseEventKind::Moved => {
                let _ = terminal.handle_pointer_event(PointerEvent::new(
                    PointerEventKind::Move,
                    mouse.column,
                    mouse.row,
                ));
                self.set_hover_from_hit(terminal.hit_test(mouse.column, mouse.row).map(|r| &r.id));
            }
            MouseEventKind::Down(button) => {
                let _ = terminal.handle_pointer_event(PointerEvent::new(
                    PointerEventKind::Down(map_mouse_button(button)),
                    mouse.column,
                    mouse.row,
                ));
            }
            MouseEventKind::Up(button) => {
                let batch = terminal.handle_pointer_event(PointerEvent::new(
                    PointerEventKind::Up(map_mouse_button(button)),
                    mouse.column,
                    mouse.row,
                ));
                for ui_event in batch.events {
                    if let UiEvent::Click { id, button, .. } = ui_event {
                        self.handle_click(&id, button);
                    }
                }
                self.set_hover_from_hit(terminal.hit_test(mouse.column, mouse.row).map(|r| &r.id));
            }
            MouseEventKind::Drag(button) => {
                let _ = terminal.handle_pointer_event(PointerEvent::new(
                    PointerEventKind::Drag(map_mouse_button(button)),
                    mouse.column,
                    mouse.row,
                ));
            }
            MouseEventKind::ScrollUp => self.scroll_for_pointer(false, terminal, mouse),
            MouseEventKind::ScrollDown => self.scroll_for_pointer(true, terminal, mouse),
            MouseEventKind::ScrollLeft | MouseEventKind::ScrollRight => {}
        }
    }

    fn handle_click(&mut self, id: &WidgetId, button: MouseButton) {
        if button != MouseButton::Left {
            return;
        }
        let id = id.as_ref();
        if let Some(row) = crate::ui::service_row_from_id(id) {
            self.selected = row.min(self.visible_count().saturating_sub(1));
            self.ensure_selected_visible();
            self.refresh_detail();
            return;
        }
        if let Some(action) = crate::ui::action_from_id(id) {
            self.run_action(action);
        }
    }

    fn set_hover_from_hit(&mut self, id: Option<&WidgetId>) {
        self.hovered_row = id.and_then(|id| crate::ui::service_row_from_id(id.as_ref()));
    }

    fn scroll_for_pointer(&mut self, down: bool, terminal: &Terminal, mouse: MouseEvent) {
        let hit_id = terminal
            .hit_test(mouse.column, mouse.row)
            .map(|region| region.id.as_ref());
        if matches!(hit_id, Some(id) if id.starts_with(crate::ui::DETAIL_PREFIX)) {
            if down {
                self.scroll_detail_down();
            } else {
                self.scroll_detail_up();
            }
        } else if down {
            self.table_scroll = self.table_scroll.saturating_add(3);
        } else {
            self.table_scroll = self.table_scroll.saturating_sub(3);
        }
    }

    fn select_next(&mut self) {
        let count = self.visible_count();
        if count == 0 {
            return;
        }
        self.selected = (self.selected + 1).min(count - 1);
        self.ensure_selected_visible();
        self.refresh_detail();
    }

    fn select_prev(&mut self) {
        if self.visible_count() == 0 {
            return;
        }
        self.selected = self.selected.saturating_sub(1);
        self.ensure_selected_visible();
        self.refresh_detail();
    }

    fn page_down(&mut self) {
        let count = self.visible_count();
        if count == 0 {
            return;
        }
        self.selected = (self.selected + usize::from(self.table_rows_hint.max(1))).min(count - 1);
        self.ensure_selected_visible();
        self.refresh_detail();
    }

    fn page_up(&mut self) {
        if self.visible_count() == 0 {
            return;
        }
        self.selected = self
            .selected
            .saturating_sub(usize::from(self.table_rows_hint.max(1)));
        self.ensure_selected_visible();
        self.refresh_detail();
    }

    fn select_first(&mut self) {
        self.selected = 0;
        self.table_scroll = 0;
        self.refresh_detail();
    }

    fn select_last(&mut self) {
        let count = self.visible_count();
        if count == 0 {
            return;
        }
        self.selected = count - 1;
        self.ensure_selected_visible();
        self.refresh_detail();
    }

    pub(crate) fn ensure_selected_visible(&mut self) {
        let rows = usize::from(self.table_rows_hint.max(1));
        let scroll = usize::from(self.table_scroll);
        if self.selected < scroll {
            self.table_scroll = self.selected.min(u16::MAX as usize) as u16;
        } else if self.selected >= scroll.saturating_add(rows) {
            let new_scroll = self.selected.saturating_add(1).saturating_sub(rows);
            self.table_scroll = new_scroll.min(u16::MAX as usize) as u16;
        }
    }

    fn clamp_selection(&mut self) {
        let count = self.visible_count();
        if count == 0 {
            self.selected = 0;
            self.table_scroll = 0;
            return;
        }
        self.selected = self.selected.min(count - 1);
        self.ensure_selected_visible();
    }

    fn toggle_detail_tab(&mut self) {
        self.detail_tab = match self.detail_tab {
            DetailTab::Journal => DetailTab::Status,
            DetailTab::Status => DetailTab::Journal,
        };
        self.detail_scroll = 0;
    }

    fn scroll_detail_up(&mut self) {
        self.detail_scroll = self.detail_scroll.saturating_sub(3);
    }

    fn scroll_detail_down(&mut self) {
        self.detail_scroll = self.detail_scroll.saturating_add(3);
    }

    fn set_lookback(&mut self, hours: u64) {
        self.cli.lookback_hours = hours;
        self.detail_scroll = 0;
        self.refresh_detail();
    }

    fn refresh_all(&mut self) {
        let selected_unit = self.selected_unit();
        self.refresh_daemons();
        if let Some(unit) = selected_unit {
            self.selected = self
                .visible_indices()
                .into_iter()
                .position(|idx| self.daemons[idx].unit == unit)
                .unwrap_or(0);
        }
        self.clamp_selection();
        self.refresh_detail();
    }

    fn refresh_daemons(&mut self) {
        match daemon::load_daemons() {
            Ok(daemons) => {
                self.daemons = daemons;
                self.clamp_selection();
                self.last_refresh = Instant::now();
                self.message = format!(
                    "loaded {} services, {} anomalies",
                    self.daemons.len(),
                    self.anomaly_count()
                );
            }
            Err(err) => {
                self.message = format!("refresh failed: {err:#}");
            }
        }
    }

    fn refresh_detail(&mut self) {
        let Some(unit) = self.selected_unit() else {
            self.detail = DetailCache::default();
            return;
        };

        if self.detail.unit != unit {
            self.detail_scroll = 0;
        }

        let status = daemon::load_status(&unit)
            .unwrap_or_else(|err| vec![format!("status error for {unit}: {err:#}")]);
        let journal = daemon::load_journal(&unit, self.cli.lookback_hours, self.cli.journal_lines)
            .unwrap_or_else(|err| vec![format!("journal error for {unit}: {err:#}")]);
        self.detail = DetailCache {
            unit,
            status,
            journal,
        };
    }

    fn auto_refresh_if_due(&mut self) {
        if self.cli.no_auto_refresh {
            return;
        }
        if self.last_refresh.elapsed() >= Duration::from_secs(self.cli.refresh_seconds.max(1)) {
            let selected_unit = self.selected_unit();
            self.refresh_daemons();
            if let Some(unit) = selected_unit {
                self.selected = self
                    .visible_indices()
                    .into_iter()
                    .position(|idx| self.daemons[idx].unit == unit)
                    .unwrap_or(self.selected);
                self.clamp_selection();
            }
        }
    }

    fn run_action(&mut self, action: DaemonAction) {
        let Some(unit) = self.selected_unit() else {
            self.message = "no service selected".to_string();
            return;
        };
        match daemon::apply_action(&unit, action) {
            Ok(report) => {
                let status = if report.ok { "ok" } else { "failed" };
                self.message = format!("{status}: {} -> {}", report.command, report.output);
            }
            Err(err) => {
                self.message = format!("{} {unit} failed: {err:#}", action.label());
            }
        }
        let action_message = self.message.clone();
        self.refresh_all();
        self.message = action_message;
    }
}

fn map_mouse_button(button: CrosstermButton) -> MouseButton {
    match button {
        CrosstermButton::Left => MouseButton::Left,
        CrosstermButton::Right => MouseButton::Right,
        CrosstermButton::Middle => MouseButton::Middle,
    }
}