helix-toolbox 1.25.7

Build, configure and optimize a Linux machine from a terminal menu: installs, dotfiles, updates, backups and health checks, every change planned and confirmed first.
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
//! The Services screen.

use super::super::*;
use super::{Ask, Cx, ToolScreen, View};

/// systemd's units and the applications started at login.
#[derive(Default)]
pub(in crate::tui::app) struct Services {
    /// The units and autostart entries, as last read.
    found: Option<services::Services>,
    pub(in crate::tui::app) rows: Vec<ServiceRow>,
    view: ServiceView,
    /// What the names shown must have.
    pub(in crate::tui::app) filter: String,
}

/// Which units the Services screen lists.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
enum ServiceView {
    #[default]
    System,
    Timers,
    User,
    Login,
    Failed,
}

impl ServiceView {
    fn next(self) -> Self {
        match self {
            ServiceView::System => ServiceView::Timers,
            ServiceView::Timers => ServiceView::User,
            ServiceView::User => ServiceView::Login,
            ServiceView::Login => ServiceView::Failed,
            ServiceView::Failed => ServiceView::System,
        }
    }

    fn label(self) -> &'static str {
        match self {
            ServiceView::System => "the machine's services",
            ServiceView::Timers => "timers and sockets",
            ServiceView::User => "your session's",
            ServiceView::Login => "started at login",
            ServiceView::Failed => "failed",
        }
    }
}

/// A line of the Services screen.
#[derive(Clone, Debug)]
pub(in crate::tui::app) enum ServiceRow {
    Heading(String),
    Unit(usize),
    Autostart(usize),
}

impl Services {
    /// Read the units and the autostart entries again; quick, so not in the
    /// background.
    fn reload(&mut self, cx: &mut Cx) {
        self.found = Some(services::gather_on(cx.host));
        self.build_rows(cx);
    }

    pub(in crate::tui::app) fn unit(&self, index: usize) -> Option<&services::Unit> {
        self.found.as_ref()?.units.get(index)
    }

    fn selected(&self, at: Option<usize>) -> Option<ServiceRow> {
        self.rows.get(at?).cloned()
    }

    /// Show only the machine's services named `name`, as opening the
    /// screen from another does.
    pub(in crate::tui::app) fn show(&mut self, name: &str) {
        self.filter = name.to_string();
        self.view = ServiceView::System;
    }

    /// What the names must have, as typed; the list starts again at the top.
    pub(in crate::tui::app) fn set_filter(&mut self, cx: &mut Cx, text: &str) {
        self.filter = text.trim().to_string();
        cx.selected.select(Some(0));
        self.build_rows(cx);
    }

    /// The rows of the chosen view, those the filter lets through.
    fn build_rows(&mut self, cx: &mut Cx) {
        let mut rows = Vec::new();
        if let Some(found) = &self.found {
            let filter = self.filter.to_lowercase();
            let wanted = |text: &str, more: &str| {
                filter.is_empty()
                    || text.to_lowercase().contains(&filter)
                    || more.to_lowercase().contains(&filter)
            };
            if self.view == ServiceView::Login {
                rows.push(ServiceRow::Heading(format!(
                    "Started at login ({})",
                    found.autostart.len()
                )));
                rows.extend(
                    found
                        .autostart
                        .iter()
                        .enumerate()
                        .filter(|(_, item)| wanted(&item.name, &item.file))
                        .map(|(index, _)| ServiceRow::Autostart(index)),
                );
            } else {
                let units: Vec<usize> = found
                    .units
                    .iter()
                    .enumerate()
                    .filter(|(_, unit)| match self.view {
                        ServiceView::System => !unit.user && unit.kind() == "service",
                        ServiceView::Timers => unit.kind() != "service",
                        ServiceView::User => unit.user && unit.kind() == "service",
                        ServiceView::Failed => unit.failed(),
                        ServiceView::Login => false,
                    })
                    .filter(|(_, unit)| wanted(&unit.name, &unit.description))
                    .map(|(index, _)| index)
                    .collect();
                rows.push(ServiceRow::Heading(format!(
                    "{} ({})",
                    match self.view {
                        ServiceView::System => "The machine's services",
                        ServiceView::Timers => "Timers and sockets",
                        ServiceView::User => "Your session's services",
                        ServiceView::Failed => "Failed units",
                        ServiceView::Login => "",
                    },
                    units.len()
                )));
                rows.extend(units.into_iter().map(ServiceRow::Unit));
            }
        }
        self.rows = rows;
        cx.clamp();
    }

