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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! The Configs screen.

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

/// Ghostty's and Fastfetch's configuration, changed after review.
#[derive(Default)]
pub(in crate::tui::app) struct Configs {
    parts: Vec<ConfigPart>,
    pub(in crate::tui::app) rows: Vec<ConfigRow>,
    /// A copy waiting for the user's editor.
    edit: Option<EditRequest>,
}

/// A line of the Configs screen.
#[derive(Clone, Debug)]
pub(in crate::tui::app) enum ConfigRow {
    Heading(String),
    Text(String),
    /// A field of a configuration, by its place in the configuration's fields.
    Field(configs::Kind, usize),
    /// The whole file, in the user's editor.
    Edit(configs::Kind),
    /// A backup kept beside the file, by its place in its history.
    Backup(configs::Kind, usize),
}

/// A configuration as read: the file to edit, its text, what its checks
/// say and the backups kept beside it.
struct ConfigPart {
    kind: configs::Kind,
    found: Result<configs::Located, String>,
    loaded: Option<Result<configs::Loaded, String>>,
    checked: Option<Checked>,
    history: Vec<backups::Backup>,
    /// Changes not saved yet, as the window's draft: fields changed one by
    /// one, then checked and saved once.
    draft: Option<String>,
    /// What the checks said of the draft, once asked.
    draft_checked: Option<Checked>,
}

/// A private copy of a configuration for the user's editor, which the
/// event loop opens with the terminal handed back.
pub struct EditRequest {
    pub copy: PathBuf,
    pub editor: String,
    located: configs::Located,
    loaded: configs::Loaded,
}

/// The private folder for copies, beside the host's history; never a
/// shared one.
fn copies_folder(host: &Host) -> Option<PathBuf> {
    host.state().map(|state| state.join("configs"))
}

/// Ghostty's own check, which runs this machine's Ghostty; none on a
/// made-up host.
fn check_on(host: &Host) -> fn(&Path) -> Option<(bool, String)> {
    if host.is_here() {
        configs::ghostty_here
    } else {
        |_| None
    }
}

fn cannot(cx: &mut Cx, why: String) {
    *cx.popup = Some(Popup::Text(TextPopup::new("Cannot change it", why, true)));
}

fn no_copies_folder(cx: &mut Cx) {
    cannot(
        cx,
        "There is no state folder for Helix Toolbox's private copies; nothing is saved.".into(),
    );
}

impl Configs {
    /// Read both configurations, their checks and their backups.
    fn read(&mut self, cx: &mut Cx) {
        let check = check_on(cx.host);
        self.parts = configs::Kind::ALL
            .into_iter()
            .map(|kind| {
                let found = configs::locate_on(cx.host, kind);
                let (loaded, checked, history) = match &found {
                    Ok(located) => {
                        let loaded = configs::read(located);
                        let checked = loaded.as_ref().ok().map(|loaded| {
                            configs::validate(
                                kind,
                                &loaded.content,
                                &check,
                                copies_folder(cx.host).as_deref(),
                            )
                        });
                        (Some(loaded), checked, configs::history(located))
                    }
                    Err(_) => (None, None, Vec::new()),
                };
                ConfigPart {
                    kind,
                    found,
                    loaded,
                    checked,
                    history,
                    draft: None,
                    draft_checked: None,
                }
            })
            .collect();
        self.build_rows(cx);
    }

    fn part(&self, kind: configs::Kind) -> Option<&ConfigPart> {
        self.parts.iter().find(|part| part.kind == kind)
    }

    /// The configuration ready to change: where it is and its text.
    fn ready(&self, kind: configs::Kind) -> Result<(configs::Located, configs::Loaded), String> {
        let part = self.part(kind).ok_or("it has not been read")?;
        let located = part.found.clone()?;
        let loaded = part.loaded.clone().ok_or("it has not been read")??;
        Ok((located, loaded))
    }

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

