summaryrefslogtreecommitdiffstats
path: root/src/display.rs
blob: 8bcbb7f41ddf747f2028d8991f90acda0f62bbb6 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use std::cmp::min;
use std::sync::Arc;

use log::info;
use sysinfo::{DiskExt, System, SystemExt};
use tuikit::attr::*;
use tuikit::term::Term;

use crate::config::Colors;
use crate::content_window::ContentWindow;
use crate::fileinfo::{fileinfo_attr, human_size};
use crate::fm_error::{FmError, FmResult};
use crate::git::git;
use crate::last_edition::LastEdition;
use crate::mode::{MarkAction, Mode};
use crate::preview::Preview;
use crate::status::Status;
use crate::tab::Tab;

/// Is responsible for displaying content in the terminal.
/// It uses an already created terminal.
pub struct Display {
    /// The Tuikit terminal attached to the display.
    /// It will print every symbol shown on screen.
    pub term: Arc<Term>,
    colors: Colors,
    /// system info
    sys: System,
}

impl Display {
    /// Returns a new `Display` instance from a `tuikit::term::Term` object.
    pub fn new(term: Arc<Term>, colors: Colors) -> Self {
        Self {
            term,
            colors,
            sys: System::new_all(),
        }
    }

    const EDIT_BOX_OFFSET: usize = 10;
    const SORT_CURSOR_OFFSET: usize = 36;
    const ATTR_LINE_NR: Attr = Attr {
        fg: Color::CYAN,
        bg: Color::Default,
        effect: Effect::empty(),
    };
    const ATTR_YELLOW: Attr = Attr {
        fg: Color::YELLOW,
        bg: Color::Default,
        effect: Effect::empty(),
    };
    /// Display every possible content in the terminal.
    ///
    /// The top line
    ///
    /// The files if we're displaying them
    ///
    /// The cursor if a content is editable
    ///
    /// The help if `Mode::Help`
    ///
    /// The jump_list if `Mode::Jump`
    ///
    /// The completion list if any.
    ///
    /// The preview in preview mode.
    pub fn display_all(&mut self, status: &Status) -> FmResult<()> {
        // let status = status.selected_non_mut();
        match status.selected_non_mut().mode {
            // Mode::Help => self.help(),
            Mode::Jump => self.jump_list(status),
            Mode::History => self.history(status),
            Mode::Exec | Mode::Goto | Mode::Search => self.completion(status),
            Mode::NeedConfirmation => self.confirmation(status),
            Mode::Preview | Mode::Help => self.preview(status),
            Mode::Shortcut => self.shortcuts(status),
            Mode::Marks(MarkAction::New) | Mode::Marks(MarkAction::Jump) => self.marks(status),
            _ => self.files(status),
        }?;
        self.cursor(status)?;
        self.first_line(status)
    }

    /// Reads and returns the `tuikit::term::Term` height.
    pub fn height(&self) -> FmResult<usize> {
        let (_, height) = self.term.term_size()?;
        Ok(height)
    }

    /// Display the top line on terminal.
    /// Its content depends on the mode.
    /// In normal mode we display the path and number of files.
    /// When a confirmation is needed we ask the user to input `'y'` or
    /// something else.
    fn first_line(&mut self, status: &Status) -> FmResult<()> {
        let tab = status.selected_non_mut();
        let first_row: String = match tab.mode {
            Mode::Normal => {
                format!(
                    "Tab: {}/{}  --  Path: {}   --   Files: {} -- Sum: {} -- Avail: {}  -- {}",
                    status.index + 1,
                    status.len(),
                    tab.path_content.path_to_str()?,
                    tab.path_content.files.len(),
                    human_size(tab.path_content.used_space),
                    human_size(self.disk_space(tab.path_str().unwrap())),
                    git(&tab.path_content.path)?,
                )
            }
            Mode::NeedConfirmation => {
                format!("Confirm {} (y/n) : ", tab.last_edition)
            }
            Mode::Preview => match tab.path_content.selected_file() {
                Some(fileinfo) => {
                    format!("{:?} {}", tab.mode.clone(), fileinfo.path.to_string_lossy())
                }
                None => "".to_owned(),
            },
            Mode::Help => "fm: a dired like file manager. Default keybindings.".to_owned(),
            Mode::Marks(MarkAction::Jump) => "Jump to...".to_owned(),
            Mode::Marks(MarkAction::New) => "Save mark...".to_owned(),
            _ => {
                format!("{:?} {}", tab.mode.clone(), tab.input.string.clone())
            }
        };
        self.term
            .print_with_attr(0, 0, &first_row, Self::ATTR_LINE_NR)?;
        Ok(())
    }

    /// Displays the current directory content, one line per item like in
    /// `ls -l`.
    ///
    /// Those files are always shown, which make it a little bit faster in;
    /// normal (ie. default) mode.
    /// When there's too much files, only those around the selected one are
    /// displayed.
    fn files(&mut self, status: &Status) -> FmResult<()> {
        let tab = status.selected_non_mut();
        let len = tab.path_content.files.len();
        for (i, (file, string)) in std::iter::zip(
            tab.path_content.files.iter(),
            tab.path_content.strings().iter(),
        )
        .enumerate()
        .take(min(len, tab.window.bottom + 1))
        .skip(tab.window.top)
        {
            let row = i + ContentWindow::WINDOW_MARGIN_TOP - tab.window.top;
            let mut attr = fileinfo_attr(status, file, &self.colors);
            if status.flagged.contains(&file.path) {
                attr.effect |= Effect::BOLD | Effect::UNDERLINE;
            }
            self.term.print_with_attr(row, 0, string, attr)?;
        }
        Ok(())
    }

    /// Display a cursor in the top row, at a correct column.
    fn cursor(&mut self, status: &Status) -> FmResult<()> {
        let tab = status.selected_non_mut();
        match tab.mode {
            Mode::Normal | Mode::Help | Mode::Marks(_) => {
                self.term.show_cursor(false)?;
            }
            Mode::NeedConfirmation => {
                self.term.set_cursor(0, tab.last_edition.offset())?;
            }
            Mode::Sort => {
                self.term.set_cursor(0, Self::SORT_CURSOR_OFFSET)?;
            }
            _ => {
                self.term
                    .set_cursor(0, tab.input.cursor_index + Self::EDIT_BOX_OFFSET)?;
            }
        }
        Ok(())
    }

    /// Display the possible jump destination from flagged files.
    fn jump_list(&mut self, tabs: &Status) -> FmResult<()> {
        self.term.clear()?;
        self.term.print(0, 0, "Jump to...")?;
        for (row, path) in tabs.flagged.iter().enumerate