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
641
642
643
644
645
use std::cmp::min;
use std::fmt::Write as _;
use std::io::{BufRead, BufReader, Read};
use std::iter::{Enumerate, Skip, Take};
use std::panic;
use std::path::{Path, PathBuf};
use std::slice::Iter;

use content_inspector::{inspect, ContentType};
use image::imageops::FilterType;
use image::{ImageBuffer, Rgb};
use pdf_extract;
use syntect::easy::HighlightLines;
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::{SyntaxReference, SyntaxSet};
use termtree::Tree;
use tuikit::attr::{Attr, Color};

use crate::compress::list_files;
use crate::fileinfo::{FileInfo, FileKind};
use crate::fm_error::{FmError, FmResult};

/// Different kind of preview used to display some informaitons
/// About the file.
/// We check if it's an archive first, then a pdf file, an image, a media file
#[derive(Clone)]
pub enum Preview {
    Syntaxed(HLContent),
    Text(TextContent),
    Binary(BinaryContent),
    Pdf(PdfContent),
    Archive(ZipContent),
    Exif(ExifContent),
    Thumbnail(Pixels),
    Media(MediaContent),
    Directory(Directory),
    Empty,
}

#[derive(Clone, Default)]
pub enum TextKind {
    HELP,
    #[default]
    TEXTFILE,
}

impl Preview {
    const CONTENT_INSPECTOR_MIN_SIZE: usize = 1024;

    /// Creates a new preview instance based on the filekind and the extension of
    /// the file.
    /// Sometimes it reads the content of the file, sometimes it delegates
    /// it to the display method.
    pub fn new(file_info: &FileInfo) -> FmResult<Self> {
        match file_info.file_kind {
            FileKind::Directory => Ok(Self::Directory(Directory::new(&file_info.path)?)),
            FileKind::NormalFile => match file_info.extension.to_lowercase().as_str() {
                e if is_ext_zip(e) => Ok(Self::Archive(ZipContent::new(&file_info.path)?)),
                e if is_ext_pdf(e) => Ok(Self::Pdf(PdfContent::new(&file_info.path))),
                e if is_ext_image(e) => Ok(Self::Exif(ExifContent::new(&file_info.path)?)),
                e if is_ext_media(e) => Ok(Self::Media(MediaContent::new(&file_info.path)?)),
                e => match Self::preview_syntaxed(e, &file_info.path) {
                    Some(syntaxed_preview) => Ok(syntaxed_preview),
                    None => Self::preview_text_or_binary(file_info),
                },
            },
            _ => Err(FmError::custom(
                "new preview",
                "Can't preview this filekind",
            )),
        }
    }

    fn preview_syntaxed(ext: &str, path: &Path) -> Option<Self> {
        let ss = SyntaxSet::load_defaults_nonewlines();
        ss.find_syntax_by_extension(ext).map(|syntax| {
            Self::Syntaxed(HLContent::new(path, ss.clone(), syntax).unwrap_or_default())
        })
    }

    fn preview_text_or_binary(file_info: &FileInfo) -> FmResult<Self> {
        let mut file = std::fs::File::open(file_info.path.clone())?;
        let mut buffer = vec![0; Self::CONTENT_INSPECTOR_MIN_SIZE];
        if Self::is_binary(file_info, &mut file, &mut buffer) {
            Ok(Self::Binary(BinaryContent::new(file_info)?))
        } else {
            Ok(Self::Text(TextContent::from_file(&file_info.path)?))
        }
    }

    fn is_binary(file_info: &FileInfo, file: &mut std::fs::File, buffer: &mut [u8]) -> bool {
        file_info.size >= Self::CONTENT_INSPECTOR_MIN_SIZE as u64
            && file.read_exact(buffer).is_ok()
            && inspect(buffer) == ContentType::BINARY
    }

    /// Creates a thumbnail preview of the file.
    pub fn thumbnail(path: PathBuf) -> FmResult<Self> {
        Ok(Self::Thumbnail(Pixels::new(path)?))
    }

