thag_rs 0.1.6

A versatile cross-platform script runner and REPL for Rust snippets, expressions and programs. Accepts a script file or dynamic options.
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/// Original is `https://github.com/flip1995/tui-rs-file-dialog/blob/master/src/lib.rs`
/// Copyright (c) 2023 Philipp Krones
/// Licence: MIT
use crossterm::{
    cursor::{Hide, Show},
    event::{KeyCode, KeyEvent, KeyEventKind},
    execute,
};
use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style, Stylize},
    text::Line,
    widgets::{Block, Borders, List, ListItem, ListState},
    Frame,
};
use std::{
    cmp,
    ffi::OsString,
    fs,
    io::Result,
    iter,
    path::{Path, PathBuf},
    sync::OnceLock,
};
use tui_textarea::{Input, TextArea};

use crate::{debug_log, key, key_mappings, Lvl};
use crate::{shared::KeyDisplayLine, tui_editor::display_popup};
use crate::{
    tui_editor::{self, centered_rect},
    KeyCombination,
};

/// File dialog mode to distinguish between Open and Save dialogs
#[derive(Debug, PartialEq, Eq)]
pub enum DialogMode {
    Open,
    Save,
}

/// A pattern that can be used to filter the displayed files.
pub enum FilePattern {
    /// Filter by file extension. This filter is case insensitive.
    Extension(String),
    /// Filter by substring. This filter is case sensitive.
    Substring(String),
}

/// Enum to represent which part of the dialog has focus.
#[derive(Debug, PartialEq, Eq)]
pub enum DialogFocus {
    List,  // Focus on file list
    Input, // Focus on input area
}

pub enum Status {
    Complete,
    Incomplete,
    Quit,
}

/// The file dialog.
///
/// This manages the state of the file dialog. After selecting or specifying a file, the absolute
/// path to that file will be stored in the file dialog.
///
/// The file dialog is opened with the current working directory by default.
// pub struct FileDialog<'a, FilePattern> {
pub struct FileDialog<'a> {
    /// The file that was selected or specified when the file dialog was open the last time.
    pub selected_file: Option<PathBuf>,

    width: u16,
    height: u16,

    filter: Option<FilePattern>,
    open: bool,
    current_dir: PathBuf,
    show_hidden: bool,

    list_state: ListState,
    items: Vec<String>,

    /// Current mode of the dialog (Open or Save)
    mode: DialogMode,

    /// Current focus of the Save dialog (List or Input)
    focus: DialogFocus,

    pub popup: bool,
    title_bottom: &'a str,

    /// Input for the file name in Input mode
    pub input: TextArea<'a>,

    pub buf: String,
}

// impl<FilePattern> FileDialog<'_, FilePattern> {
impl<'a> FileDialog<'a> {
    /// Create a new file dialog.
    ///
    /// The width and height are the size of the file dialog in percent of the terminal size. They
    /// are clamped to 100%.
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `update_entries` method.
    pub fn new(width: u16, height: u16, mode: DialogMode) -> Result<Self> {
        let mut s = Self {
            width: cmp::min(width, 100),
            height: cmp::min(height, 100),
            selected_file: None,
            filter: None,
            open: false,
            current_dir: PathBuf::from(".").canonicalize()?,
            show_hidden: false,
            list_state: ListState::default(),
            items: vec![],
            mode,
            focus: DialogFocus::List,
            popup: false, // Start with focus on the file list
            input: TextArea::default(),
            title_bottom: "Ctrl+l to show keys",
            buf: String::new(),
        };
        s.update_entries()?;
        Ok(s)
    }

    /// Sets the directory to open the file dialog in.
    ///
    /// # Errors
    ///
    /// This function will return an error if there is a problem canonicalizing the directory.
    pub fn set_dir(&mut self, dir: &Path) -> Result<()> {
        self.current_dir = dir.canonicalize()?;
        self.update_entries()
    }

    /// Sets the filter to use when browsing files.
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `update_entries` method.
    pub fn set_filter(&mut self, filter: FilePattern) -> Result<()> {
        self.filter = Some(filter);
        self.update_entries()
    }

    /// Removes the filter.
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `update_entries` method.
    pub fn reset_filter(&mut self) -> Result<()> {
        self.filter.take();
        self.update_entries()
    }

