tui-file-explorer 2.0.2

A self-contained, keyboard-driven file-browser widget for Ratatui
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use super::*;

// ── Theme panel ───────────────────────────────────────────────────────────────

/// Render the slide-in theme-picker panel occupying `area`.
///
/// The panel is divided into three vertical zones:
/// - A controls header showing the `[` / `t` key hints.
/// - A scrollable list of all available themes.
/// - A description footer for the currently selected theme.
pub fn render_theme_panel(frame: &mut Frame, area: Rect, app: &App) {
    let theme = app.theme();

    // Three-row vertical layout: controls | list | description.
    let v = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(0),
            Constraint::Length(4),
        ])
        .split(area);

    // Controls header.
    let controls = Paragraph::new(Line::from(vec![
        Span::styled(" ↑ [ ", Style::default().fg(theme.dim)),
        Span::styled("prev", Style::default().fg(theme.accent)),
        Span::styled("   ", Style::default().fg(theme.dim)),
        Span::styled("↓ t ", Style::default().fg(theme.dim)),
        Span::styled("next", Style::default().fg(theme.accent)),
    ]))
    .block(
        Block::default()
            .title(Span::styled(
                " \u{1F3A8} Themes ",
                Style::default()
                    .fg(theme.brand)
                    .add_modifier(Modifier::BOLD),
            ))
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(theme.accent)),
    );
    frame.render_widget(controls, v[0]);

    // Scrollable theme list — keep the selected item in view.
    let visible = v[1].height.saturating_sub(2) as usize;
    let scroll = if app.theme_idx >= visible {
        app.theme_idx - visible + 1
    } else {
        0
    };

    let items: Vec<ListItem> = app
        .themes
        .iter()
        .enumerate()
        .skip(scroll)
        .take(visible)
        .map(|(i, (name, _, _))| {
            let is_active = i == app.theme_idx;
            let marker = if is_active { "\u{25BA} " } else { "   " };
            let line = Line::from(vec![
                Span::styled(
                    format!("{marker}{:>2}. ", i + 1),
                    Style::default().fg(if is_active { theme.brand } else { theme.dim }),
                ),
                Span::styled(
                    name.to_string(),
                    if is_active {
                        Style::default()
                            .fg(theme.accent)
                            .add_modifier(Modifier::BOLD)
                    } else {
                        Style::default().fg(theme.fg)
                    },
                ),
            ]);
            if is_active {
                ListItem::new(line).style(Style::default().bg(theme.sel_bg))
            } else {
                ListItem::new(line)
            }
        })
        .collect();

    let mut list_state = ListState::default();
    list_state.select(Some(app.theme_idx.saturating_sub(scroll)));

    let list = List::new(items).block(
        Block::default()
            .borders(Borders::LEFT | Borders::RIGHT)
            .border_style(Style::default().fg(theme.accent)),
    );
    frame.render_stateful_widget(list, v[1], &mut list_state);

    // Description footer.
    let desc_text = format!("{}\n{}", app.theme_name(), app.theme_desc());
    let desc = Paragraph::new(desc_text)
        .style(Style::default().fg(theme.success))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(theme.accent)),
        );
    frame.render_widget(desc, v[2]);
}

// ── Editor panel ──────────────────────────────────────────────────────────────

