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

use crate::config::clean::app::AppConfig;
use crate::fs::{FileType, JoshutoMetadata};
use crate::fs::{JoshutoDirEntry, JoshutoDirList};
use crate::ui::widgets::trim_file_label;
use crate::util::style;

pub struct TuiDirList<'a> {
    pub config: &'a AppConfig,
    pub dirlist: &'a JoshutoDirList,
    pub focused: bool,
}

impl<'a> TuiDirList<'a> {
    pub fn new(config: &'a AppConfig, dirlist: &'a JoshutoDirList, focused: bool) -> Self {
        Self {
            config,
            dirlist,
            focused,
        }
    }
}

impl<'a> Widget for TuiDirList<'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();

        if self.dirlist.contents.is_empty() {
            let style = Style::default().bg(Color::Red).fg(Color::White);
            buf.set_stringn(x, y, "empty", area.width as usize, style);
            return;
        }

        let curr_index = self.dirlist.get_index().unwrap();
        let skip_dist = self.dirlist.first_index_for_viewport();

        let drawing_width = area.width as usize;

        let space_fill = " ".repeat(drawing_width);

        self.dirlist
            .iter()
            .skip(skip_dist)
            .enumerate()
            .take(area.height as usize)
            .for_each(|(i, entry)| {
                let ix = skip_dist + i;

                let style = if !self.focused {
                    style::entry_style(self.config, entry)
                } else if ix == curr_index {
                    style::entry_style(self.config, entry).add_modifier(Modifier::REVERSED)
                } else {
                    style::entry_style(self.config, entry)
                };

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

                print_entry(
                    self.config,
                    buf,
                    entry,
                    style,
                    (x + 1, y + i as u16),
                    drawing_width - 1,
                );
            });
    }
}

fn print_entry(
    config: &AppConfig,
    buf: &mut Buffer,
    entry: &JoshutoDirEntry,
    style: Style,
    (x, y): (u16, u16),
    drawing_width: usize,
) {
    let name = entry.file_name();
    #[cfg(feature = "devicons")]
    let (label, label_width) = {
        if config.display_options_ref().show_icons() {
            let icon = get_entry_icon(config, entry.file_name(), entry.ext(), &entry.metadata);
            let label = format!("{icon} {name}");
            let label_width = label.width();
            (label, label_width)
        } else {
            (name.to_string(), name.width())
        }
    };

    #[cfg(not(feature = "devicons"))]
    let (label, label_width) = {
        let label = name.to_string();
        let label_width = label.width();
        (label, label_width)
    };

    let label = if label_width > drawing_width {
        trim_file_label(&label, drawing_width)
    } else {
        label.to_string()
    };
    buf.set_string(x, y, label, style);
}

#[cfg(feature = "devicons")]
pub fn get_entry_icon(
    config: &AppConfig,
    name: &str,
    ext: Option<&str>,
    metadata: &JoshutoMetadata,
) -> &'static str {
    use crate::ICONS_T;

    if let FileType::Directory = metadata.file_type() {
        return ICONS_T
            .directory_exact
            .get(name)
            .map(|s| s.as_str())
            .unwrap_or(ICONS_T.default_dir.as_str());
    }
    ICONS_T
        .file_exact
        .get(name)
        .map(|s| s.as_str())
        .unwrap_or_else(|| {
            ext.and_then(|ext| {
                let ext: String = if config.case_insensitive_ext {
                    ext.to_lowercase()
                } else {
                    ext.to_owned()
                };
                ICONS_T.ext.get(&ext).map(|s| s.as_str())
            })
            .unwrap_or_else(|| ICONS_T.default_file.as_str())
        })
}