summaryrefslogtreecommitdiffstats
path: root/src/preview/zero_len_file_view.rs
blob: 5ec304517110c41a5464fe93fc4dd1934d9646ae (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use {
    crate::{
        display::{CropWriter, SPACE_FILLING, Screen, W},
        errors::ProgramError,
        skin::PanelSkin,
    },
    char_reader::CharReader,
    crossterm::{
        cursor,
        QueueableCommand,
    },
    std::{
        fs::File,
        path::PathBuf,
    },
    termimad::{Area},
};

/// a (light) display for a file declaring a size 0,
/// as happens for many system "files", for example in /proc
pub struct ZeroLenFileView {
    path: PathBuf,
}

impl ZeroLenFileView {
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
        }
    }
    pub fn display(
        &mut self,
        w: &mut W,
        _screen: Screen,
        panel_skin: &PanelSkin,
        area: &Area,
    ) -> Result<(), ProgramError> {
        let styles = &panel_skin.styles;
        let line_count = area.height as usize;
        let file = File::open(&self.path)?;
        let mut reader = CharReader::new(file);
        // line_len here is in chars, and we crop in cols, but it's OK because both
        // are usually identical for system files and we crop later anyway
        let line_len = area.width as usize;
        for y in 0..line_count {
            w.queue(cursor::MoveTo(area.left, y as u16 + area.top))?;
            let mut cw = CropWriter::new(w, area.width as usize);
            let cw = &mut cw;
            if let Some(line) = reader.next_line(line_len, 15_000)? {
                cw.queue_str(&styles.default, &line)?;
            }
            cw.fill(&styles.default, &SPACE_FILLING)?;
        }
        Ok(())
    }
}