    /// The configuration the selected row belongs to.
    fn selected_kind(&self, at: Option<usize>) -> Option<configs::Kind> {
        let at = at?;
        self.rows[..=at.min(self.rows.len().saturating_sub(1))]
            .iter()
            .rev()
            .find_map(|row| match row {
                ConfigRow::Field(kind, _) | ConfigRow::Edit(kind) | ConfigRow::Backup(kind, _) => {
                    Some(*kind)
                }
                ConfigRow::Heading(text) => configs::Kind::ALL
                    .into_iter()
                    .find(|kind| text.starts_with(kind.label())),
                ConfigRow::Text(_) => None,
            })
    }

    fn ask_field(&self, cx: &mut Cx, kind: configs::Kind, index: usize) {
        let Some(field) = kind.fields().get(index) else {
            return;
        };
        let (_, loaded) = match self.ready(kind) {
            Ok(ready) => ready,
            Err(why) => return cannot(cx, why),
        };
        let text = self
            .part(kind)
            .and_then(|part| part.draft.clone())
            .unwrap_or(loaded.content);
        let now = configs::values(kind, &text)
            .into_iter()
            .find(|(key, _)| *key == field.key)
            .and_then(|(_, value)| value)
            .unwrap_or_default();
        *cx.popup = Some(Popup::Ask(
            InputPopup::new(
                format!("{}: {}", kind.label(), field.label),
                format!("{} ({}):", field.key, field.hint),
            )
            .with_value(now)
            .answering("Change"),
            Asked::ConfigField(kind, field.key),
        ));
    }

    /// Change a field in the draft; s saves the draft, after review.
    pub(in crate::tui::app) fn set_config(
        &mut self,
        cx: &mut Cx,
        kind: configs::Kind,
        key: &str,
        value: &str,
    ) {
        let (_, loaded) = match self.ready(kind) {
            Ok(ready) => ready,
            Err(why) => return cannot(cx, why),
        };
        let text = self
            .part(kind)
            .and_then(|part| part.draft.clone())
            .unwrap_or_else(|| loaded.content.clone());
        match configs::set(kind, &text, key, value) {
            Ok(after) => self.set_draft(cx, kind, &loaded, after),
            Err(why) => cannot(cx, why),
        }
    }

    /// Keep text as the draft, or none when it is the file as it is.
    fn set_draft(
        &mut self,
        cx: &mut Cx,
        kind: configs::Kind,
        loaded: &configs::Loaded,
        after: String,
    ) {
        if let Some(part) = self.parts.iter_mut().find(|part| part.kind == kind) {
            part.draft = (after != loaded.content).then_some(after);
            part.draft_checked = None;
        }
        self.build_rows(cx);
    }

    /// Check the draft, or the file when there is none, as the window's Check.
    fn check(&mut self, cx: &mut Cx, kind: configs::Kind) {
        let (_, loaded) = match self.ready(kind) {
            Ok(ready) => ready,
            Err(why) => return cannot(cx, why),
        };
        let check = check_on(cx.host);
        let folder = copies_folder(cx.host);
        if let Some(part) = self.parts.iter_mut().find(|part| part.kind == kind) {
            let checked = configs::validate(
                kind,
                part.draft.as_deref().unwrap_or(&loaded.content),
                &check,
                folder.as_deref(),
            );
            if part.draft.is_some() {
                part.draft_checked = Some(checked);
            } else {
                part.checked = Some(checked);
            }
        }
        self.build_rows(cx);
    }

    /// Review saving the draft.
    fn save_draft(&self, cx: &mut Cx, kind: configs::Kind) {
        let (located, loaded) = match self.ready(kind) {
            Ok(ready) => ready,
            Err(why) => return cannot(cx, why),
        };
        let draft = self
            .part(kind)
            .and_then(|part| part.draft.clone())
            .unwrap_or_else(|| loaded.content.clone());
        save(cx, &located, &loaded, &draft);
    }

    /// Go back to the file as it was read.
    fn discard_draft(&mut self, cx: &mut Cx, kind: configs::Kind) {
        if let Some(part) = self.parts.iter_mut().find(|part| part.kind == kind) {
            part.draft = None;
            part.draft_checked = None;
        }
        self.build_rows(cx);
    }

