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

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

/// What this machine has, to take elsewhere, and a migration brought here.
pub(in crate::tui::app) struct Migration {
    /// What a migration of this machine would hold, as last read.
    pub(in crate::tui::app) here: Option<migration::Migration>,
    reading: Read<(migration::Migration, bool)>,
    /// Whether the packages installed by hand can be told apart here.
    pub(in crate::tui::app) packages_known: bool,
    /// The migration file opened, and what it means here.
    pub(in crate::tui::app) opened: Option<(PathBuf, Translation)>,
    opening: Read<MigrationOpened>,
    /// Why the migration file asked for could not be opened, answered after
    /// the screen was left.
    unopened: Option<String>,
    /// A run while another screen was shown left what it read out of
    /// date.
    stale: bool,
    pub(in crate::tui::app) rows: Vec<MigrationRow>,
}

impl Default for Migration {
    fn default() -> Self {
        Self {
            here: None,
            reading: Read::default(),
            packages_known: true,
            opened: None,
            opening: Read::default(),
            unopened: None,
            stale: false,
            rows: Vec::new(),
        }
    }
}

/// A migration file read here, or why it could not be.
type MigrationOpened = (PathBuf, Result<Translation, String>);

/// A line of the Migration screen.
#[derive(Clone, Debug)]
pub(in crate::tui::app) enum MigrationRow {
    Heading(String),
    Text(String),
    /// A thing the opened migration names, by its place in the translation.
    Item(usize),
}

/// How the Migration screen names a kind of thing.
fn migration_kind(kind: migration::Kind) -> &'static str {
    match kind {
        migration::Kind::Entry => "entry",
        migration::Kind::Flatpak => "Flatpak",
        migration::Kind::Package => "package",
    }
}

impl Migration {
    /// Read in the background what a migration of this machine would hold.
    fn read(&mut self, cx: &mut Cx) {
        let entries: Vec<Entry> = cx.catalog.entries().into_iter().cloned().collect();
        let platform = cx.platform.clone();
        self.reading.start(cx.host, move |host| {
            let refs: Vec<&Entry> = entries.iter().collect();
            // As migration::gather, keeping whether the packages chosen by
            // hand could be read.
            let inventory = Inventory::gather(&host, &platform);
            let chosen = migration::chosen(&host, platform.manager);
            let found =
                migration::export(&refs, &platform, &inventory, chosen.as_deref(), host.now());
            (found, chosen.is_some())
        });
        self.build_rows(cx);
    }

    /// Read this machine again, and the opened migration with it.
    fn reload(&mut self, cx: &mut Cx) {
        self.read(cx);
        if let Some((path, _)) = self.opened.clone() {
            self.open_file(cx, path);
        }
    }

    /// Read a migration file and what it means here, in the background.
    pub(in crate::tui::app) fn open_file(&mut self, cx: &mut Cx, path: PathBuf) {
        let entries: Vec<Entry> = cx.catalog.entries().into_iter().cloned().collect();
        let platform = cx.platform.clone();
        self.opening.start(cx.host, move |host| {
            let refs: Vec<&Entry> = entries.iter().collect();
            let read = migration::read_here(&host, &path, &refs, &platform)
                .map_err(|error| error.to_string());
            (path, read)
        });
        self.build_rows(cx);
    }

    /// The migration file read, or why it could not be.
    fn file_read(&mut self, cx: &mut Cx, path: PathBuf, read: Result<Translation, String>) {
        match read {
            Ok(translation) => {
                self.opened = Some((path, translation));
                self.build_rows(cx);
            }
            Err(error) => self.not_opened(cx, error),
        }
    }

    /// The migration file could not be opened. The popup is the screen
    /// shown: when it was left, why waits until it is shown again.
    fn not_opened(&mut self, cx: &mut Cx, error: String) {
        self.build_rows(cx);
        if cx.screen == Screen::Migration {
            Self::cannot_open(cx, error);
        } else {
            self.unopened = Some(error);
        }
    }

    fn cannot_open(cx: &mut Cx, error: String) {
        *cx.popup = Some(Popup::Text(TextPopup::new(
            "Cannot open the migration",
            error,
            true,
        )));
    }

    /// Write what this machine has to a file, as `helix-toolbox migrate
    /// export` does.
    pub(in crate::tui::app) fn export(&self, cx: &Cx, text: &str) -> Result<String, String> {
        let path = typed_path(text)?;
        if path.extension().is_none_or(|extension| extension != "toml") {
            return Err("A migration is written to a .toml file.".to_string());
        }
        let found = match &self.here {
            Some(found) => found.clone(),
            None => migration::gather(cx.host, &cx.catalog.entries(), cx.platform, cx.host.now()),
        };
        migration::write(&path, &found).map_err(|error| error.to_string())?;
        Ok(format!(
            "{} entries, {} Flatpaks and {} packages of yours are written to {}.\n\nOn the other machine: o in Migration opens it, or helix-toolbox migrate show.",
            found.entries.len(),
            found.flatpaks.len(),
            found.packages.len(),
            path.display()
        ))
    }

