summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_dirlist_detailed.rs
blob: c6cd9ba86b969a5143b6b29315714864c555ac20 (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
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
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Modifier, Style};
use tui::widgets::Widget;
use unicode_width::UnicodeWidthStr;

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

const FILE_SIZE_WIDTH: usize = 8;

const ELLIPSIS: &str = "…";

pub struct TuiDirListDetailed<'a> {
    dirlist: &'a JoshutoDirList,
}

impl<'a> TuiDirListDetailed<'a> {
    pub fn new(dirlist: &'a JoshutoDirList) -> Self {
        Self { dirlist }
    }
}

impl<'a> Widget for TuiDirListDetailed<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width < 4 || area.height < 1 {
            return;
        }

        let x = area.left();
        let y = area.top();
        let curr_index = match self.dirlist.index {
            Some(i) => i,
            None => {
                let style = Style::default().bg(Color::Red).fg(Color::White);
                buf.set_stringn(x, y, "empty", area.width as usize, style);
                return;
            }
        };

        let drawing_width = area.width as usize - 2;
        let skip_dist = curr_index / area.height as usize * area.height as usize;
        for (i, entry) in self
            .dirlist
            .iter()
            .skip(skip_dist)
            .enumerate()
            .take(area.height as usize)
        {
            let style = entry.get_style();
            print_entry(buf, entry, style, (x + 1, y + i as u16), drawing_width);
        }
        {
            let screen_index = curr_index % area.height as usize;
            let space_fill = " ".repeat(drawing_width + 1);

            let entry = self.dirlist.curr_entry_ref().unwrap();
            let style = {
                let s = entry.get_style().add_modifier(Modifier::REVERSED);
                buf.set_string(x, y + screen_index as u16, space_fill.as_str(), s);
                s
            };
            print_entry(
                buf,
                entry,
                style,
                (x + 1, y + screen_index as u16),
                drawing_width,
            );
        }
    }
}

fn print_entry(
    buf: &mut Buffer,
    entry: &JoshutoDirEntry,
    style: Style,
    (x, y): (u16, u16),
    drawing_width: usize,
) {
    let name = entry.label();
    let name_width = name.width();

    match entry.metadata.file_type() {
        FileType::Directory => {
            // print filename
            buf.set_stringn(x, y, name, drawing_width - 1, style);
            if name_width > drawing_width {
                buf.set_string(x + drawing_width as u16 - 1, y, ELLIPSIS, style);
            }
        }
        FileType::Symlink(_) => {
            // print filename
            buf.set_stringn(x, y, name, drawing_width - 1, style);
            buf.set_string(x + drawing_width as u16 - 4, y, "->", style);
            if name_width >= drawing_width - 4 {
                buf.set_string(x + drawing_width as u16 - 1, y, ELLIPSIS, style);
            }
        }
        FileType::File => {
            if drawing_width < FILE_SIZE_WIDTH {
                return;
            }
            let file_drawing_width = drawing_width - FILE_SIZE_WIDTH;

            let (stem, extension) = match name.rfind('.') {
                None => (name, ""),
                Some(i) => name.split_at(i),
            };
            if stem.is_empty() {
                let ext_width = extension.width();
                buf.set_stringn(x, y, extension, file_drawing_width, style);
                if ext_width > drawing_width {
                    buf.set_string(x + drawing_width as u16 - 1, y, ELLIPSIS, style);
                }
            } else if extension.is_empty() {
                let stem_width = stem.width();
                buf.set_stringn(x, y, stem, file_drawing_width, style);
                if stem_width > file_drawing_width {
                    buf.set_string(x + drawing_width as u16 - 1, y, ELLIPSIS, style);
                }
            } else {
                let stem_width = stem.width();
                let ext_width = extension.width();
                buf.set_stringn(x, y, stem, file_drawing_width, style);
                if stem_width + ext_width > file_drawing_width {
                    let ext_start_idx = if file_drawing_width < ext_width {
                        0
                    } else {
                        (file_drawing_width - ext_width) as u16
                    };
                    buf.set_string(x + ext_start_idx, y, extension, style);
                    let ext_start_idx = if ext_start_idx > 0 {
                        ext_start_idx - 1
                    } else {
                        0
                    };
                    buf.set_string(x + ext_start_idx, y, ELLIPSIS, style);
                } else {
                    buf.set_string(x + stem_width as u16, y, extension, style);
                }
            }
            // print file size
            let file_size_string = format::file_size_to_string(entry.metadata.len());
            buf.set_string(x + file_drawing_width as u16, y, " ", style);
            buf.set_string(
                x + file_drawing_width as u16 + 1,
                y,
                file_size_string,
                style,
            );
        }
    }
}