/// Render the slide-in editor-picker side panel occupying `area`.
///
/// Two bordered group cells — "Terminal Editors" and "IDEs & GUI Editors" —
/// mirror the Options panel layout.  The highlighted row (cursor) is tracked
/// by `app.editor_panel_idx`; the active editor is marked with a `✓`.
pub fn render_editor_panel(frame: &mut Frame, area: Rect, app: &App) {
    use crate::app::{App as TfeApp, Editor};

    let theme = app.theme();

    let on_style = Style::default()
        .fg(theme.success)
        .add_modifier(Modifier::BOLD);
    let key_style = Style::default()
        .fg(theme.accent)
        .add_modifier(Modifier::BOLD);
    let label_style = Style::default().fg(theme.fg);
    let subtitle_style = Style::default().fg(theme.dim);
    let title_style = Style::default()
        .fg(theme.brand)
        .add_modifier(Modifier::BOLD);

    let editors = TfeApp::all_editors();
    let first_ide = TfeApp::first_ide_idx();
    let terminal_editors = &editors[..first_ide]; // None … Emacs
    let ide_editors = &editors[first_ide..]; // Sublime … Eclipse

    // ── Layout ───────────────────────────────────────────────────────────────
    // Slots (top to bottom):
    //   [0]  hints header box              — 2 rows
    //   [1]  gap                           — 1 row
    //   [2]  "Terminal Editors" title      — 1 row
    //   [3]  Terminal Editors cell         — terminal_editors.len() + 2 (borders)
    //   [4]  gap                           — 1 row
    //   [5]  "IDEs & GUI Editors" title    — 1 row
    //   [6]  IDEs cell                     — ide_editors.len() + 2 (borders)
    //   [7]  gap                           — 1 row
    //   [8]  footer                        — 3 rows
    //   [9]  remainder
    let terminal_cell_h = terminal_editors.len() as u16 + 2;
    let ide_cell_h = ide_editors.len() as u16 + 2;

    let slots = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(2),               // [0] hints header
            Constraint::Length(1),               // [1] gap
            Constraint::Length(1),               // [2] "Terminal Editors" title
            Constraint::Length(terminal_cell_h), // [3] terminal cell
            Constraint::Length(1),               // [4] gap
            Constraint::Length(1),               // [5] "IDEs & GUI Editors" title
            Constraint::Length(ide_cell_h),      // [6] IDE cell
            Constraint::Length(1),               // [7] gap
            Constraint::Length(3),               // [8] footer
            Constraint::Min(0),                  // [9] slack
        ])
        .split(area);

    // ── Helper: floating section title ────────────────────────────────────────
    let section_title = |frame: &mut Frame, slot: Rect, label: &str| {
        let dashes = "─".repeat((slot.width as usize).saturating_sub(label.len() + 2));
        let para = Paragraph::new(Line::from(vec![
            Span::styled(format!(" {label} "), subtitle_style),
            Span::styled(dashes, subtitle_style),
        ]));
        frame.render_widget(para, slot);
    };

    // ── Helper: one editor row ────────────────────────────────────────────────
    let editor_row = |editor: &Editor, idx: usize| -> Line {
        let is_highlighted = idx == app.editor_panel_idx;
        let is_selected = editor == &app.editor;
        let marker = if is_highlighted { "\u{25BA} " } else { "   " };
        let check = if is_selected { "\u{2713} " } else { "  " };
        Line::from(vec![
            Span::styled(
                marker,
                Style::default().fg(if is_highlighted {
                    theme.brand
                } else {
                    theme.dim
                }),
            ),
            Span::styled(
                check,
                if is_selected {
                    on_style
                } else {
                    subtitle_style
                },
            ),
            Span::styled(
                format!("{:<width$}", editor.label(), width = 16),
                if is_highlighted {
                    key_style
                } else {
                    label_style
                },
            ),
        ])
    };

    // ── Hints header ─────────────────────────────────────────────────────────
    let header = Block::default()
        .title(Span::styled(" \u{1F4DD} Editor ", title_style))
        .title_bottom(Line::from(vec![
            Span::styled(" Shift + E ", key_style),
            Span::styled("close", subtitle_style),
        ]))
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(theme.accent));
    frame.render_widget(header, slots[0]);

    // ── Terminal Editors cell ─────────────────────────────────────────────────
    section_title(frame, slots[2], "Terminal Editors");

    let terminal_rows: Vec<Line> = terminal_editors
        .iter()
        .enumerate()
        .map(|(i, ed)| editor_row(ed, i))
        .collect();
    let terminal_cell = Paragraph::new(terminal_rows).block(
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(theme.dim)),
    );
    frame.render_widget(terminal_cell, slots[3]);

    // ── IDEs & GUI Editors cell ───────────────────────────────────────────────
    section_title(frame, slots[5], "IDEs & GUI Editors");

    let ide_rows: Vec<Line> = ide_editors
        .iter()
        .enumerate()
        .map(|(i, ed)| editor_row(ed, first_ide + i))
        .collect();
    let ide_cell = Paragraph::new(ide_rows).block(
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(theme.dim)),
    );
    frame.render_widget(ide_cell, slots[6]);

    // ── Footer — binary of the highlighted editor ─────────────────────────────
    let highlighted_editor = &editors[app.editor_panel_idx];
    let footer_text = if *highlighted_editor == Editor::None {
        "none  —  no editor".to_string()
    } else {
        format!(
            "{}  →  {}",
            highlighted_editor.label(),
            highlighted_editor.binary().unwrap_or_default()
        )
    };
    let footer = Paragraph::new(footer_text)
        .style(Style::default().fg(theme.success))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(theme.accent)),
        );
    frame.render_widget(footer, slots[8]);
}

// ── Options panel ─────────────────────────────────────────────────────────────