    /// Creates the help preview as if it was a text file.
    pub fn help(help: &str) -> Self {
        Self::Text(TextContent::help(help))
    }

    /// Empty preview, holding nothing.
    pub fn new_empty() -> Self {
        Self::Empty
    }

    /// The size (most of the time the number of lines) of the preview.
    /// Some preview (thumbnail, empty) can't be scrolled and their size is always 0.
    pub fn len(&self) -> usize {
        match self {
            Self::Empty => 0,
            Self::Syntaxed(syntaxed) => syntaxed.len(),
            Self::Text(text) => text.len(),
            Self::Binary(binary) => binary.len(),
            Self::Pdf(pdf) => pdf.len(),
            Self::Archive(zip) => zip.len(),
            Self::Thumbnail(_img) => 0,
            Self::Exif(exif_content) => exif_content.len(),
            Self::Media(media) => media.len(),
            Self::Directory(directory) => directory.len(),
        }
    }

    /// True if nothing is currently previewed.
    pub fn is_empty(&self) -> bool {
        matches!(*self, Self::Empty)
    }
}

/// Holds a preview of a text content.
/// It's a boxed vector of strings (per line)
#[derive(Clone, Default)]
pub struct TextContent {
    pub kind: TextKind,
    content: Vec<String>,
    length: usize,
}

impl TextContent {
    fn help(help: &str) -> Self {
        let content: Vec<String> = help.split('\n').map(|s| s.to_owned()).collect();
        Self {
            kind: TextKind::HELP,
            length: content.len(),
            content,
        }
    }

    fn from_file(path: &Path) -> FmResult<Self> {
        let reader = std::io::BufReader::new(std::fs::File::open(path)?);
        let content: Vec<String> = reader
            .lines()
            .map(|line| line.unwrap_or_else(|_| "".to_owned()))
            .collect();
        Ok(Self {
            kind: TextKind::TEXTFILE,
            length: content.len(),
            content,
        })
    }

    fn len(&self) -> usize {
        self.length
    }
}

/// Holds a preview of a code text file whose language is supported by `Syntect`.
/// The file is colored propery and line numbers are shown.
#[derive(Clone, Default)]
pub struct HLContent {
    content: Vec<Vec<SyntaxedString>>,
    length: usize,
}

impl HLContent {
    /// Creates a new displayable content of a syntect supported file.
    /// It may file if the file isn't properly formatted or the extension
    /// is wrong (ie. python content with .c extension).
    /// ATM only Solarized (dark) theme is supported.
    fn new(path: &Path, syntax_set: SyntaxSet, syntax_ref: &SyntaxReference) -> FmResult<Self> {
        let reader = std::io::BufReader::new(std::fs::File::open(path)?);
        let raw_content: Vec<String> = reader
            .lines()
            .map(|line| line.unwrap_or_else(|_| "".to_owned()))
            .collect();
        let highlighted_content = Self::parse_raw_content(raw_content, syntax_set, syntax_ref);

        Ok(Self {
            length: highlighted_content.len(),
            content: highlighted_content,
        })
    }

    fn len(&self) -> usize {
        self.length
    }

    fn parse_raw_content(
        raw_content: Vec<String>,
        syntax_set: SyntaxSet,
        syntax_ref: &SyntaxReference,
    ) -> Vec<Vec<SyntaxedString>> {
        let theme_set = ThemeSet::load_defaults();
        let mut highlighted_content = vec![];
        let mut highlighter =
            HighlightLines::new(syntax_ref, &theme_set.themes["Solarized (dark)"]);

        for line in raw_content.iter() {
            let mut col = 0;
            let mut v_line = vec![];
            if let Ok(v) = highlighter.highlight_line(line, &syntax_set) {
                for (style, token) in v.iter() {
                    v_line.push(SyntaxedString::from_syntect(col, token, *style));
                    col += token.len();
                }
            }
            highlighted_content.push(v_line)
        }

        highlighted_content
    }
}