    /// Toggles whether hidden files should be shown.
    ///
    /// This only checks whether the file name starts with a dot.
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `update_entries` method.
    pub fn toggle_show_hidden(&mut self) -> Result<()> {
        self.show_hidden = !self.show_hidden;
        self.update_entries()
    }

    /// Opens the file dialog.
    pub fn open(&mut self) {
        self.selected_file.take();
        self.open = true;
    }

    /// Closes the file dialog.
    pub fn close(&mut self) {
        self.open = false;
    }

    /// Returns whether the file dialog is currently open.
    #[must_use]
    pub const fn is_open(&self) -> bool {
        self.open
    }

    /// Draws the file dialog in the TUI application.
    pub fn draw(&mut self, f: &mut Frame) {
        if self.open {
            let area = centered_rect(self.width, self.height, f.area());

            // Split the area into two parts: one for the file list and one for the input field.
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints(
                    [
                        Constraint::Length(3),            // Input field area
                        Constraint::Min(self.height - 3), // File list area
                    ]
                    .as_ref(),
                )
                .split(area);

            // Determine if the file list has focus
            let file_list_focus = matches!(self.focus, DialogFocus::List);

            let title = format!("{}", self.current_dir.display());
            let block = Block::default()
                .title(title.clone())
                .borders(Borders::ALL)
                .border_style(if file_list_focus {
                    Style::default()
                        .fg(Color::Indexed(u8::from(&Lvl::HEAD)))
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(Color::DarkGray).dim()
                })
                .title_bottom(Line::from(self.title_bottom).centered());
            let list_items: Vec<ListItem> = self
                .items
                .iter()
                .map(|s| ListItem::new(s.as_str()))
                .collect();
            let list = List::new(list_items)
                .block(block)
                .highlight_style(
                    Style::default()
                        .fg(Color::Indexed(u8::from(&Lvl::EMPH)))
                        .bold(),
                )
                .style(if file_list_focus {
                    Style::default()
                        .fg(Color::Indexed(u8::from(&Lvl::SUBH)))
                        .not_bold()
                } else {
                    Style::default().fg(Color::DarkGray).dim()
                });
            f.render_stateful_widget(list, chunks[1], &mut self.list_state);

            // Render the input box for the filename
            let input_focus = matches!(self.focus, DialogFocus::Input);
            if self.mode == DialogMode::Save {
                // Create a Block for the input area with borders and background
                let input_style = if input_focus {
                    Style::default()
                        .fg(Color::Indexed(u8::from(&Lvl::HEAD)))
                        .bold()
                } else {
                    Style::default().fg(Color::DarkGray).dim()
                };
                let input_block = Block::default()
                    .title(title)
                    .borders(Borders::ALL)
                    .style(input_style)
                    .border_style(input_style);

                let input_area = input_block.inner(chunks[0]); // Adjusts area to fit within borders
                f.render_widget(input_block, chunks[0]);

                // Conditionally show the cursor only if the input box has focus
                self.input
                    .set_cursor_line_style(Style::default().fg(Color::DarkGray));
                if input_focus {
                    self.input.set_style(Style::default());
                    self.input
                        .set_selection_style(Style::default().bg(Color::Blue));
                    self.input.set_cursor_style(Style::default()); //.on_magenta());
                    self.input.set_cursor_line_style(
                        Style::default()
                            .fg(Color::Indexed(u8::from(&Lvl::EMPH)))
                            .bold(),
                    );
                } else {
                    self.input.set_style(Style::default().dim());
                    self.input.set_selection_style(Style::default().hidden());
                    self.input.set_cursor_style(Style::default().hidden());
                    self.input.set_cursor_line_style(Style::default().hidden());
                }
                f.render_widget(&self.input, input_area); // Renders the input widget inside the block
            }

            if self.popup {
                let mappings: &[KeyDisplayLine] = match self.focus {
                    DialogFocus::List => LIST_MAPPINGS,
                    DialogFocus::Input => INPUT_MAPPINGS,
                };
                let (max_key_len, max_desc_len) = get_max_lengths(mappings);
                let title_bottom = tui_editor::TITLE_BOTTOM;
                display_popup(
                    mappings,
                    "Key bindings",
                    title_bottom,
                    max_key_len,
                    max_desc_len,
                    f,
                );
            };
        }
    }