    /// A private copy of the file for the user's editor, opened when the
    /// key has been handled.
    fn start_edit(&mut self, cx: &mut Cx, kind: configs::Kind) {
        let (located, loaded) = match self.ready(kind) {
            Ok(ready) => ready,
            Err(why) => return cannot(cx, why),
        };
        let Some(folder) = copies_folder(cx.host) else {
            return no_copies_folder(cx);
        };
        // The draft, when there is one, is what the editor starts from.
        let text = self
            .part(kind)
            .and_then(|part| part.draft.clone())
            .unwrap_or_else(|| loaded.content.clone());
        let copy = match configs::private_copy(kind, "edit", &text, &folder) {
            Ok(copy) => copy,
            Err(why) => return cannot(cx, why),
        };
        let editor = ["VISUAL", "EDITOR"]
            .iter()
            .filter_map(|name| std::env::var(name).ok())
            .find(|editor| !editor.trim().is_empty())
            .unwrap_or_else(|| "nano".to_string());
        self.edit = Some(EditRequest {
            copy,
            editor,
            located,
            loaded,
        });
    }

    /// The copy to open in the user's editor, once.
    pub(in crate::tui::app) fn take_edit(&mut self) -> Option<EditRequest> {
        self.edit.take()
    }

    /// The editor closed: check the copy, and review saving it.
    pub(in crate::tui::app) fn edited(
        &mut self,
        cx: &mut Cx,
        request: EditRequest,
        closed: Result<(), String>,
    ) {
        let after = std::fs::read_to_string(&request.copy);
        let _ = std::fs::remove_file(&request.copy);
        if let Err(why) = closed {
            return cannot(cx, format!("{why}; nothing is saved."));
        }
        match after {
            Ok(after) => {
                // What came back is the draft, and its save is reviewed.
                self.set_draft(cx, request.located.kind, &request.loaded, after.clone());
                save(cx, &request.located, &request.loaded, &after)
            }
            Err(error) => cannot(
                cx,
                format!("The edited copy cannot be read: {error}; nothing is saved."),
            ),
        }
    }

    /// Put a backup back, after review: the file there now is kept too.
    fn restore(&self, cx: &mut Cx, kind: configs::Kind, index: usize) {
        let Some(backup) = self
            .part(kind)
            .and_then(|part| part.history.get(index))
            .cloned()
        else {
            return;
        };
        cx.confirm(backups::plan_restore(&backup));
    }

    fn describe(&self, cx: &mut Cx) {
        let Some(kind) = self.selected_kind(cx.at()) else {
            return;
        };
        let Some(part) = self.part(kind) else {
            return;
        };
        let text = match (&part.found, &part.loaded) {
            (Err(why), _) => format!("{why}."),
            (Ok(located), loaded) => {
                let mut text = format!(
                    "{}, named by {}.\n\n{} reads {}{}.",
                    helix_toolbox_core::printable(&located.path.display().to_string()),
                    located.origin.label(),
                    kind.label(),
                    helix_toolbox_core::printable(&located.live.display().to_string()),
                    if located.managed {
                        ", a Home Manager link into the Nix store"
                    } else {
                        ""
                    }
                );
                if let Some(Ok(loaded)) = loaded {
                    text.push_str(&format!(
                        "\n\n{} bytes; SHA-256 {}.",
                        loaded.content.len(),
                        loaded.revision
                    ));
                }
                if let Some(flake) = &located.flake {
                    text.push_str(&format!(
                        "\n\nIt is part of the Home Manager flake in {}: run Home Manager after a change to make it live.",
                        helix_toolbox_core::printable(&flake.display().to_string())
                    ));
                }
                text.push_str("\n\nA save keeps the file there now as a backup beside it, which Backups and helix-toolbox restore can put back.");
                text
            }
        };
        *cx.popup = Some(Popup::Text(TextPopup::new(
            format!("{}'s configuration", kind.label()),
            text,
            true,
        )));
    }

