summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_footer.rs
blob: 83598a27d9c4e76e2c1fa0a410de61c83cdab0b4 (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
57
58
59
60
61
62
63
64
use std::fs;

use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Paragraph, Text, Widget};

use crate::fs::{FileType, JoshutoDirEntry};
use crate::util::format;

pub struct TuiFooter<'a> {
    entry: &'a JoshutoDirEntry,
}

impl<'a> TuiFooter<'a> {
    pub fn new(entry: &'a JoshutoDirEntry) -> Self {
        Self { entry }
    }
}

impl<'a> Widget for TuiFooter<'a> {
    fn draw(&mut self, area: Rect, buf: &mut Buffer) {
        use std::os::unix::fs::PermissionsExt;

        let mode = self.entry.metadata.permissions.mode();
        let mode = format::mode_to_string(mode);

        let mode_style = Style::default().fg(Color::Cyan);

        let mtime = self.entry.metadata.modified;
        let mtime = format::mtime_to_string(mtime);

        let size = self.entry.metadata.len;
        let size = format::file_size_to_string(size);

        #[cfg(unix)]
        let mimetype = match self.entry.metadata.mimetype.as_ref() {
            Some(s) => s,
            None => "",
        };

        let mut text = vec![
            Text::styled(mode, mode_style),
            Text::raw("  "),
            Text::raw(mtime),
            Text::raw("  "),
            Text::raw(size),
            #[cfg(unix)]
            Text::raw("  "),
            #[cfg(unix)]
            Text::raw(mimetype),
        ];

        match &self.entry.metadata.file_type {
            FileType::Symlink(s) => {
                text.push(Text::styled(" -> ", mode_style));
                text.push(Text::styled(s, mode_style));
            }
            _ => {}
        }

        Paragraph::new(text.iter()).wrap(true).draw(area, buf);
    }
}