fm84 0.41.0

A synthwave dual-pane TUI file manager
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
//! Archives as directories, from the keys.

use super::*;
use crate::archive::fixtures::{Put, scratch, tar, zip};
use std::path::PathBuf;

/// Put the active panel's cursor on `name`.
fn on(app_state: &mut AppState, name: &str) {
    let (children, state) = if app_state.is_left_active {
        (&app_state.children_left, &mut app_state.state_left)
    } else {
        (&app_state.children_right, &mut app_state.state_right)
    };
    let index = children.iter().position(|item| item.name_os == name).unwrap_or_else(|| panic!("no {name} in the panel"));
    state.select(Some(index));
}

fn shown(app_state: &AppState) -> Vec<String> {
    app_state.children_left.iter().map(|item| item.name_full.clone()).collect()
}

fn finish(app_state: &mut AppState) {
    let started = std::time::Instant::now();
    while app_state.job.is_some() {
        assert!(started.elapsed() < std::time::Duration::from_secs(10), "the job never finished");
        std::thread::sleep(std::time::Duration::from_millis(5));
        app_state.poll_transfer();
    }
}

/// An archive with a little of everything, beside a panel to copy out to.
fn setup(name: &str) -> (PathBuf, PathBuf, AppState) {
    let dir = scratch(name);
    let archive = dir.join("photos.zip");
    zip(&archive, &[("2024/june/beach.txt", Put::File(b"sand and sea")), ("2024/notes.txt", Put::File(b"notes")), ("readme.txt", Put::File(b"read me"))]);
    let out = dir.join("out");
    std::fs::create_dir_all(&out).unwrap();
    let mut app_state = AppState::new();
    app_state.open_dir(false, out, None);
    app_state.is_left_active = true;
    app_state.open_dir(true, dir.clone(), Some("photos.zip".as_ref()));
    (dir, archive, app_state)
}

/// Enter goes into an archive as into a directory, and into the directories
/// in it; Backspace comes back out the way it went in, landing on what it
/// left - the archive itself last.
#[test]
fn enter_goes_into_an_archive_and_backspace_comes_out() {
    let (dir, archive, mut app_state) = setup("in-and-out");

    press(&mut app_state, KeyCode::Enter);
    // As written, not only as compared: a separator on the end of a file's
    // path does not stat, and the next reread climbed out.
    assert_eq!(app_state.dir_left.as_os_str(), archive.as_os_str());
    assert_eq!(app_state.archive_left.as_ref(), Some(&archive));
    assert_eq!(shown(&app_state), ["..", "2024", "readme.txt"]);

    on(&mut app_state, "2024");
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(app_state.dir_left, archive.join("2024"));
    assert_eq!(shown(&app_state), ["..", "june", "notes.txt"]);

    press(&mut app_state, KeyCode::Backspace);
    assert_eq!(app_state.dir_left, archive);
    assert_eq!(app_state.children_left[app_state.state_left.selected().unwrap()].name_full, "2024");
    press(&mut app_state, KeyCode::Backspace);
    assert_eq!(app_state.dir_left, dir);
    assert_eq!(app_state.archive_left, None);
    assert_eq!(app_state.children_left[app_state.state_left.selected().unwrap()].name_full, "photos.zip");
    std::fs::remove_dir_all(&dir).unwrap();
}

/// F3 copies a file out to a scratch directory and shows it there; the
/// panels are left as they were.
#[test]
fn f3_views_a_file_inside_an_archive() {
    let (dir, archive, mut app_state) = setup("view");
    app_state.open_dir(true, archive.join("2024/june"), Some("beach.txt".as_ref()));
    press(&mut app_state, KeyCode::F(3));
    finish(&mut app_state);
    let Screen::Viewer(state) = &app_state.screen else {
        panic!("not viewing: {:?}", app_state.error);
    };
    assert_eq!(state.content_lines, ["sand and sea"]);
    assert_eq!(state.file_path.file_name().unwrap(), "beach.txt");
    assert!(!state.file_path.starts_with(&dir));
    assert_eq!(app_state.dir_left, archive.join("2024/june"));
    // Its scratch directory, and the one they are all in if nothing else in
    // this run has one there too.
    let scratch = state.file_path.parent().unwrap().to_path_buf();
    std::fs::remove_dir_all(&scratch).unwrap();
    let _ = std::fs::remove_dir(scratch.parent().unwrap());
    std::fs::remove_dir_all(&dir).unwrap();
}