    fn describe_unit(&self, cx: &mut Cx, index: usize) {
        let Some(unit) = self.unit(index).cloned() else {
            return;
        };
        let needed = services::needed_by(cx.host, &unit.name, unit.user);
        let shown = rows::unit(&unit);
        let mut text = format!(
            "{}\n\nEnabled: {}\nRunning: {}\n",
            if unit.description.is_empty() {
                "No description."
            } else {
                unit.description.as_str()
            },
            shown.enablement,
            shown.now
        );
        if !needed.is_empty() {
            text.push_str(&format!("Needed by: {}\n", needed.join(", ")));
        }
        if let Some(cost) = services::consequence(&unit.name, unit.user, cx.host.ssh()) {
            text.push_str(&format!("\nBefore stopping or disabling it: {cost}\n"));
        }
        if let Some(note) = shown.note {
            text.push_str(&format!("\n{note}\n"));
        }
        text.push_str(
            "\ns starts or stops it, e enables or disables it, R restarts it; each asks first.",
        );
        *cx.popup = Some(Popup::Text(TextPopup::new(
            format!("{}{}", if unit.user { "Your " } else { "" }, unit.name),
            text,
            true,
        )));
    }

    /// Plan starting, stopping, restarting, enabling or disabling a unit.
    fn change_unit(&self, cx: &mut Cx, index: usize, verb: Verb) {
        let Some(unit) = self.unit(index).cloned() else {
            return;
        };
        cx.confirm_or_say(services::plan_on(cx.host, &unit.name, unit.user, verb));
    }

    /// An application started at login, as the window's side pane shows it.
    fn describe_autostart(&self, cx: &mut Cx, index: usize) {
        let Some(item) = self
            .found
            .as_ref()
            .and_then(|found| found.autostart.get(index))
        else {
            return;
        };
        let shown = rows::autostart(item);
        let mut text = String::new();
        if !item.comment.is_empty() {
            text.push_str(&format!("{}\n\n", item.comment));
        }
        text.push_str(&format!(
            "Runs: {}\nFrom: {}\nAt login: {}\n\n",
            shown.runs, shown.from, shown.at_login
        ));
        text.push_str(&format!(
            "Enter plans {}, reviewed before it changes anything. For you only: the system's file stays as it is.",
            if item.enabled {
                "not starting it at login"
            } else {
                "starting it at login"
            }
        ));
        *cx.popup = Some(Popup::Text(TextPopup::new(item.name.clone(), text, true)));
    }

    /// Plan turning an autostart entry off, or on again.
    fn switch_autostart(&self, cx: &mut Cx, index: usize) {
        let Some(item) = self
            .found
            .as_ref()
            .and_then(|found| found.autostart.get(index))
            .cloned()
        else {
            return;
        };
        let dirs = services::AutostartDirs::of(cx.host);
        cx.confirm_or_say(services::plan_autostart(
            &item,
            !item.enabled,
            &dirs,
            cx.host,
        ));
    }
}

impl ToolScreen for Services {
    fn open(&mut self, cx: &mut Cx) {
        self.reload(cx);
    }

    fn len(&self, _view: &View) -> usize {
        self.rows.len()
    }

    fn key(&mut self, cx: &mut Cx, code: KeyCode) {
        match code {
            KeyCode::Enter | KeyCode::Char(' ') | KeyCode::Char('l') | KeyCode::Right => {
                match self.selected(cx.at()) {
                    Some(ServiceRow::Unit(index)) => self.describe_unit(cx, index),
                    Some(ServiceRow::Autostart(index)) => self.switch_autostart(cx, index),
                    _ => {}
                }
            }
            KeyCode::Char('d') | KeyCode::Char('D') => match self.selected(cx.at()) {
                Some(ServiceRow::Unit(index)) => self.describe_unit(cx, index),
                Some(ServiceRow::Autostart(index)) => self.describe_autostart(cx, index),
                _ => {}
            },
            KeyCode::Char('s') => {
                if let Some(ServiceRow::Unit(index)) = self.selected(cx.at())
                    && let Some(unit) = self.unit(index)
                {
                    let verb = if unit.running() {
                        Verb::Stop
                    } else {
                        Verb::Start
                    };
                    self.change_unit(cx, index, verb);
                }
            }
            KeyCode::Char('e') => {
                if let Some(ServiceRow::Unit(index)) = self.selected(cx.at())
                    && let Some(unit) = self.unit(index)
                    && unit.switchable()
                {
                    let verb = if unit.enabled() {
                        Verb::Disable
                    } else {
                        Verb::Enable
                    };
                    self.change_unit(cx, index, verb);
                }
            }
            KeyCode::Char('R') => {
                if let Some(ServiceRow::Unit(index)) = self.selected(cx.at()) {
                    self.change_unit(cx, index, Verb::Restart);
                }
            }
            KeyCode::Char('i') => {
                self.view = self.view.next();
                cx.selected.select(Some(0));
                self.build_rows(cx);
            }
            KeyCode::Char('f') => {
                *cx.popup = Some(Popup::Ask(
                    InputPopup::new(
                        "Filter",
                        "Only units and applications whose name or description has (blank for all):",
                    )
                    .with_value(self.filter.clone())
                    .answering("Filter"),
                    Asked::ServiceFilter,
                ));
            }
            KeyCode::Char('r') => self.reload(cx),
            KeyCode::Char('h') | KeyCode::Left => cx.ask(Ask::Sidebar),
            _ => {}
        }
    }

