helix-toolbox 1.24.16

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
//! The Configs screen.

use super::super::*;

impl App {
    /// Where a configuration is: in the tests' folders, else this machine's.
    fn locate_config(&self, kind: configs::Kind) -> Result<configs::Located, String> {
        match &self.config_places {
            Some((home, config)) => configs::locate(
                kind,
                &configs::Facts {
                    home,
                    config_home: config,
                    settings: None,
                    command_center: None,
                    store: Path::new("/nix/store"),
                },
            ),
            None if self.examine_here => configs::here(kind),
            None => Err("configurations are not read here".to_string()),
        }
    }

    /// The private folder for copies: the tests' own, else beside the
    /// history; never a shared one.
    fn copies_folder(&self) -> Option<PathBuf> {
        self.config_folder.clone().or_else(configs::private_folder)
    }

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

    /// Ghostty's own check on this machine; none in tests.
    fn config_check(&self) -> fn(&Path) -> Option<(bool, String)> {
        if self.examine_here {
            configs::ghostty_here
        } else {
            |_| None
        }
    }

    /// Read both configurations, their checks and their backups.
    pub(in crate::tui::app) fn read_configs(&mut self) {
        let check = self.config_check();
        self.config_parts = configs::Kind::ALL
            .into_iter()
            .map(|kind| {
                let found = self.locate_config(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,
                                self.copies_folder().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_config_rows();
    }

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

    /// The configuration ready to change: where it is and its text.
    fn config_ready(
        &self,
        kind: configs::Kind,
    ) -> Result<(configs::Located, configs::Loaded), String> {
        let part = self.config_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_config_row(&self) -> Option<ConfigRow> {
        self.config_rows.get(self.main_state.selected()?).cloned()
    }

    /// The configuration the selected row belongs to.
    fn selected_config_kind(&self) -> Option<configs::Kind> {
        let at = self.main_state.selected()?;
        self.config_rows[..=at.min(self.config_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,
            })
    }

    pub(in crate::tui::app) fn config_key(&mut self, code: KeyCode) {
        match code {
            KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
                match self.selected_config_row() {
                    Some(ConfigRow::Field(kind, index)) => self.ask_config_field(kind, index),
                    Some(ConfigRow::Edit(kind)) => self.start_edit(kind),
                    Some(ConfigRow::Backup(kind, index)) => self.restore_config(kind, index),
                    _ => self.describe_config(),
                }
            }
            KeyCode::Char('E') => {
                if let Some(kind) = self.selected_config_kind() {
                    self.start_edit(kind);
                }
            }
            KeyCode::Char('d') => self.describe_config(),
            KeyCode::Char('c') => {
                if let Some(kind) = self.selected_config_kind() {
                    self.check_config(kind);
                }
            }
            KeyCode::Char('s') => {
                if let Some(kind) = self.selected_config_kind() {
                    self.save_draft(kind);
                }
            }
            KeyCode::Char('x') => {
                if let Some(kind) = self.selected_config_kind() {
                    self.discard_draft(kind);
                }
            }
            KeyCode::Char('v') | KeyCode::Char('r') => self.read_configs(),
            KeyCode::Char('h') | KeyCode::Left => self.focus = Focus::Sidebar,
            _ => {}
        }
    }

    pub(in crate::tui::app) fn config_hints(&self, list: &mut Vec<Hint>) {
        list.push(Hint::new(&["↑↓"], "Navigate"));
        match self.selected_config_row() {
            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_config_kind() {
            list.push(Hint::new(&["c"], "Check"));
            if self
                .config_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 cannot_config(&mut self, why: String) {
        self.popup = Some(Popup::Text(TextPopup::new("Cannot change it", why, true)));
    }

    fn ask_config_field(&mut self, kind: configs::Kind, index: usize) {
        let Some(field) = kind.fields().get(index) else {
            return;
        };
        let (_, loaded) = match self.config_ready(kind) {
            Ok(ready) => ready,
            Err(why) => return self.cannot_config(why),
        };
        let text = self
            .config_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();
        self.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, kind: configs::Kind, key: &str, value: &str) {
        let (_, loaded) = match self.config_ready(kind) {
            Ok(ready) => ready,
            Err(why) => return self.cannot_config(why),
        };
        let text = self
            .config_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(kind, &loaded, after),
            Err(why) => self.cannot_config(why),
        }
    }

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

    /// Check the draft, or the file when there is none, as the window's Check.
    fn check_config(&mut self, kind: configs::Kind) {
        let (_, loaded) = match self.config_ready(kind) {
            Ok(ready) => ready,
            Err(why) => return self.cannot_config(why),
        };
        let check = self.config_check();
        let folder = self.copies_folder();
        if let Some(part) = self.config_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_config_rows();
    }

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

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

    /// Check new text and review the plan that saves it.
    fn save_config(&mut self, located: &configs::Located, loaded: &configs::Loaded, after: &str) {
        let Some(folder) = self.copies_folder() else {
            return self.no_copies_folder();
        };
        let checked = configs::validate(located.kind, after, &self.config_check(), Some(&folder));
        if let Checked::Invalid(why) = &checked {
            return self.cannot_config(format!("Not saved: {why}."));
        }
        if loaded.content == after {
            self.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) => self.confirm_plan(Ok(planned)),
            Err(why) => self.cannot_config(why),
        }
    }

    /// A private copy of the file for the user's editor, opened when the
    /// key has been handled.
    fn start_edit(&mut self, kind: configs::Kind) {
        let (located, loaded) = match self.config_ready(kind) {
            Ok(ready) => ready,
            Err(why) => return self.cannot_config(why),
        };
        let Some(folder) = self.copies_folder() else {
            return self.no_copies_folder();
        };
        // The draft, when there is one, is what the editor starts from.
        let text = self
            .config_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 self.cannot_config(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_request = Some(EditRequest {
            copy,
            editor,
            located,
            loaded,
        });
    }

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

    /// The editor closed: check the copy, and review saving it.
    pub fn edited(&mut self, 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 self.cannot_config(format!("{why}; nothing is saved."));
        }
        match after {
            Ok(after) => {
                // What came back is the draft, and its save is reviewed.
                self.set_draft(request.located.kind, &request.loaded, after.clone());
                self.save_config(&request.located, &request.loaded, &after)
            }
            Err(error) => self.cannot_config(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_config(&mut self, kind: configs::Kind, index: usize) {
        let Some(backup) = self
            .config_part(kind)
            .and_then(|part| part.history.get(index))
            .cloned()
        else {
            return;
        };
        self.confirm_plan(Ok(backups::plan_restore(&backup)));
    }

    fn describe_config(&mut self) {
        let Some(kind) = self.selected_config_kind() else {
            return;
        };
        let Some(part) = self.config_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
            }
        };
        self.popup = Some(Popup::Text(TextPopup::new(
            format!("{}'s configuration", kind.label()),
            text,
            true,
        )));
    }

    fn build_config_rows(&mut self) {
        let mut rows = Vec::new();
        for part in &self.config_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.config_rows = rows;
        self.clamp_main();
    }

    pub(in crate::tui::app) fn draw_configs(&mut self, frame: &mut Frame, area: Rect) {
        let inner = self.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.",
        );
        self.fill(frame, inner);
        let focused = self.focus == Focus::Main;
        let muted = Style::default().fg(self.theme.muted());
        let mut items: Vec<Line> = Vec::new();
        for row in &self.config_rows {
            items.push(match row {
                ConfigRow::Heading(text) => Line::styled(
                    helix_toolbox_core::printable(text),
                    Style::default().fg(self.theme.group()).bold(),
                ),
                ConfigRow::Text(text) => {
                    let color = if text.starts_with("Checks: Invalid")
                        || text.starts_with("Draft checks: Invalid")
                    {
                        self.theme.failure()
                    } else {
                        self.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.config_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(self.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(self.theme.entry()),
                ),
                ConfigRow::Backup(kind, index) => {
                    let backup = self
                        .config_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(self.theme.entry()),
                        ),
                        Span::styled(
                            backup
                                .map(|backup| {
                                    helix_toolbox_core::printable(
                                        &backup.backup.display().to_string(),
                                    )
                                })
                                .unwrap_or_default(),
                            muted,
                        ),
                    ])
                }
            });
        }
        let list = List::new(self.measure(items))
            .block(self.card("Configs"))
            .highlight_style(self.highlight(focused))
            .highlight_symbol(self.marker(focused))
            .scroll_padding(1);
        bordered_list(frame, list, inner, &mut self.main_state, &self.hits);
    }
}