    /// Plan what the opened migration marks Install, to review and run.
    fn apply(&self, cx: &mut Cx) {
        let Some((_, translation)) = &self.opened else {
            *cx.popup = Some(Popup::Text(TextPopup::new(
                "No migration open",
                "o opens a migration file made on the other machine; then a installs what it can here.",
                true,
            )));
            return;
        };
        if translation.count(Standing::Install) == 0 {
            *cx.popup = Some(Popup::Text(TextPopup::new(
                "Nothing to install",
                "Everything the migration names is here already, cannot be installed here, or is left to you.",
                true,
            )));
            return;
        }
        let entries = cx.catalog.entries();
        let planned = migration::plan_here(cx.host, translation, &entries, cx.platform);
        cx.ask(Ask::Plan(planned));
    }

    fn selected(&self, at: Option<usize>) -> Option<&migration::Item> {
        let MigrationRow::Item(index) = self.rows.get(at?)? else {
            return None;
        };
        self.opened.as_ref()?.1.items.get(*index)
    }

    fn describe(&self, cx: &mut Cx) {
        let Some(item) = self.selected(cx.at()) else {
            return;
        };
        let kind = migration_kind(item.kind);
        let next = match item.standing {
            Standing::Install => "a installs it with the rest, after you review the plan.",
            Standing::Here => "Nothing to do: it is here already.",
            Standing::Unavailable => "Helix Toolbox has no way to install it here.",
            Standing::Yours => {
                "It is left to you: install what this distribution calls it, if you want it."
            }
        };
        let text = format!(
            "{} ({kind}): {}.\n\n{next}",
            helix_toolbox_core::printable(&item.id),
            helix_toolbox_core::printable(&item.detail)
        );
        *cx.popup = Some(Popup::Text(TextPopup::new(
            helix_toolbox_core::printable(&item.name),
            text,
            true,
        )));
    }

    fn build_rows(&mut self, cx: &mut Cx) {
        let mut rows = vec![MigrationRow::Heading("This machine".into())];
        rows.push(MigrationRow::Text(match &self.here {
            Some(found) => format!(
                "{} entries, {} Flatpaks and {} packages of yours, from {}",
                found.entries.len(),
                found.flatpaks.len(),
                found.packages.len(),
                found.from
            ),
            None if self.reading.busy() => "Reading what this machine has…".into(),
            None if self.reading.failed() => {
                "Could not read what this machine has. r reads again.".into()
            }
            None => "Press r to read what this machine has.".into(),
        }));
        if self.here.is_some() && !self.packages_known {
            rows.push(MigrationRow::Text(
                "The packages you installed yourself cannot be told apart here, so only the entries and Flatpaks go in the file.".into(),
            ));
        }
        rows.push(MigrationRow::Text(
            "e writes it to a file to take to another machine; o opens one made elsewhere.".into(),
        ));
        if self.opening.busy() {
            rows.push(MigrationRow::Heading("Opening the migration…".into()));
        }
        if let Some((path, translation)) = &self.opened {
            let read = &translation.migration;
            rows.push(MigrationRow::Heading(format!(
                "From {} ({}), made {}{}",
                read.from,
                read.architecture,
                timestamp(read.made),
                if translation.same_family {
                    ", the same family"
                } else {
                    ""
                }
            )));
            rows.push(MigrationRow::Text(path.display().to_string()));
            for standing in [
                Standing::Install,
                Standing::Here,
                Standing::Unavailable,
                Standing::Yours,
            ] {
                let count = translation.count(standing);
                if count == 0 {
                    continue;
                }
                rows.push(MigrationRow::Heading(format!(
                    "{} ({count})",
                    match standing {
                        Standing::Install => "To install",
                        Standing::Here => "Here already",
                        Standing::Unavailable => "Cannot be installed here",
                        Standing::Yours => "Left to you",
                    }
                )));
                rows.extend(
                    translation
                        .items
                        .iter()
                        .enumerate()
                        .filter(|(_, item)| item.standing == standing)
                        .map(|(index, _)| MigrationRow::Item(index)),
                );
            }
        }
        self.rows = rows;
        cx.clamp();
    }
}

impl ToolScreen for Migration {
    fn open(&mut self, cx: &mut Cx) {
        if std::mem::take(&mut self.stale) {
            self.reload(cx);
        } else if self.here.is_none() {
            self.read(cx);
        } else {
            self.build_rows(cx);
        }
        if let Some(error) = self.unopened.take() {
            Self::cannot_open(cx, error);
        }
    }

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