    /// Goes to the next item in the file list.
    pub fn next(&mut self) {
        let i = match self.list_state.selected() {
            Some(i) => cmp::min(self.items.len() - 1, i + 1),
            None => cmp::min(self.items.len().saturating_sub(1), 1),
        };
        self.list_state.select(Some(i));
    }
    /// Goes to the previous item in the file list.
    pub fn previous(&mut self) {
        let i = self
            .list_state
            .selected()
            .map_or(0, |i| i.saturating_sub(1));
        self.list_state.select(Some(i));
    }

    /// Goes to the parent directory.
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `update_entries` method.
    pub fn up(&mut self) -> Result<()> {
        self.current_dir.pop();
        self.update_entries()
    }

    /// Selects an item or sets a file name (for Save mode).
    ///
    /// If the item is a directory, the file dialog will move into that directory. If the item is a
    /// file, the file dialog will close and the path to the file will be stored in
    /// [`FileDialog::selected_file`].
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `update_entries` method.
    pub fn select(&mut self) -> Result<()> {
        // Open mode logic (already correct)
        debug_log!("In select()");
        let Some(selected) = self.list_state.selected() else {
            self.next();
            debug_log!("Returning Ok(())");
            return Ok(());
        };

        let path = self.current_dir.join(&self.items[selected]);
        debug_log!(
            "current_dir={:?}; path={path:?}; is_file? {}; mode={:?}",
            self.current_dir,
            path.is_file(),
            self.mode
        );

        if path.is_dir() {
            self.current_dir.clone_from(&path);
            self.update_entries()?;
        }
        // if matches!(self.mode, DialogMode::Save) {
        if self.focus == DialogFocus::Input {
            // Save mode logic to use the entered filename
            let file_name = self.input.lines().join(""); // Get the input from TextArea
            debug_log!("file_name={file_name}");
            if !file_name.is_empty() {
                let path = self.current_dir.join(file_name);
                self.selected_file = Some(path); // Set the selected file
                self.close(); // Close the dialog
            }
        } else if path.is_file() {
            self.selected_file = Some(path);
            self.close();
            // return Ok(());
        }
        debug_log!("self.selected_file={:?}", self.selected_file);
        Ok(())
    }

    /// Updates the entries in the file list. This function is called automatically when necessary.
    ///
    /// # Panics
    ///
    /// Panics if there is a logic error comparing two strings.
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered.
    fn update_entries(&mut self) -> Result<()> {
        self.items = iter::once("..".to_string())
            .chain(
                fs::read_dir(&self.current_dir)?
                    .flatten()
                    .filter(|e| -> bool {
                        let e = e.path();
                        if e.file_name()
                            .map_or(false, |n| n.to_string_lossy().starts_with('.'))
                        {
                            return self.show_hidden;
                        }
                        if self.filter.is_none()
                            || matches!(self.filter, Some(FilePattern::Extension(_)))
                        {
                            return true;
                        }
                        match self.filter.as_ref().unwrap() {
                            FilePattern::Extension(ext) => e.extension().map_or(false, |e| {
                                e.to_ascii_lowercase() == OsString::from(ext.to_ascii_lowercase())
                            }),
                            FilePattern::Substring(substr) => e
                                .file_name()
                                .map_or(false, |n| n.to_string_lossy().contains(substr)),
                        }
                    })
                    .map(|file| {
                        let file_name = file.file_name();
                        if matches!(file.file_type(), Ok(t) if t.is_dir()) {
                            format!("{}/", file_name.to_string_lossy())
                        } else {
                            file_name.to_string_lossy().to_string()
                        }
                    }),
            )
            .collect();
        self.items.sort_by(|a, b| {
            if a == ".." {
                return cmp::Ordering::Less;
            }
            if b == ".." {
                return cmp::Ordering::Greater;
            }
            match (a.chars().last().unwrap(), b.chars().last().unwrap()) {
                ('/', '/') => a.cmp(b),
                ('/', _) => cmp::Ordering::Less,
                (_, '/') => cmp::Ordering::Greater,
                _ => a.cmp(b),
            }
        });
        self.list_state.select(None);
        self.next();
        Ok(())
    }