/// Render the slide-in options panel occupying `area`.
///
/// Shows all toggleable persistent settings with their current state.
/// Each row shows the toggle key, setting name, and on/off indicator.
pub fn render_options_panel(frame: &mut Frame, area: Rect, app: &App) {
    let theme = app.theme();

    let on_style = Style::default()
        .fg(theme.success)
        .add_modifier(Modifier::BOLD);
    let off_style = Style::default().fg(theme.dim);
    let key_style = Style::default()
        .fg(theme.accent)
        .add_modifier(Modifier::BOLD);
    let label_style = Style::default().fg(theme.fg);
    let subtitle_style = Style::default().fg(theme.dim);
    let title_style = Style::default()
        .fg(theme.brand)
        .add_modifier(Modifier::BOLD);

    // ── Layout ───────────────────────────────────────────────────────────────
    // Slots (top to bottom):
    //   [0]  hints header box         — 2 rows  (top border: title, bottom border: hints)
    //   [1]  gap                      — 1 row
    //   [2]  "Toggles" section title  — 1 row
    //   [3]  Toggles group cell       — 6 rows  (border + 4 rows + border)
    //   [4]  gap                      — 1 row
    //   [5]  "Editor" section title   — 1 row
    //   [6]  Editor group cell        — 3 rows  (border + 1 row + border)
    //   [7]  gap                      — 1 row
    //   [8]  "File Ops" section title — 1 row
    //   [9]  File Ops group cell      — 9 rows  (border + 7 rows + border)
    //   [10] remainder (absorbs slack)
    let slots = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(2), // [0] hints header (border-only, no body)
            Constraint::Length(1), // [1] gap
            Constraint::Length(1), // [2] "Toggles" title
            Constraint::Length(6), // [3] Toggles group (4 option rows)
            Constraint::Length(1), // [4] gap
            Constraint::Length(1), // [5] "Editor" title
            Constraint::Length(3), // [6] Editor group (1 option row)
            Constraint::Length(1), // [7] gap
            Constraint::Length(1), // [8] "File Ops" title
            Constraint::Length(9), // [9] File Ops group (7 option rows)
            Constraint::Min(0),    // [10] slack
        ])
        .split(area);

    // ── Helper: floating section title ────────────────────────────────────────
    // Renders " Label ─────" in dim colour with no border.
    let section_title = |frame: &mut Frame, slot: Rect, label: &str| {
        let dashes = "─".repeat((slot.width as usize).saturating_sub(label.len() + 2));
        let para = Paragraph::new(Line::from(vec![
            Span::styled(format!(" {label} "), subtitle_style),
            Span::styled(dashes, subtitle_style),
        ]));
        frame.render_widget(para, slot);
    };

    // ── Helper: one option row inside a group cell ────────────────────────────
    let option_row = |key: &str, label: &str, value: Span<'static>| -> Line {
        Line::from(vec![
            Span::raw(" "),
            Span::styled(format!("{key:<12}"), key_style),
            Span::styled(format!("{label:<14}"), label_style),
            value,
        ])
    };

    // ── Bool value span helper ────────────────────────────────────────────────
    let bool_span = |enabled: bool| -> Span {
        if enabled {
            Span::styled("● on ", on_style)
        } else {
            Span::styled("○ off", off_style)
        }
    };

    // ── Hints header ─────────────────────────────────────────────────────────
    // Title on the top border line; key hints on the bottom border line.
    // No body row — the block is exactly 2 rows (top + bottom borders).
    let header = Block::default()
        .title(Span::styled(" ⚙ Options ", title_style))
        .title_bottom(Line::from(vec![
            Span::styled(" Shift + O ", key_style),
            Span::styled("close", subtitle_style),
        ]))
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(theme.accent));
    frame.render_widget(header, slots[0]);

    // ── Toggles group ─────────────────────────────────────────────────────────
    section_title(frame, slots[2], "Toggles");

    let toggles_rows = vec![
        option_row("Shift + C", "cd on exit", bool_span(app.cd_on_exit)),
        option_row("w", "single pane", bool_span(app.single_pane)),
        option_row("Shift + T", "theme panel", bool_span(app.show_theme_panel)),
        option_row("z", "show sizes", bool_span(app.active_pane().show_sizes)),
    ];
    let toggles_cell = Paragraph::new(toggles_rows).block(
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(theme.dim)),
    );
    frame.render_widget(toggles_cell, slots[3]);

    // ── Editor group ──────────────────────────────────────────────────────────
    section_title(frame, slots[5], "Editor");

    let editor_label = app.editor.label().to_string();
    let editor_val_style = if app.editor == crate::app::Editor::None {
        off_style
    } else {
        Style::default()
            .fg(theme.success)
            .add_modifier(Modifier::BOLD)
    };

    let editor_rows = vec![option_row(
        "Shift + E",
        "editor",
        Span::styled(editor_label, editor_val_style),
    )];
    let editor_cell = Paragraph::new(editor_rows).block(
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(theme.dim)),
    );
    frame.render_widget(editor_cell, slots[6]);

    // ── File Ops group ────────────────────────────────────────────────────────
    section_title(frame, slots[8], "File Ops");

    let fileops_rows = vec![
        option_row(
            "Space",
            "mark",
            Span::styled("multi-select", Style::default().fg(theme.accent)),
        ),
        option_row(
            "x",
            "cut",
            Span::styled("cut", Style::default().fg(theme.accent)),
        ),
        option_row(
            "p",
            "paste",
            Span::styled("paste", Style::default().fg(theme.accent)),
        ),
        option_row(
            "d",
            "delete",
            Span::styled("delete", Style::default().fg(theme.accent)),
        ),
        option_row(
            "n",
            "new folder",
            Span::styled("mkdir", Style::default().fg(theme.accent)),
        ),
        option_row(
            "N",
            "new file",
            Span::styled("touch", Style::default().fg(theme.accent)),
        ),
        option_row(
            "r",
            "rename",
            Span::styled("rename", Style::default().fg(theme.accent)),
        ),
    ];
    let fileops_cell = Paragraph::new(fileops_rows).block(
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(theme.dim)),
    );
    frame.render_widget(fileops_cell, slots[9]);
}