/// Holds a string to be displayed with given colors.
/// We have to read the colors from Syntect and parse it into tuikit attr
/// This struct does the parsing.
#[derive(Clone)]
pub struct SyntaxedString {
    // row: usize,
    col: usize,
    content: String,
    attr: Attr,
}

impl SyntaxedString {
    /// Parse a content and style into a `SyntaxedString`
    /// Only the foreground color is read, we don't the background nor
    /// the style (bold, italic, underline) defined in Syntect.
    fn from_syntect(col: usize, content: &str, style: Style) -> Self {
        let content = content.to_owned();
        let fg = style.foreground;
        let attr = Attr::from(Color::Rgb(fg.r, fg.g, fg.b));
        Self { col, content, attr }
    }

    /// Prints itself on a tuikit canvas.
    pub fn print(
        &self,
        canvas: &mut dyn tuikit::canvas::Canvas,
        row: usize,
        offset: usize,
    ) -> FmResult<()> {
        canvas.print_with_attr(row, self.col + offset + 2, &self.content, self.attr)?;
        Ok(())
    }
}

/// Holds a preview of a binary content.
/// It doesn't try to respect endianness.
/// The lines are formatted to display 16 bytes.
#[derive(Clone)]
pub struct BinaryContent {
    pub path: PathBuf,
    length: u64,
    content: Vec<Line>,
}

impl BinaryContent {
    const LINE_WIDTH: usize = 16;

    fn new(file_info: &FileInfo) -> FmResult<Self> {
        let mut reader = BufReader::new(std::fs::File::open(file_info.path.clone())?);
        let mut buffer = [0; Self::LINE_WIDTH];
        let mut content: Vec<Line> = vec![];
        while let Ok(nb_bytes_read) = reader.read(&mut buffer[..]) {
            if nb_bytes_read != Self::LINE_WIDTH {
                content.push(Line::new((&buffer[0..nb_bytes_read]).into()));
                break;
            } else {
                content.push(Line::new(buffer.into()));
            }
        }

        Ok(Self {
            path: file_info.path.clone(),
            length: file_info.size / Self::LINE_WIDTH as u64,
            content,
        })
    }

    /// WATCHOUT !
    /// Doesn't return the size of the file, like similar methods in other variants.
    /// It returns the number of **lines**.
    /// It's the size of the file divided by `BinaryContent::LINE_WIDTH` which is 16.
    pub fn len(&self) -> usize {
        self.length as usize
    }

    pub fn is_empty(&self) -> bool {
        self.length == 0
    }
}

/// Holds a `Vec` of "bytes" (`u8`).
/// It's mostly used to implement a `print` method.
#[derive(Clone)]
pub struct Line {
    line: Vec<u8>,
}

impl Line {
    fn new(line: Vec<u8>) -> Self {
        Self { line }
    }

    fn format(&self) -> String {
        let mut s = "".to_owned();
        for (i, byte) in self.line.iter().enumerate() {
            let _ = write!(s, "{:02x}", byte);
            if i % 2 == 1 {
                s.push(' ');
            }
        }
        s
    }

    /// Print line of pair of bytes in hexadecimal, 16 bytes long.
    /// It tries to imitates the output of hexdump.
    pub fn print(&self, canvas: &mut dyn tuikit::canvas::Canvas, row: usize, offset: usize) {
        let _ = canvas.print(row, offset + 2, &self.format());
    }
}

/// Holds a preview of a pdffile as outputed by `pdf_extract` crate.
/// If the pdf file content can't be extracted, it doesn't fail but simply hold
/// an error message to be displayed.
/// Afterall, it's just a TUI filemanager, the user shouldn't expect to display
/// any kind of graphical pdf...
#[derive(Clone)]
pub struct PdfContent {
    length: usize,
    content: Vec<String>,
}

