elio 1.5.1

Snappy, batteries-included terminal file manager with rich previews, inline images, bulk actions, and trash support.
Documentation
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
use super::super::ArchiveFormat;
use super::super::build_archive_preview;
use super::{
    ComicArchiveBackend, ComicArchiveSignature, build_comic_archive_preview,
    parse_comic_archive_from_7z_output, parse_comic_book_info_comment, parse_unrar_archive_comment,
    parse_zip_comic_archive, sniff_comic_archive_signature,
};
use crate::preview::PreviewKind;
use image::{DynamicImage, ImageFormat, Rgba, RgbaImage};
use std::{
    env,
    fs::{self, File},
    io::Write,
    path::PathBuf,
    process::Command,
    sync::atomic::{AtomicBool, Ordering},
    time::{SystemTime, UNIX_EPOCH},
};

fn temp_path(label: &str) -> PathBuf {
    let unique = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time should be after unix epoch")
        .as_nanos();
    env::temp_dir().join(format!("elio-comic-archive-{label}-{unique}"))
}

#[test]
fn sniff_comic_archive_signature_detects_common_formats() {
    let root = temp_path("signature-sniff");
    fs::create_dir_all(&root).expect("failed to create temp root");

    let zip = root.join("issue.cbz");
    fs::write(&zip, b"PK\x03\x04demo").expect("failed to write zip signature");
    assert_eq!(
        sniff_comic_archive_signature(&zip),
        ComicArchiveSignature::Zip
    );

    let rar = root.join("issue.cbr");
    fs::write(&rar, b"Rar!\x1a\x07\x01\x00demo").expect("failed to write rar signature");
    assert_eq!(
        sniff_comic_archive_signature(&rar),
        ComicArchiveSignature::Rar
    );

    let seven_zip = root.join("issue.7z");
    fs::write(&seven_zip, [0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, 0, 0])
        .expect("failed to write 7z signature");
    assert_eq!(
        sniff_comic_archive_signature(&seven_zip),
        ComicArchiveSignature::SevenZip
    );

    let unknown = root.join("issue.bin");
    fs::write(&unknown, b"not-an-archive").expect("failed to write unknown file");
    assert_eq!(
        sniff_comic_archive_signature(&unknown),
        ComicArchiveSignature::Unknown
    );

    fs::remove_dir_all(&root).expect("failed to remove temp root");
}

#[test]
fn parses_comic_pages_from_7z_listing_output() {
    let output = r#"
Path = /tmp/orbital.cbz
Type = Rar
Physical Size = 1024

----------
Path = 010.jpg
Folder = -
Size = 10
Packed Size = 10

Path = 002.jpg
Folder = -
Size = 20
Packed Size = 20

Path = notes/readme.txt
Folder = -
Size = 30
Packed Size = 30

Path = 001.jpg
Folder = -
Size = 40
Packed Size = 40
"#;

    let comic = parse_comic_archive_from_7z_output(output, &|| false)
        .expect("7z output should yield comic pages");

    assert_eq!(comic.backend, ComicArchiveBackend::SevenZip);
    assert_eq!(comic.page_entries.len(), 3);
    assert_eq!(comic.page_entries[0].entry_name, "001.jpg");
    assert_eq!(comic.page_entries[1].entry_name, "002.jpg");
    assert_eq!(comic.page_entries[2].entry_name, "010.jpg");
    assert!(comic.archive_comment.is_none());
}

#[test]
fn parses_comic_book_info_from_7z_archive_comment() {
    let output = r#"
Path = /tmp/commented.cbr
Type = Rar
Physical Size = 1024
Comment = {"ComicBookInfo/1.0":{"series":"Aurora Riders","title":"First Light","issue":"1","publisher":"Elio Press","publicationYear":1958,"genre":"Sci-Fi","credits":[{"role":"Writer","person":"Lee Maven"}]}}

----------
Path = 001.jpg
Folder = -
Size = 10
Packed Size = 10
"#;

    let comic = parse_comic_archive_from_7z_output(output, &|| false)
        .expect("7z output should yield comic pages");
    let metadata = comic
        .archive_comment
        .as_deref()
        .and_then(parse_comic_book_info_comment)
        .expect("7z archive comment should contain ComicBookInfo metadata");

    assert_eq!(metadata.series.as_deref(), Some("Aurora Riders"));
    assert_eq!(metadata.title.as_deref(), Some("First Light"));
    assert_eq!(metadata.number.as_deref(), Some("1"));
    assert_eq!(metadata.publisher.as_deref(), Some("Elio Press"));
    assert_eq!(metadata.year.as_deref(), Some("1958"));
    assert_eq!(metadata.writer.as_deref(), Some("Lee Maven"));
    assert_eq!(metadata.genre.as_deref(), Some("Sci-Fi"));
}