/// F5 copies out into the other panel, a directory whole; F6, which would
/// take entries out of the archive, is refused.
#[test]
fn f5_copies_out_and_f6_is_refused() {
    let (dir, archive, mut app_state) = setup("copy-out");
    let out = dir.join("out");
    app_state.open_dir(true, archive.clone(), Some("2024".as_ref()));

    press(&mut app_state, KeyCode::F(5));
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert_eq!(app_state.error, None);
    assert_eq!(std::fs::read_to_string(out.join("2024/june/beach.txt")).unwrap(), "sand and sea");
    assert_eq!(std::fs::read_to_string(out.join("2024/notes.txt")).unwrap(), "notes");

    on(&mut app_state, "readme.txt");
    press(&mut app_state, KeyCode::F(6));
    assert_eq!(app_state.error.as_deref(), Some(crate::app::READ_ONLY));
    assert!(app_state.dialog.is_none());
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Everything that would write into an archive says it is read-only and
/// leaves it as it was - from inside it, and from the other panel into it.
#[test]
fn nothing_writes_into_an_archive() {
    let (dir, archive, mut app_state) = setup("read-only");
    let before = std::fs::read(&archive).unwrap();
    app_state.open_dir(true, archive.clone(), Some("readme.txt".as_ref()));

    let keys = [
        (KeyCode::F(2), KeyModifiers::NONE),
        (KeyCode::F(4), KeyModifiers::NONE),
        (KeyCode::F(4), KeyModifiers::SHIFT),
        (KeyCode::F(5), KeyModifiers::ALT),
        (KeyCode::F(7), KeyModifiers::NONE),
        (KeyCode::F(8), KeyModifiers::NONE),
        (KeyCode::F(8), KeyModifiers::SHIFT),
        (KeyCode::Delete, KeyModifiers::NONE),
        (KeyCode::Char('a'), KeyModifiers::ALT),
    ];
    for (code, modifiers) in keys {
        handle_key(&mut app_state, KeyEvent::new(code, modifiers));
        assert_eq!(app_state.error.as_deref(), Some(crate::app::READ_ONLY), "{code:?} {modifiers:?}");
        assert!(app_state.dialog.is_none() && app_state.job.is_none() && matches!(app_state.screen, Screen::Panels), "{code:?} {modifiers:?}");
        app_state.reset_error();
    }

    // From the other side: a file copied or moved into the archive.
    std::fs::write(dir.join("out/new.txt"), "new").unwrap();
    app_state.open_dir(false, dir.join("out"), Some("new.txt".as_ref()));
    app_state.is_left_active = false;
    for code in [KeyCode::F(5), KeyCode::F(6)] {
        press(&mut app_state, code);
        press(&mut app_state, KeyCode::Enter);
        assert_eq!(app_state.error.as_deref(), Some(crate::app::READ_ONLY), "{code:?}");
        assert!(app_state.job.is_none());
        app_state.reset_error();
    }
    assert_eq!(std::fs::read(&archive).unwrap(), before);
    std::fs::remove_dir_all(&dir).unwrap();
}

/// What is not an archive, named like one, says why it cannot be gone into
/// and the panel stays where it was.
#[test]
fn a_damaged_archive_is_not_gone_into() {
    let dir = scratch("damaged");
    std::fs::write(dir.join("broken.zip"), "not a zip at all").unwrap();
    let mut app_state = AppState::new();
    app_state.is_left_active = true;
    app_state.open_dir(true, dir.clone(), Some("broken.zip".as_ref()));
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(app_state.dir_left, dir);
    assert_eq!(app_state.archive_left, None);
    assert!(app_state.error.as_deref().unwrap_or_default().contains("not readable as an archive"), "{:?}", app_state.error);
    std::fs::remove_dir_all(&dir).unwrap();
}

/// An archive written over while a panel is inside it is read again, and a
/// directory it no longer has is climbed out of; one deleted is left.
#[test]
fn a_changed_or_deleted_archive_is_followed() {
    let dir = scratch("changed");
    let archive = dir.join("src.tar.gz");
    tar(&archive, &[(b"src/main.rs", Put::File(b"fn main() {}")), (b"src/lib.rs", Put::File(b""))]);
    let mut app_state = AppState::new();
    app_state.is_left_active = true;
    app_state.open_dir(true, archive.join("src"), None);
    assert_eq!(shown(&app_state), ["..", "lib.rs", "main.rs"]);

    tar(&archive, &[(b"docs/readme", Put::File(b"r"))]);
    app_state.reload_panel(true, None);
    assert_eq!(app_state.dir_left, archive);
    assert_eq!(shown(&app_state), ["..", "docs"]);

    std::fs::remove_file(&archive).unwrap();
    app_state.reload_panel(true, None);
    assert_eq!(app_state.dir_left, dir);
    assert_eq!(app_state.archive_left, None);
    std::fs::remove_dir_all(&dir).unwrap();
}

/// The preview and the detail lines read an entry from its archive, and
/// Space sizes a directory there from the index.
#[test]
fn preview_detail_and_size_inside_an_archive() {
    let (dir, archive, mut app_state) = setup("preview");
    app_state.show_preview = true;
    app_state.open_dir(true, archive.clone(), Some("readme.txt".as_ref()));
    app_state.refresh_cursor_detail();
    app_state.refresh_preview();
    assert_eq!(app_state.preview.as_ref().unwrap().lines, ["read me"]);
    assert_eq!(app_state.cursor_detail.as_ref().unwrap().size, "7 bytes");

    on(&mut app_state, "2024");
    press(&mut app_state, KeyCode::Char(' '));
    let started = std::time::Instant::now();
    while !app_state.dir_sizing.is_empty() {
        assert!(started.elapsed() < std::time::Duration::from_secs(10), "never sized");
        std::thread::sleep(std::time::Duration::from_millis(5));
        app_state.poll_dir_sizes();
    }
    assert_eq!(app_state.dir_sizes.get(&archive.join("2024")), Some(&17));
    assert_eq!(app_state.error, None);
    std::fs::remove_dir_all(&dir).unwrap();
}

/// A path through an archive is somewhere a tab can be, typed into Ctrl+L
/// or kept as a bookmark: going there goes in.
#[test]
fn a_path_through_an_archive_is_gone_to() {
    let (dir, archive, mut app_state) = setup("go-to");
    handle_key(&mut app_state, KeyEvent::new(KeyCode::Char('l'), KeyModifiers::CONTROL));
    app_state.dialog_input().unwrap().set(archive.join("2024/june").display().to_string());
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(app_state.dir_left, archive.join("2024/june"));
    assert_eq!(shown(&app_state), ["..", "beach.txt"]);
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Alt+F9 extracts the whole of the archive under the cursor - or, Ctrl+F9,
/// of the one the panel is inside - into the other panel; on anything else
/// it says what it wants.
#[test]
fn alt_f9_extracts_a_whole_archive() {
    let (dir, archive, mut app_state) = setup("extract-all");
    let out = dir.join("out");

    handle_key(&mut app_state, KeyEvent::new(KeyCode::F(9), KeyModifiers::ALT));
    assert!(matches!(app_state.dialog, Some(Dialog::Extract { .. })));
    assert_eq!(PathBuf::from(&app_state.dialog_input().unwrap().text), out);
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert_eq!(app_state.error, None);
    assert_eq!(std::fs::read_to_string(out.join("2024/june/beach.txt")).unwrap(), "sand and sea");
    assert_eq!(std::fs::read_to_string(out.join("readme.txt")).unwrap(), "read me");

    // From inside it, into a directory typed: relative to where the archive is.
    std::fs::create_dir(dir.join("again")).unwrap();
    app_state.open_dir(true, archive.join("2024"), None);
    handle_key(&mut app_state, KeyEvent::new(KeyCode::F(9), KeyModifiers::CONTROL));
    app_state.dialog_input().unwrap().set("again".to_string());
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert_eq!(std::fs::read_to_string(dir.join("again/readme.txt")).unwrap(), "read me");

    // Not onto an archive, nor into one.
    app_state.open_dir(true, dir.clone(), Some("out".as_ref()));
    handle_key(&mut app_state, KeyEvent::new(KeyCode::F(9), KeyModifiers::ALT));
    assert_eq!(app_state.error.as_deref(), Some("Put the cursor on an archive to extract it"));
    app_state.reset_error();
    on(&mut app_state, "photos.zip");
    handle_key(&mut app_state, KeyEvent::new(KeyCode::F(9), KeyModifiers::ALT));
    app_state.dialog_input().unwrap().set(archive.join("2024").display().to_string());
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(app_state.error.as_deref(), Some(crate::app::READ_ONLY));
    assert!(app_state.job.is_none());
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Shift+F3 compares a file inside an archive with its namesake on the disk,
/// and with one inside another archive, copying each out first; the diff is
/// headed with the paths the panels know them by.
#[test]
fn shift_f3_compares_files_inside_archives() {
    let (dir, archive, mut app_state) = setup("compare");
    let sides = |app_state: &AppState| match &app_state.screen {
        Screen::Diff(state) => (state.left_path.clone(), state.right_path.clone(), state.hunks.len()),
        _ => panic!("not comparing: {:?}", app_state.error),
    };
    let compare = |app_state: &mut AppState| {
        handle_key(app_state, KeyEvent::new(KeyCode::F(3), KeyModifiers::SHIFT));
        finish(app_state);
    };

    // Inside on the left, on the disk on the right.
    std::fs::write(dir.join("out/readme.txt"), "read me\nand more\n").unwrap();
    app_state.open_dir(false, dir.join("out"), Some("readme.txt".as_ref()));
    app_state.open_dir(true, archive.clone(), Some("readme.txt".as_ref()));
    compare(&mut app_state);
    assert_eq!(sides(&app_state), (archive.join("readme.txt"), dir.join("out/readme.txt"), 1));

    // Inside two archives, one a panel each: two jobs, one after the other.
    app_state.screen = Screen::Panels;
    let other = dir.join("other.tar.gz");
    tar(&other, &[(b"readme.txt", Put::File(b"read me too"))]);
    app_state.open_dir(false, other.clone(), Some("readme.txt".as_ref()));
    compare(&mut app_state);
    assert_eq!(sides(&app_state), (archive.join("readme.txt"), other.join("readme.txt"), 1));
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Ctrl+C copies an entry's path as it runs through its archive, as the
/// panel shows it.
#[test]
fn ctrl_c_copies_a_path_through_an_archive() {
    let (dir, archive, mut app_state) = setup("paths");
    app_state.open_dir(true, archive.join("2024"), Some("notes.txt".as_ref()));
    handle_key(&mut app_state, KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
    assert_eq!(PathBuf::from(&app_state.clipboard), archive.join("2024/notes.txt"));
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Alt+P packs what is chosen into an archive in the other panel, named
/// after it, and lands there on what it made; Tab goes through the formats
/// by the ending of the name.
#[test]
fn alt_p_packs_into_the_other_panel() {
    let dir = scratch("pack-keys");
    let (here, out) = (dir.join("here"), dir.join("out"));
    std::fs::create_dir_all(here.join("photos")).unwrap();
    std::fs::write(here.join("photos/beach.txt"), "sand").unwrap();
    std::fs::create_dir_all(&out).unwrap();
    let mut app_state = AppState::new();
    app_state.open_dir(false, out.clone(), None);
    app_state.is_left_active = true;
    app_state.open_dir(true, here.clone(), Some("photos".as_ref()));
    let alt_p = |app_state: &mut AppState| handle_key(app_state, KeyEvent::new(KeyCode::Char('p'), KeyModifiers::ALT));
    let typed = |app_state: &mut AppState| PathBuf::from(&app_state.dialog_input().unwrap().text);

    alt_p(&mut app_state);
    assert_eq!(typed(&mut app_state), out.join("photos.zip"));
    let mut seen = Vec::new();
    for _ in 0..6 {
        press(&mut app_state, KeyCode::Tab);
        seen.push(typed(&mut app_state).file_name().unwrap().to_string_lossy().into_owned());
    }
    assert_eq!(seen, ["photos.7z", "photos.tar.gz", "photos.tar.xz", "photos.tar.bz2", "photos.tar", "photos.zip"]);
    press(&mut app_state, KeyCode::Tab);
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert_eq!(app_state.error, None);
    let index = crate::archive::index(&out.join("photos.7z")).unwrap();
    assert!(index.find(std::path::Path::new("photos/beach.txt")).is_some());
    let landed = &app_state.children_right[app_state.state_right.selected().unwrap()];
    assert_eq!(landed.name_full, "photos.7z");

    // Not again over it, unless F11 says to overwrite.
    alt_p(&mut app_state);
    app_state.dialog_input().unwrap().set(out.join("photos.7z").display().to_string());
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.error.as_deref().unwrap_or_default().starts_with("Already exists"), "{:?}", app_state.error);
    assert!(app_state.job.is_none());
    app_state.reset_error();

    // With F11 on Keep both, beside it.
    app_state.options.on_existing = crate::options::OnExisting::KeepBoth;
    alt_p(&mut app_state);
    app_state.dialog_input().unwrap().set(out.join("photos.7z").display().to_string());
    press(&mut app_state, KeyCode::Enter);
    finish(&mut app_state);
    assert!(out.join("photos (2).7z").is_file());
    app_state.options.on_existing = crate::options::OnExisting::Ask;

    // Not a name an archive has, nor into an archive, nor from inside one.
    alt_p(&mut app_state);
    app_state.dialog_input().unwrap().set(out.join("photos.rar").display().to_string());
    press(&mut app_state, KeyCode::Enter);
    assert!(app_state.error.as_deref().unwrap_or_default().starts_with("Name it as an archive"), "{:?}", app_state.error);
    app_state.reset_error();
    alt_p(&mut app_state);
    app_state.dialog_input().unwrap().set(out.join("photos.7z/inside.zip").display().to_string());
    press(&mut app_state, KeyCode::Enter);
    assert_eq!(app_state.error.as_deref(), Some(crate::app::READ_ONLY));
    app_state.reset_error();
    app_state.open_dir(true, out.join("photos.7z"), None);
    alt_p(&mut app_state);
    assert!(app_state.error.as_deref().unwrap_or_default().starts_with("Copy it out of the archive first"), "{:?}", app_state.error);
    assert!(app_state.dialog.is_none());
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Checksums are worked out of files on the disk: inside an archive, Alt+H
/// says so.
#[test]
fn alt_h_inside_an_archive_says_to_copy_out_first() {
    let (dir, archive, mut app_state) = setup("checksum-inside");
    app_state.open_dir(true, archive.clone(), Some("readme.txt".as_ref()));
    handle_key(&mut app_state, KeyEvent::new(KeyCode::Char('h'), KeyModifiers::ALT));
    assert!(app_state.error.as_deref().unwrap_or_default().starts_with("Copy it out of the archive first"), "{:?}", app_state.error);
    assert!(app_state.job.is_none());
    std::fs::remove_dir_all(&dir).unwrap();
}

/// Ctrl+B lists every file in an archive at once, by its path from where
/// the panel is, and F3 opens one from there as from its own directory.
#[test]
fn ctrl_b_lists_every_file_in_an_archive() {
    let (dir, archive, mut app_state) = setup("flat");
    press(&mut app_state, KeyCode::Enter);
    handle_key(&mut app_state, KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert_eq!(app_state.flat_left.as_ref(), Some(&archive));
    let mut names = shown(&app_state);
    names[1..].sort();
    assert_eq!(names, ["..", "2024/june/beach.txt", "2024/notes.txt", "readme.txt"]);

    on(&mut app_state, "2024/june/beach.txt");
    press(&mut app_state, KeyCode::F(3));
    finish(&mut app_state);
    let Screen::Viewer(state) = &app_state.screen else {
        panic!("not viewing: {:?}", app_state.error);
    };
    assert_eq!(state.content_lines, ["sand and sea"]);
    let scratch = state.file_path.parent().unwrap().to_path_buf();
    std::fs::remove_dir_all(&scratch).unwrap();
    let _ = std::fs::remove_dir(scratch.parent().unwrap());
    std::fs::remove_dir_all(&dir).unwrap();
}