impl PdfContent {
    fn new(path: &Path) -> Self {
        let result = catch_unwind_silent(|| {
            if let Ok(content_string) = pdf_extract::extract_text(path) {
                content_string
                    .split_whitespace()
                    .map(|s| s.to_owned())
                    .collect()
            } else {
                vec!["Coudln't parse the pdf".to_owned()]
            }
        });
        let content = result.unwrap_or_else(|_| vec!["Couldn't read the pdf".to_owned()]);

        Self {
            length: content.len(),
            content,
        }
    }

    fn len(&self) -> usize {
        self.length
    }
}

/// Holds a list of file of an archive as returned by `compress_tools::list_files`.
/// It may fail if the archive can't be read properly.
#[derive(Clone)]
pub struct ZipContent {
    length: usize,
    content: Vec<String>,
}

impl ZipContent {
    fn new(path: &Path) -> FmResult<Self> {
        let content = list_files(path)?;

        Ok(Self {
            length: content.len(),
            content,
        })
    }

    fn len(&self) -> usize {
        self.length
    }
}

/// Holds the exif content of an image.
/// Since displaying a thumbnail is ugly and idk how to bind ueberzug into
/// tuikit, it's preferable.
/// At least it's an easy way to display informations about an image.
#[derive(Clone)]
pub struct ExifContent {
    length: usize,
    /// The exif strings.
    content: Vec<String>,
}

impl ExifContent {
    fn new(path: &Path) -> FmResult<Self> {
        let mut bufreader = BufReader::new(std::fs::File::open(path)?);
        let content: Vec<String> =
            if let Ok(exif) = exif::Reader::new().read_from_container(&mut bufreader) {
                exif.fields()
                    .map(|f| Self::format_exif_field(f, &exif))
                    .collect()
            } else {
                vec![]
            };
        Ok(Self {
            length: content.len(),
            content,
        })
    }

    fn format_exif_field(f: &exif::Field, exif: &exif::Exif) -> String {
        format!(
            "{} {} {}",
            f.tag,
            f.ifd_num,
            f.display_value().with_unit(exif)
        )
    }

    fn len(&self) -> usize {
        self.length
    }
}

/// Holds media info about a "media" file (mostly videos and audios).
/// Requires the [`mediainfo`](https://mediaarea.net/) executable installed in path.
#[derive(Clone)]
pub struct MediaContent {
    length: usize,
    /// The media info details.
    content: Vec<String>,
}

impl MediaContent {
    fn new(path: &Path) -> FmResult<Self> {
        let content: Vec<String>;
        if let Ok(output) = std::process::Command::new("mediainfo").arg(path).output() {
            let s = String::from_utf8(output.stdout).unwrap_or_default();
            content = s.lines().map(|s| s.to_owned()).collect();
        } else {
            content = vec![];
        }
        Ok(Self {
            length: content.len(),
            content,
        })
    }

    fn len(&self) -> usize {
        self.length
    }
}

/// Holds a path to an image and a method to convert it into an ugly thumbnail.
#[derive(Clone)]
pub struct Pixels {
    pub img_path: PathBuf,
}

impl Pixels {
    /// Creates a new preview instance. It simply holds a path.
    fn new(img_path: PathBuf) -> FmResult<Self> {
        Ok(Self { img_path })
    }

    /// Tries to scale down the image to be displayed in the terminal canvas.
    /// Fastest algorithm is used (nearest neighbor) since the result is always
    /// ugly nonetheless.
    /// It may be fun to show to non geek users :)
    pub fn resized_rgb8(&self, width: u32, height: u32) -> FmResult<ImageBuffer<Rgb<u8>, Vec<u8>>> {
        let img = image::open(&self.img_path)?;
        Ok(img.resize(width, height, FilterType::Nearest).to_rgb8())
    }
}

/// Display a tree view of a directory.
/// The "tree view" is calculated recursively. It may take some time
/// if the directory has a lot of children.
#[derive(Clone, Debug)]
pub struct Directory {
    pub content: Vec<String>,
    len: usize,
}