#[test]
fn parses_multiline_comic_book_info_from_7z_archive_comment() {
    let output = r#"
Path = /tmp/commented.cbr
Type = Rar
Physical Size = 1024
Comment =
{
{
  "appMetadata": {},
  "ComicBookInfo/1.0": {
    "series": "Orbital Stories",
    "issue": 4
  }
}
}

----------
Path = 001.jpg
Folder = -
Size = 10
Packed Size = 10
"#;

    let comic = parse_comic_archive_from_7z_output(output, &|| false)
        .expect("7z output should yield comic pages");
    let metadata = comic
        .archive_comment
        .as_deref()
        .and_then(parse_comic_book_info_comment)
        .expect("7z multiline archive comment should contain ComicBookInfo metadata");

    assert_eq!(metadata.series.as_deref(), Some("Orbital Stories"));
    assert_eq!(metadata.number.as_deref(), Some("4"));
}

#[test]
fn parses_plain_multiline_comic_book_info_from_7z_archive_comment() {
    let output = r#"
Path = /tmp/commented.cbr
Type = Rar
Physical Size = 1024
Comment =
{
  "ComicBookInfo/1.0": {
    "series": "Plain Comment",
    "issue": 7
  }
}

----------
Path = 001.jpg
Folder = -
Size = 10
Packed Size = 10
"#;

    let comic = parse_comic_archive_from_7z_output(output, &|| false)
        .expect("7z output should yield comic pages");
    let metadata = comic
        .archive_comment
        .as_deref()
        .and_then(parse_comic_book_info_comment)
        .expect("plain 7z archive comment should contain ComicBookInfo metadata");

    assert_eq!(metadata.series.as_deref(), Some("Plain Comment"));
    assert_eq!(metadata.number.as_deref(), Some("7"));
}

#[test]
fn parses_comic_book_info_from_unrar_archive_comment() {
    let output = r#"
UNRAR 7.21

Archive: /tmp/commented.cbr
{"ComicBookInfo/1.0":{"series":"Orbital Stories","issue":4}}
Details: RAR 5

 Attributes       Size     Date    Time   Name
----------- ----------  ---------- -----  ----
    ..A....         10  2026-01-01 00:00  001.jpg
"#;

    let metadata = parse_unrar_archive_comment(output)
        .as_deref()
        .and_then(parse_comic_book_info_comment)
        .expect("unrar archive comment should contain ComicBookInfo metadata");

    assert_eq!(metadata.series.as_deref(), Some("Orbital Stories"));
    assert_eq!(metadata.number.as_deref(), Some("4"));
}

#[test]
fn parse_zip_comic_archive_returns_none_when_canceled() {
    let root = temp_path("zip-cancel");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let archive = root.join("issue.cbz");
    let file = File::create(&archive).expect("failed to create comic zip");
    let mut zip = zip::ZipWriter::new(file);
    let options = zip::write::SimpleFileOptions::default();
    zip.start_file("001.jpg", options)
        .expect("failed to start first page");
    zip.write_all(b"page-one")
        .expect("failed to write first page");
    zip.start_file("002.jpg", options)
        .expect("failed to start second page");
    zip.write_all(b"page-two")
        .expect("failed to write second page");
    zip.finish().expect("failed to finish comic zip");

    let canceled = AtomicBool::new(true);
    let parsed = parse_zip_comic_archive(&archive, &|| canceled.load(Ordering::Relaxed));
    assert!(parsed.is_none(), "canceled zip parsing should stop early");

    fs::remove_dir_all(&root).expect("failed to remove temp root");
}