    /// Handle keyboard input while the file dialog is open.
    ///
    /// # Errors
    ///
    /// This function will bubble up any i/o errors encountered by the `select` method.
    /// # Panics
    ///
    /// Panics if there is a logic error popping the last character out of the search buffer.
    #[allow(clippy::unnested_or_patterns)]
    pub fn handle_input(&mut self, key_event: KeyEvent) -> Result<Status> {
        // Make sure for Windows
        if matches!(key_event.kind, KeyEventKind::Press) {
            debug_log!("key_event={key_event:#?}");
            debug_log!("self.focus={:#?}", self.focus);
            let key_code = key_event.code;
            let key_combination = KeyCombination::from(key_event);
            match key_combination {
                key!(ctrl - l) => self.popup = !self.popup,
                key!(ctrl - q) | key!(Esc) => return Ok(Status::Quit),
                key!(Enter) => {
                    self.select()?;
                    self.buf.clear();
                    self.reset_filter()?;
                }
                _ => match self.focus {
                    DialogFocus::List => {
                        // Handle keys for navigating the file list
                        match key_combination {
                            key!(tab) | key!(backtab) => {
                                debug_log!("Matched Tab / Backtab in {:#?} mode", self.focus);
                                self.focus = DialogFocus::Input;
                                let _ = execute!(std::io::stdout().lock(), Show,);
                            } // Switch to input area
                            key!(down) | key!(ctrl - j) => self.next(),
                            key!(up) | key!(ctrl - k) => self.previous(),
                            key!(ctrl - u) => {
                                self.buf.clear();
                                self.reset_filter()?;
                                self.up()?;
                            }
                            key!(ctrl - h) => self.toggle_show_hidden()?,
                            key!(backspace) => {
                                if !self.buf.is_empty() {
                                    self.buf.pop().expect("Logic error updating search buffer");
                                }
                                self.set_filter(FilePattern::Substring(self.buf.clone()))?;
                            }
                            _ => {
                                if let KeyCode::Char(c) = key_code {
                                    self.buf.push(c);
                                    debug_log!("self.buf={}", self.buf);
                                    self.set_filter(FilePattern::Substring(self.buf.clone()))?;
                                }
                            }
                        }
                    }
                    DialogFocus::Input => {
                        match key_combination {
                            key!(tab) | key!(backtab) => {
                                debug_log!("Matched Tab / Backtab in {:#?} mode", self.focus);
                                self.focus = DialogFocus::List;
                                let _ = execute!(std::io::stdout().lock(), Hide,);
                            } // Switch back to list
                            _ => {
                                // Handle keys for the input area (filename)
                                handle_save_input(&mut self.input, key_event);
                            }
                        }
                    }
                },
            }
        }
        Ok(Status::Incomplete)
    }
}

fn get_max_lengths(mappings: &[KeyDisplayLine]) -> (u16, u16) {
    static MAX_LENGTHS: OnceLock<(u16, u16)> = OnceLock::new();
    let (max_key_len, max_desc_len) = *MAX_LENGTHS.get_or_init(|| {
        mappings
            .iter()
            .fold((0_u16, 0_u16), |(max_key, max_desc), row| {
                let key_len = row.keys.len().try_into().unwrap();
                let desc_len = row.desc.len().try_into().unwrap();
                (max_key.max(key_len), max_desc.max(desc_len))
            })
    });
    (max_key_len, max_desc_len)
}

/// Handle input in Save mode (for typing file name).
fn handle_save_input(text_area: &mut TextArea, key: KeyEvent) {
    // Convert the KeyEvent into an Input that TextArea can handle
    let input = Input::from(key);
    text_area.input(input);
}

const LIST_MAPPINGS: &[KeyDisplayLine] = key_mappings![
    (10, "Key bindings", "Description"),
    (20, "Esc, Ctrl+q", "Close the file dialog"),
    (30, "↓, Ctrl+j", "Move down in file list view"),
    (40, "↑, Ctrl+k", "Move up in file list view"),
    (50, "Enter", "Choose the current selection"),
    (60, "Ctrl+u", "Go up to parent directory"),
    (70, "Ctrl+h", "Toggle showing hidden files"),
    (
        80,
        "Tab, BackTab",
        "Toggle between directory list and file name input"
    ),
    (90, "a-z, A-Z", "Append character to match string"),
    (100, "Backspace", "Remove last character from match string"),
    (110, "Ctrl+l", "Toggle keys display (this screen)"),
];

const INPUT_MAPPINGS: &[KeyDisplayLine] = key_mappings![
    (10, "Key bindings", "Description"),
    (20, "Esc, Ctrl+q", "Close the file dialog"),
    (30, "Enter", "Save file"),
    (
        40,
        "Tab, BackTab",
        "Toggle between directory list and file name input"
    ),
    (50, "Ctrl+l", "Toggle keys display (this screen)"),
];