summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_footer.rs
blob: 3e044f4311bb6dd07145318bbf691745409dda34 (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
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::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 as f64);

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

        if self.entry.metadata.file_type.is_symlink() {
            if let Ok(path) = fs::read_link(self.entry.file_path()) {
                text.push(Text::styled(" -> ", mode_style));
                if let Some(s) = path.to_str() {
                    text.push(Text::styled(s.to_string(), mode_style));
                }
            }
        }

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