    fn build_rows(&mut self, cx: &mut Cx) {
        let mut rows = Vec::new();
        for part in &self.parts {
            let kind = part.kind;
            let located = match &part.found {
                Ok(located) => located,
                Err(why) => {
                    rows.push(ConfigRow::Heading(kind.label().to_string()));
                    rows.push(ConfigRow::Text(format!("{why}.")));
                    continue;
                }
            };
            rows.push(ConfigRow::Heading(format!(
                "{}  {}",
                kind.label(),
                located.path.display()
            )));
            let mut about = format!("named by {}", located.origin.label());
            if located.managed {
                about.push_str("; the live file is a Home Manager link");
            }
            if located.flake.is_some() {
                about.push_str("; run Home Manager after a change");
            }
            rows.push(ConfigRow::Text(about));
            match &part.loaded {
                Some(Err(why)) => {
                    rows.push(ConfigRow::Text(format!("{why}.")));
                    continue;
                }
                Some(Ok(loaded)) if !loaded.exists => {
                    rows.push(ConfigRow::Text(
                        "not there yet; a change makes it".to_string(),
                    ));
                }
                _ => {}
            }
            if let Some(checked) = &part.checked {
                rows.push(ConfigRow::Text(match checked {
                    Checked::Valid => "Checks: valid".to_string(),
                    Checked::Unchecked(why) | Checked::Invalid(why) => {
                        format!("Checks: {}: {why}", checked.label())
                    }
                }));
            }
            if part.draft.is_some() {
                rows.push(ConfigRow::Text(
                    "Draft: changed, not saved yet. c checks it, s saves it after review, x discards it."
                        .to_string(),
                ));
                rows.push(ConfigRow::Text(match &part.draft_checked {
                    None => "Draft checks: not checked yet".to_string(),
                    Some(Checked::Valid) => "Draft checks: valid".to_string(),
                    Some(checked @ (Checked::Unchecked(why) | Checked::Invalid(why))) => {
                        format!("Draft checks: {}: {why}", checked.label())
                    }
                }));
            }
            rows.extend((0..kind.fields().len()).map(|index| ConfigRow::Field(kind, index)));
            rows.push(ConfigRow::Edit(kind));
            if !part.history.is_empty() {
                rows.push(ConfigRow::Text(format!(
                    "backups kept ({}), newest first:",
                    part.history.len()
                )));
                rows.extend((0..part.history.len()).map(|index| ConfigRow::Backup(kind, index)));
            }
        }
        self.rows = rows;
        cx.clamp();
    }
}