#[test]
fn build_comic_archive_preview_falls_back_to_7z_for_mislabeled_cbz() {
    let root = temp_path("mislabeled-cbz");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let first = root.join("001.png");
    let second = root.join("010.png");
    let image = DynamicImage::ImageRgba8(RgbaImage::from_pixel(1, 1, Rgba([1, 2, 3, 255])));
    image
        .save_with_format(&first, ImageFormat::Png)
        .expect("failed to write first image");
    image
        .save_with_format(&second, ImageFormat::Png)
        .expect("failed to write second image");

    let archive = root.join("broken.cbz");
    let status = Command::new("7z")
        .current_dir(&root)
        .arg("a")
        .arg("-t7z")
        .arg(&archive)
        .arg("001.png")
        .arg("010.png")
        .status();
    let Ok(status) = status else {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    };
    if !status.success() {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    }

    let preview = build_comic_archive_preview(
        &archive,
        ArchiveFormat::ComicZip,
        Some("Comic ZIP archive"),
        0,
        &|| false,
    )
    .expect("mislabeled cbz should still build comic preview");

    assert_eq!(preview.kind, PreviewKind::Comic);
    assert_eq!(preview.detail.as_deref(), Some("Comic ZIP archive"));
    assert_eq!(
        preview
            .navigation_position
            .as_ref()
            .map(|position| position.count),
        Some(2)
    );
    let visual = preview
        .preview_visual
        .as_ref()
        .expect("comic preview should expose a page visual");
    let dimensions = image::ImageReader::open(&visual.path)
        .expect("extracted page should open")
        .with_guessed_format()
        .expect("page format should be detected")
        .into_dimensions()
        .expect("page dimensions should be readable");
    assert_eq!(dimensions, (1, 1));

    fs::remove_dir_all(&root).expect("failed to remove temp root");
}

#[test]
fn build_archive_preview_detects_cbr_as_comic_when_7z_backend_is_needed() {
    let root = temp_path("cbr-7z-backend");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let first = root.join("001.png");
    let second = root.join("010.png");
    let image = DynamicImage::ImageRgba8(RgbaImage::from_pixel(1, 1, Rgba([1, 2, 3, 255])));
    image
        .save_with_format(&first, ImageFormat::Png)
        .expect("failed to write first image");
    image
        .save_with_format(&second, ImageFormat::Png)
        .expect("failed to write second image");

    let archive = root.join("issue.cbr");
    let status = Command::new("7z")
        .current_dir(&root)
        .arg("a")
        .arg("-t7z")
        .arg(&archive)
        .arg("001.png")
        .arg("010.png")
        .status();
    let Ok(status) = status else {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    };
    if !status.success() {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    }

    let preview = build_archive_preview(&archive, Some("Comic RAR archive"), Some(0), &|| false)
        .expect("cbr should build comic preview");

    assert_eq!(preview.kind, PreviewKind::Comic);
    assert_eq!(preview.detail.as_deref(), Some("Comic RAR archive"));
    assert_eq!(
        preview
            .navigation_position
            .as_ref()
            .map(|position| position.count),
        Some(2)
    );
    assert!(preview.preview_visual.is_some());

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn build_comic_archive_preview_shows_status_note_when_cbr_cannot_be_opened() {
    // Write a file with a RAR5 magic header but invalid/truncated body.
    // All backends (ZIP, 7z, unrar) reject it, so the code must choose a
    // status note based on whether a RAR-capable extractor is installed:
    //   • no extractor  → "RAR preview requires unrar or a 7z build with RAR support"
    //   • extractor present but file unreadable → "Unable to read RAR archive …"
    // Both messages contain "RAR", which is the common denominator we assert.
    let root = temp_path("cbr-unreadable");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let archive = root.join("issue.cbr");
    let rar5_header = b"Rar!\x1a\x07\x01\x00\x00\x00\x00";
    fs::write(&archive, rar5_header).expect("failed to write fake rar header");

    let preview = build_comic_archive_preview(
        &archive,
        ArchiveFormat::ComicRar,
        Some("Comic RAR archive"),
        0,
        &|| false,
    )
    .expect("unreadable cbr should return a status preview, not None");

    assert_eq!(preview.kind, PreviewKind::Comic);
    assert_eq!(preview.detail.as_deref(), Some("Comic RAR archive"));
    assert!(
        preview.navigation_position.is_none(),
        "no pages should be navigable when no backend could open the archive"
    );
    let status = preview.status_note.as_deref().unwrap_or("");
    assert!(
        status.contains("RAR"),
        "status note should mention RAR, got: {status:?}"
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}