    fn key(&mut self, cx: &mut Cx, code: KeyCode) {
        match code {
            KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => match self.selected(cx.at()) {
                Some(item) if item.standing == Standing::Install => self.apply(cx),
                Some(_) => self.describe(cx),
                None => {}
            },
            KeyCode::Char('d') => self.describe(cx),
            KeyCode::Char('e') => {
                let name = self
                    .here
                    .as_ref()
                    .map(migration::file_name)
                    .unwrap_or_else(|| "helix-toolbox-migration.toml".to_string());
                *cx.popup = Some(Popup::Ask(
                    InputPopup::new(
                        "Export this machine",
                        "The file to write its entries, Flatpaks and packages to, to take to the other machine:",
                    )
                    .with_value(format!("~/{name}"))
                    .path()
                    .answering("Export"),
                    Asked::ExportMigration,
                ));
            }
            KeyCode::Char('o') => {
                let mut ask = InputPopup::new(
                    "Open a migration",
                    "The migration file made on the other machine, such as ~/helix-toolbox-migration.toml:",
                )
                .path()
                .answering("Open");
                if let Some((path, _)) = &self.opened {
                    ask = ask.with_value(path.display().to_string());
                }
                *cx.popup = Some(Popup::Ask(ask, Asked::OpenMigration));
            }
            KeyCode::Char('a') => self.apply(cx),
            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"));
        if self.selected(at).is_some() {
            list.push(Hint::new(&["d"], "Details"));
        }
        list.push(Hint::new(&["e"], "Export"));
        list.push(Hint::new(&["o"], "Open a file"));
        if self
            .opened
            .as_ref()
            .is_some_and(|(_, translation)| translation.count(Standing::Install) > 0)
        {
            list.push(Hint::new(&["a"], "Install"));
        }
        list.push(Hint::new(&["r"], "Read again"));
    }

    fn poll(&mut self, cx: &mut Cx) -> bool {
        if let Some(answer) = self.reading.take() {
            if let Ok((found, known)) = answer {
                self.here = Some(found);
                self.packages_known = known;
            }
            self.build_rows(cx);
            return true;
        }
        if let Some(answer) = self.opening.take() {
            match answer {
                Ok((path, read)) => self.file_read(cx, path, read),
                Err(_) => self.not_opened(cx, "Reading the file stopped without an answer.".into()),
            }
            return true;
        }
        false
    }

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

    fn stale(&mut self, reading: Reading) {
        if reading == Reading::Tools {
            self.stale = true;
        }
    }

    fn draw(&self, view: &View, frame: &mut Frame, area: Rect, state: &mut ListState) {
        let inner = view.page(
            frame,
            area,
            "Migration",
            "Take this machine's applications to another machine or distribution. Exporting only reads.",
        );
        view.fill(frame, inner);
        let focused = view.focused;
        let items_of = self
            .opened
            .as_ref()
            .map(|(_, translation)| translation.items.as_slice())
            .unwrap_or_default();
        let name_width = items_of
            .iter()
            .map(|item| item.name.chars().count())
            .max()
            .unwrap_or(0)
            .min(36)
            + 2;
        let mut items: Vec<Line> = Vec::new();
        for row in &self.rows {
            items.push(match row {
                MigrationRow::Heading(text) => {
                    Line::styled(text.clone(), Style::default().fg(view.theme.group()).bold())
                }
                MigrationRow::Text(text) => {
                    Line::styled(format!("  {text}"), Style::default().fg(view.theme.muted()))
                }
                MigrationRow::Item(index) => {
                    let item = &items_of[*index];
                    let color = match item.standing {
                        Standing::Install => view.theme.entry(),
                        Standing::Here => view.theme.success(),
                        Standing::Unavailable => view.theme.failure(),
                        Standing::Yours => view.theme.warning(),
                    };
                    Line::from(vec![
                        Span::styled(
                            format!(
                                "  {:<name_width$}",
                                helix_toolbox_core::printable(&item.name)
                            ),
                            Style::default().fg(color),
                        ),
                        Span::styled(
                            format!("{:<9}", migration_kind(item.kind)),
                            Style::default().fg(view.theme.muted()),
                        ),
                        Span::styled(
                            helix_toolbox_core::printable(&item.detail),
                            Style::default().fg(view.theme.muted()),
                        ),
                    ])
                }
            });
        }
        let list = List::new(view.measure(items))
            .block(view.card("Migration"))
            .highlight_style(view.highlight(focused))
            .highlight_symbol(view.marker(focused))
            .scroll_padding(1);
        bordered_list(frame, list, inner, state, view.hits);
    }
}