impl Directory {
    fn new(path: &Path) -> FmResult<Self> {
        let tree = tree_view(
            path.to_str().ok_or_else(|| {
                FmError::custom("Directory Preview", "Can't parse the filename to str")
            })?,
            10,
        )?;
        let tree_str = format!("{}", tree);
        let content: Vec<String> = tree_str.lines().map(|s| s.to_owned()).collect();
        let len = content.len();

        Ok(Self { content, len })
    }

    fn len(&self) -> usize {
        self.len
    }
}

/// Common trait for many preview methods which are just a bunch of lines with
/// no specific formatting.
/// Some previewing (thumbnail and syntaxed text) needs more details.
pub trait Window<T> {
    fn window(
        &self,
        top: usize,
        bottom: usize,
        length: usize,
    ) -> Take<Skip<Enumerate<Iter<'_, T>>>>;
}

impl Window<Vec<SyntaxedString>> for HLContent {
    fn window(
        &self,
        top: usize,
        bottom: usize,
        length: usize,
    ) -> std::iter::Take<Skip<Enumerate<Iter<'_, Vec<SyntaxedString>>>>> {
        self.content
            .iter()
            .enumerate()
            .skip(top)
            .take(min(length, bottom + 1))
    }
}

macro_rules! impl_window {
    ($t:ident, $u:ident) => {
        impl Window<$u> for $t {
            fn window(
                &self,
                top: usize,
                bottom: usize,
                length: usize,
            ) -> Take<Skip<Enumerate<Iter<'_, $u>>>> {
                self.content
                    .iter()
                    .enumerate()
                    .skip(top)
                    .take(min(length, bottom + 1))
            }
        }
    };
}

impl_window!(TextContent, String);
impl_window!(BinaryContent, Line);
impl_window!(PdfContent, String);
impl_window!(ZipContent, String);
impl_window!(ExifContent, String);
impl_window!(MediaContent, String);
impl_window!(Directory, String);

fn is_ext_zip(ext: &str) -> bool {
    matches!(
        ext,
        "zip" | "gzip" | "bzip2" | "xz" | "lzip" | "lzma" | "tar" | "mtree" | "raw" | "7z"
    )
}
fn is_ext_image(ext: &str) -> bool {
    matches!(ext, "png" | "jpg" | "jpeg" | "tiff" | "heif")
}

fn is_ext_media(ext: &str) -> bool {
    matches!(
        ext,
        "mkv"
            | "ogg"
            | "ogm"
            | "riff"
            | "mpeg"
            | "mp2"
            | "mp3"
            | "mp4"
            | "wm"
            | "qt"
            | "ac3"
            | "dts"
            | "aac"
            | "mac"
            | "flac"
            | "avi"
    )
}

fn is_ext_pdf(ext: &str) -> bool {
    ext == "pdf"
}

fn filename_as_string<P: AsRef<Path>>(p: P) -> String {
    p.as_ref()
        .file_name()
        .unwrap_or_default()
        .to_str()
        .unwrap_or_default()
        .to_owned()
}

fn tree_view<P: AsRef<Path>>(p: P, max_depth: usize) -> std::io::Result<Tree<String>> {
    Ok(std::fs::read_dir(&p)?.filter_map(|e| e.ok()).fold(
        Tree::new(filename_as_string(p.as_ref().canonicalize()?)),
        |mut root, entry| {
            if max_depth > 0 {
                if let Ok(dir) = entry.metadata() {
                    if dir.is_dir() {
                        if let Ok(tree) = tree_view(entry.path(), max_depth - 1) {
                            root.push(tree);
                        }
                    } else {
                        root.push(Tree::new(filename_as_string(entry.path())));
                    }
                }
            }
            root
        },
    ))
}

fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> std::thread::Result<R> {
    let prev_hook = panic::take_hook();
    panic::set_hook(Box::new(|_| {}));
    let result = panic::catch_unwind(f);
    panic::set_hook(prev_hook);
    result
}