summaryrefslogtreecommitdiffstats
path: root/src/preview/preview_file.rs
blob: ab734287f985f9c889326e9b6c1edf7e6d5956d9 (plain)
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
use std::fmt::Debug;
use std::{process::Output, time};

pub enum PreviewFileState {
    Loading,
    Error(String),
    Success(FilePreview),
}

impl Debug for PreviewFileState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Loading => f.debug_tuple("Loading").finish(),
            Self::Error(message) => f.debug_tuple("Error").field(message).finish(),
            Self::Success(_) => f.debug_tuple("Success").finish(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct FilePreview {
    pub status: std::process::ExitStatus,
    pub output: String,
    pub index: usize,
    pub modified: time::SystemTime,
}

impl std::convert::From<Output> for FilePreview {
    fn from(output: Output) -> Self {
        let s = String::from_utf8_lossy(&output.stdout).to_string();
        let s2 = s.replace('\t', "        ");
        let status = output.status;
        let modified = time::SystemTime::now();
        Self {
            status,
            output: s2,
            modified,
            index: 0,
        }
    }
}