/// Check new text and review the plan that saves it.
fn save(cx: &mut Cx, located: &configs::Located, loaded: &configs::Loaded, after: &str) {
    let Some(folder) = copies_folder(cx.host) else {
        return no_copies_folder(cx);
    };
    let checked = configs::validate(located.kind, after, &check_on(cx.host), Some(&folder));
    if let Checked::Invalid(why) = &checked {
        return cannot(cx, format!("Not saved: {why}."));
    }
    if loaded.content == after {
        *cx.popup = Some(Popup::Text(TextPopup::new(
            "Nothing changed",
            "The file is as it was; nothing is saved.",
            true,
        )));
        return;
    }
    match configs::plan_save(located, &loaded.revision, after, &checked, &folder) {
        Ok(planned) => cx.confirm(planned),
        Err(why) => cannot(cx, why),
    }
}

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

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

    fn key(&mut self, cx: &mut Cx, code: KeyCode) {
        let kind = self.selected_kind(cx.at());
        match code {
            KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => match self.selected(cx.at()) {
                Some(ConfigRow::Field(kind, index)) => self.ask_field(cx, kind, index),
                Some(ConfigRow::Edit(kind)) => self.start_edit(cx, kind),
                Some(ConfigRow::Backup(kind, index)) => self.restore(cx, kind, index),
                _ => self.describe(cx),
            },
            KeyCode::Char('E') => {
                if let Some(kind) = kind {
                    self.start_edit(cx, kind);
                }
            }
            KeyCode::Char('d') => self.describe(cx),
            KeyCode::Char('c') => {
                if let Some(kind) = kind {
                    self.check(cx, kind);
                }
            }
            KeyCode::Char('s') => {
                if let Some(kind) = kind {
                    self.save_draft(cx, kind);
                }
            }
            KeyCode::Char('x') => {
                if let Some(kind) = kind {
                    self.discard_draft(cx, kind);
                }
            }
            KeyCode::Char('v') | KeyCode::Char('r') => self.read(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(ConfigRow::Field(..)) => list.push(Hint::new(&["Enter"], "Change")),
            Some(ConfigRow::Backup(..)) => list.push(Hint::new(&["Enter"], "Put back")),
            _ => {}
        }
        if let Some(kind) = self.selected_kind(at) {
            list.push(Hint::new(&["c"], "Check"));
            if self.part(kind).is_some_and(|part| part.draft.is_some()) {
                list.push(Hint::new(&["s"], "Save"));
                list.push(Hint::new(&["x"], "Discard changes"));
            }
            list.push(Hint::new(&["E"], "Edit in your editor"));
        }
        list.push(Hint::new(&["d"], "Details"));
        list.push(Hint::new(&["r"], "Read again"));
    }

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

    fn draw(&self, view: &View, frame: &mut Frame, area: Rect, state: &mut ListState) {
        let inner = view.page(
            frame,
            area,
            "Configs",
            "Ghostty's and Fastfetch's configuration. Each change is checked and shown before it is saved, and the file there now is kept as a backup.",
        );
        view.fill(frame, inner);
        let focused = view.focused;
        let muted = Style::default().fg(view.theme.muted());
        let mut items: Vec<Line> = Vec::new();
        for row in &self.rows {
            items.push(match row {
                ConfigRow::Heading(text) => Line::styled(
                    helix_toolbox_core::printable(text),
                    Style::default().fg(view.theme.group()).bold(),
                ),
                ConfigRow::Text(text) => {
                    let color = if text.starts_with("Checks: Invalid")
                        || text.starts_with("Draft checks: Invalid")
                    {
                        view.theme.failure()
                    } else {
                        view.theme.muted()
                    };
                    Line::styled(
                        format!("  {}", helix_toolbox_core::printable(text)),
                        Style::default().fg(color),
                    )
                }
                ConfigRow::Field(kind, index) => {
                    let field = &kind.fields()[*index];
                    // The draft's value, when there is a draft.
                    let value = self.part(*kind).and_then(|part| {
                        let text = match &part.draft {
                            Some(draft) => draft.as_str(),
                            None => part.loaded.as_ref()?.as_ref().ok()?.content.as_str(),
                        };
                        configs::values(*kind, text)
                            .into_iter()
                            .find(|(key, _)| *key == field.key)?
                            .1
                    });
                    Line::from(vec![
                        Span::styled(
                            format!("  {:<26}", field.label),
                            Style::default().fg(view.theme.entry()),
                        ),
                        Span::styled(
                            value
                                .as_deref()
                                .map(helix_toolbox_core::printable)
                                .unwrap_or_else(|| "(not set)".to_string()),
                            muted,
                        ),
                    ])
                }
                ConfigRow::Edit(_) => Line::styled(
                    "  Edit the whole file in your editor…".to_string(),
                    Style::default().fg(view.theme.entry()),
                ),
                ConfigRow::Backup(kind, index) => {
                    let backup = self.part(*kind).and_then(|part| part.history.get(*index));
                    Line::from(vec![
                        Span::styled(
                            format!(
                                "    {}  ",
                                backup
                                    .and_then(|backup| backup.taken.clone())
                                    .unwrap_or_default()
                            ),
                            Style::default().fg(view.theme.entry()),
                        ),
                        Span::styled(
                            backup
                                .map(|backup| {
                                    helix_toolbox_core::printable(
                                        &backup.backup.display().to_string(),
                                    )
                                })
                                .unwrap_or_default(),
                            muted,
                        ),
                    ])
                }
            });
        }
        let list = List::new(view.measure(items))
            .block(view.card("Configs"))
            .highlight_style(view.highlight(focused))
            .highlight_symbol(view.marker(focused))
            .scroll_padding(1);
        bordered_list(frame, list, inner, state, view.hits);
    }
}