    fn hints(&self, _view: &View, at: Option<usize>, list: &mut Vec<Hint>) {
        list.push(Hint::new(&["↑↓"], "Navigate"));
        match self.selected(at) {
            Some(ServiceRow::Unit(index)) => {
                list.push(Hint::new(&["Enter"], "Details"));
                if let Some(unit) = self.unit(index) {
                    list.push(Hint::new(
                        &["s"],
                        if unit.running() { "Stop" } else { "Start" },
                    ));
                    if unit.switchable() {
                        list.push(Hint::new(
                            &["e"],
                            if unit.enabled() { "Disable" } else { "Enable" },
                        ));
                    }
                    if unit.running() {
                        list.push(Hint::new(&["R"], "Restart"));
                    }
                }
            }
            Some(ServiceRow::Autostart(_)) => {
                list.push(Hint::new(&["Enter"], "On or off at login"));
                list.push(Hint::new(&["d"], "Details"));
            }
            _ => {}
        }
        list.push(Hint::new(&["i"], "Show"));
        list.push(Hint::new(&["f"], "Filter"));
        list.push(Hint::new(&["r"], "Read again"));
    }

    fn after_run(&mut self, cx: &mut Cx, reading: Reading) {
        if reading == Reading::Tools {
            self.reload(cx);
        }
    }

    fn draw(&self, view: &View, frame: &mut Frame, area: Rect, state: &mut ListState) {
        let inner = view.page(
            frame,
            area,
            "Services",
            "systemd's units and what starts at login; each change asks first and says what it costs.",
        );
        view.fill(frame, inner);
        let focused = view.focused;
        let mut items: Vec<Line> = Vec::new();
        let mut title = format!("Showing {}", self.view.label());
        if !self.filter.is_empty() {
            title.push_str(&format!(", with \"{}\"", self.filter));
        }
        match &self.found {
            None => items.push(Line::styled(
                "Press r to read this machine's services.",
                Style::default().fg(view.theme.muted()),
            )),
            Some(found) if !found.systemd && self.view != ServiceView::Login => {
                items.push(Line::styled(
                    "systemd does not run this machine, so there are no units; i shows what starts at login.",
                    Style::default().fg(view.theme.muted()),
                ))
            }
            Some(found) => {
                let width = inner.width.saturating_sub(8) as usize;
                for row in &self.rows {
                    items.push(match row {
                        ServiceRow::Heading(text) => Line::styled(
                            text.clone(),
                            Style::default().fg(view.theme.group()).bold(),
                        ),
                        ServiceRow::Unit(index) => {
                            let unit = &found.units[*index];
                            let state = format!(
                                "{:<9} {:<9}",
                                unit.enablement.as_deref().unwrap_or("-"),
                                unit.active.as_deref().unwrap_or("-")
                            );
                            let color = if unit.failed() {
                                view.theme.failure()
                            } else if unit.running() {
                                view.theme.success()
                            } else {
                                view.theme.muted()
                            };
                            let name: String = unit.name.chars().take(40).collect();
                            let room = width.saturating_sub(42 + state.len());
                            Line::from(vec![
                                Span::styled(format!("  {name:<40} "), Style::default().fg(view.theme.entry())),
                                Span::styled(state, Style::default().fg(color)),
                                Span::styled(
                                    format!("  {}", unit.description.chars().take(room).collect::<String>()),
                                    Style::default().fg(view.theme.muted()),
                                ),
                            ])
                        }
                        ServiceRow::Autostart(index) => {
                            let item = &found.autostart[*index];
                            let word = rows::autostart(item).state;
                            let color = match (item.here, item.enabled) {
                                (false, _) => view.theme.muted(),
                                (true, true) => view.theme.success(),
                                (true, false) => view.theme.warning(),
                            };
                            Line::from(vec![
                                Span::styled(format!("  {:<36} ", item.name), Style::default().fg(view.theme.entry())),
                                Span::styled(format!("{word:<9}"), Style::default().fg(color)),
                                Span::styled(
                                    format!("  {}", item.exec.chars().take(width.saturating_sub(48)).collect::<String>()),
                                    Style::default().fg(view.theme.muted()),
                                ),
                            ])
                        }
                    });
                }
            }
        }
        let list = List::new(view.measure(items))
            .block(view.card(&title))
            .highlight_style(view.highlight(focused))
            .highlight_symbol(view.marker(focused))
            .scroll_padding(1);
        bordered_list(frame, list, inner, state, view.hits);
    }
}