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

use crate::fs::{FileType, 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 skip_dist = curr_index / area.height as usize * area.height as usize;
        let screen_index = curr_index % area.height as usize;

        let drawing_width = area.width as usize;
        let space_fill = " ".repeat(drawing_width);

        let x_start = x + 1;

        for (i, entry) in self
            .dirlist
            .iter()
            .skip(skip_dist)
            .enumerate()
            .take(area.height as usize)
        {
            let name = entry.label();
            let name_width = name.width();

            let style = if i == screen_index {
                entry.get_style().add_modifier(Modifier::REVERSED)
            } else {
                entry.get_style()
            };

            buf.set_string(x, y + i as u16, space_fill.as_str(), style);

            match entry.metadata.file_type() {
                FileType::Directory => {
                    if name_width <= drawing_width {
                        buf.set_string(x_start, y + i as u16, name, style);
                    } else {
                        buf.set_stringn(x_start, y + i as u16, name, drawing_width - 1, style);
                        buf.set_string(x_start + drawing_width as u16 - 1, y + i as u16, ELLIPSIS, style);
                    }
                }
                FileType::Symlink(_) => {
                    if name_width < drawing_width - 4 {
                        buf.set_string(x_start, y + i as u16, name, style);
                        buf.set_string(x_start + drawing_width as u16 - 4, y + i as u16, "->", style);
                    } else {
                        buf.set_stringn(x_start, y + i as u16, name, drawing_width - 1, style);
                        buf.set_string(x_start + drawing_width as u16 - 1, y + i as u16, ELLIPSIS, style);
                    }
                }
                FileType::File => {
                    if name_width < drawing_width - FILE_SIZE_WIDTH {
                        buf.set_stringn(x_start, y + i as u16, name, drawing_width - FILE_SIZE_WIDTH, style);
                    } else {
                        match name.rfind('.') {
                            None => {
                                buf.set_stringn(
                                    x_start,
                                    y + i as u16,
                                    name,
                                    drawing_width - FILE_SIZE_WIDTH,
                                    style,
                                );
                            }
                            Some(p_ind) => {
                                let ext_width = name[p_ind..].width();
                                let file_name_width =
                                    if ext_width > drawing_width - FILE_SIZE_WIDTH - 2 {
                                        0
                                    } else {
                                        drawing_width - FILE_SIZE_WIDTH - ext_width - 2
                                    };

                                buf.set_stringn(
                                    x_start,
                                    y + i as u16,
                                    &name[..p_ind],
                                    file_name_width,
                                    style,
                                );
                                buf.set_string(
                                    x_start + file_name_width as u16,
                                    y + i as u16,
                                    ELLIPSIS,
                                    style,
                                );

                                let file_ext_width =
                                    drawing_width - file_name_width - FILE_SIZE_WIDTH - 2;

                                buf.set_stringn(
                                    x_start + file_name_width as u16 + 1,
                                    y + i as u16,
                                    &name[p_ind..],
                                    file_ext_width,
                                    style,
                                );
                            }
                        }
                    }
                    let file_size_string = format::file_size_to_string(entry.metadata.len());
                    buf.set_string(
                        x_start + (drawing_width - FILE_SIZE_WIDTH) as u16,
                        y + i as u16,
                        file_size_string,
                        style,
                    );
                }
            }
        }
    }
}