summaryrefslogtreecommitdiffstats
path: root/src/listview.rs
blob: 0cbdd634e31afa36fcd95d008e45ade267fd0b36 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use rayon::prelude::*;
use termion::event::{Event, Key};
use unicode_width::UnicodeWidthStr;

use std::path::{Path, PathBuf};

use crate::coordinates::{Coordinates, Position, Size};
use crate::files::{File, Files};
use crate::term;
use crate::widget::Widget;

// Maybe also buffer drawlist for efficiency when it doesn't change every draw

pub struct ListView<T>
where
    T: Send,
{
    pub content: T,
    selection: usize,
    offset: usize,
    buffer: Vec<String>,
    // dimensions: (u16, u16),
    // position: (u16, u16),
    coordinates: Coordinates,
}

impl<T> ListView<T>
where
    ListView<T>: Widget,
    T: Send,
{
    pub fn new(content: T) -> Self {
        let view = ListView::<T> {
            content: content,
            selection: 0,
            offset: 0,
            buffer: Vec::new(),
            coordinates: Coordinates {
                size: Size((1, 1)),
                position: Position((1, 1)),
            }, // dimensions: (1,1),
               // position: (1,1)
        };
        view
    }

    fn move_up(&mut self) {
        if self.selection == 0 {
            return;
        }

        if self.selection - self.offset <= 0 {
            self.offset -= 1;
        }

        self.selection -= 1;
    }
    fn move_down(&mut self) {
        let lines = self.buffer.len();
        let y_size = self.coordinates.ysize() as usize;

        if self.selection == lines - 1 {
            return;
        }

        if self.selection + 1 >= y_size && self.selection + 1 - self.offset >= y_size {
            self.offset += 1;
        }

        self.selection += 1;
    }

    fn set_selection(&mut self, position: usize) {
        let ysize = self.coordinates.ysize() as usize;
        let mut offset = 0;

        while position + 1 > ysize + offset {
            offset += 1
        }

        self.offset = offset;
        self.selection = position;
    }

    fn render_line(&self, file: &File) -> String {
        let name = &file.name;
        let (size, unit) = file.calculate_size();

        let xsize = self.get_size().xsize();
        let sized_string = term::sized_string(&name, xsize);

        format!(
            "{}{}{}{}{}",
            match &file.color {
                Some(color) => format!("{}{:padding$}",
                                       term::from_lscolor(color),
                                       &sized_string,
                                       padding = xsize as usize),
                _ => format!("{}{:padding$}",
                             term::normal_color(),
                             &sized_string,
                             padding = xsize as usize),
            } ,
            term::highlight_color(),
            term::cursor_left(size.to_string().width() + unit.width()),
            size,
            unit
        )
    }
}

impl ListView<Files>
where
    ListView<Files>: Widget,
    Files: std::ops::Index<usize, Output = File>,
    Files: std::marker::Sized,
{
    pub fn selected_file(&self) -> &File {
        let selection = self.selection;
        let file = &self.content[selection];
        file
    }

    pub fn clone_selected_file(&self) -> File {
        let selection = self.selection;
        let file = self.content[selection].clone();
        file
    }

    pub fn grand_parent(&self) -> Option<PathBuf> {
        self.selected_file().grand_parent()
    }

    pub fn goto_grand_parent(&mut self) {
        match self.grand_parent() {
            Some(grand_parent) => self.goto_path(&grand_parent),
            None => self.show_status("Can't go further!"),
        }
    }

    fn goto_selected(&mut self) {
        let path = self.selected_file().path();

        self.goto_path(&path);
    }

    pub fn goto_path(&mut self, path: &Path) {
        match crate::files::Files::new_from_path(path) {
            Ok(files) => {
                self.content = files;
                self.selection = 0;
                self.offset = 0;
                self.refresh();
            }
            Err(err) => {
                self.show_status(&format!("Can't open this path: {}", err));
                return;
            }
        }
    }

    pub fn select_file(&mut self, file: &File) {
        let pos = self
            .content
            .files
            .par_iter()
            .position_any(|item| item == file)
            .unwrap();
        self.set_selection(pos);
    }

    fn cycle_sort(&mut self) {
        let file = self.clone_selected_file();
        self.content.cycle_sort();
        self.content.sort();
        self.select_file(&file);
        self.refresh();
        self.show_status(&format!("Sorting by: {}", self.content.sort));
    }

    fn toggle_dirs_first(&mut self) {
        let file = self.clone_selected_file();
        self.content.dirs_first = !self.content.dirs_first;
        self.content.sort();
        self.select_file(&file);
        self.refresh();
        self.show_status(&format!("Direcories first: {}", self.content.dirs_first));
    }

    fn exec_cmd(&mut self) {
        match self.minibuffer("exec ($s for selected files)") {
            Some(cmd) => {
                self.show_status(&format!("Running: \"{}\"", &cmd));

                let filename = self.selected_file().name.clone();
                let cmd = cmd.replace("$s", &format!("{}", &filename));

                let status = std::process::Command::new("sh")
                    .arg("-c")
                    .arg(&cmd)
                    .status();
                match status {
                    Ok(status) => self.show_status(&format!("\"{}\" exited with {}", cmd, status)),
                    Err(err) => self.show_status(&format!("Can't run this \"{}\": {}", cmd, err)),
                }
            }
            None => self.show_status(""),
        }
    }

    fn render(&self) -> Vec<String> {
        self.content
            .files
            .par_iter()
            .map(|file| self.render_line(&file))
            .collect()
    }
}

impl Widget for ListView<Files> {
    fn get_size(&self) -> &Size {
        &self.coordinates.size
    }
    fn get_position(&self) -> &Position {
        &self.coordinates.position
    }
    fn set_size(&mut self, size: Size) {
        self.coordinates.size = size;
    }
    fn set_position(&mut self, position: Position) {
        self.coordinates.position = position;
    }
    fn get_coordinates(&self) -> &Coordinates {
        &self.coordinates
    }
    fn set_coordinates(&mut self, coordinates: &Coordinates) {
        if self.coordinates == *coordinates {
            return;
        }
        self.coordinates = coordinates.clone();
        self.refresh();
    }
    fn refresh(&mut self) {
        self.buffer = self.render();
    }


    fn get_drawlist(&self) -> String {
        let mut output = term::reset();