| 1 | # ratatui-explorer |
| 2 | |
| 3 | [ratatui-explorer](https://crates.io/crates/ratatui-explorer) is a simple library for creating file explorers for [ratatui](https://github.com/ratatui-org/ratatui). |
| 4 | |
| 5 | Features: |
| 6 | |
| 7 | - File explorer functionality. |
| 8 | - Input handling (from [crossterm](https://docs.rs/crossterm/latest/crossterm/), [termion](https://docs.rs/termion/latest/termion/), [termwiz](https://docs.rs/termwiz/latest/termwiz/) and your own backend). |
| 9 | - Customizable widget theming. |
| 10 | |
| 11 | # Examples |
| 12 | |
| 13 | Run `cargo run --example` to try the different examples available. |
| 14 | |
| 15 | ## [Basic usage](examples/basic.rs) |
| 16 | |
| 17 | The simplest use of [ratatui-explorer](https://crates.io/crates/ratatui-explorer) with the [crossterm](https://docs.rs/crossterm/latest/crossterm/) backend. |
| 18 | |
| 19 | ```shell |
| 20 | cargo run --example basic |
| 21 | ``` |
| 22 | |
| 23 |  |
| 24 | |
| 25 | --- |
| 26 | |
| 27 | ## [Mutate and filter](examples/filter_by_size.rs) |
| 28 | |
| 29 | Filter showed files and customize how they are displayed. |
| 30 | |
| 31 | ```shell |
| 32 | cargo run --example file_preview |
| 33 | ``` |
| 34 | |
| 35 |  |
| 36 | |
| 37 | --- |
| 38 | |
| 39 | ## [File preview](examples/file_preview.rs) |
| 40 | |
| 41 | Adapt the interface depending on the selected file. |
| 42 | |
| 43 | ```shell |
| 44 | cargo run --example file_preview |
| 45 | ``` |
| 46 | |
| 47 |  |
| 48 | |
| 49 | --- |
| 50 | |
| 51 | ## [Light and dark theme](examples/light_and_dark_theme.rs) |
| 52 | |
| 53 | Switching custom themes while running. |
| 54 | |
| 55 | ```shell |
| 56 | cargo run --example light_and_dark_theme |
| 57 | ``` |
| 58 | |
| 59 |  |
| 60 | |
| 61 | # Basic usage |
| 62 | |
| 63 | Install the libraries in your `Cargo.toml` file: |
| 64 | |
| 65 | ```plaintext |
| 66 | cargo add ratatui ratatui-explorer crossterm |
| 67 | ``` |
| 68 | |
| 69 | Then inside your `main.rs` file: |
| 70 | |
| 71 | ```rust no_run |
| 72 | use std::io::{self, stdout}; |
| 73 | |
| 74 | use ratatui::crossterm::{ |
| 75 | event::{read, Event, KeyCode}, |
| 76 | terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, |
| 77 | ExecutableCommand, |
| 78 | }; |
| 79 | use ratatui::prelude::*; |
| 80 | use ratatui::widgets::FrameExt as _; |
| 81 | |
| 82 | use ratatui_explorer::{FileExplorerBuilder, Theme}; |
| 83 | |
| 84 | fn main() -> io::Result<()> { |
| 85 | enable_raw_mode()?; |
| 86 | stdout().execute(EnterAlternateScreen)?; |
| 87 | |
| 88 | let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?; |
| 89 | |
| 90 | // Create a new file explorer with the default theme and title. |
| 91 | let theme = Theme::default().add_default_title(); |
| 92 | let mut file_explorer = FileExplorerBuilder::build_with_theme(theme)?; |
| 93 | |
| 94 | loop { |
| 95 | // Render the file explorer widget. |
| 96 | terminal.draw(|f| { |
| 97 | f.render_widget_ref(file_explorer.widget(), f.area()); |
| 98 | })?; |
| 99 | |
| 100 | // Read the next event from the terminal. |
| 101 | let event = read()?; |
| 102 | if let Event::Key(key) = event { |
| 103 | if key.code == KeyCode::Char('q') { |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | // Handle the event in the file explorer. |
| 108 | file_explorer.handle(&event)?; |
| 109 | } |
| 110 | |
| 111 | disable_raw_mode()?; |
| 112 | stdout().execute(LeaveAlternateScreen)?; |
| 113 | Ok(()) |
| 114 | } |
| 115 | ``` |
| 116 | |
| 117 | ## Customizing the theme |
| 118 | |
| 119 | You can customize the theme of the file explorer widget by using the `Theme` struct. |
| 120 | |
| 121 | ```rust |
| 122 | use ratatui::{prelude::*, widgets::*}; |
| 123 | use ratatui_explorer::Theme; |
| 124 | |
| 125 | let theme = Theme::default() |
| 126 | .add_default_title() |
| 127 | .with_title_bottom(|fe| format!("[{} files]", fe.files().len()).into()) |
| 128 | .with_block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)) |
| 129 | .with_highlight_item_style(Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)) |
| 130 | .with_highlight_dir_style(Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)) |
| 131 | .with_highlight_symbol("> ".into()); |
| 132 | ``` |
| 133 | |
| 134 | # Bindings |
| 135 | |
| 136 | The following bindings are used by default for [crossterm](https://docs.rs/crossterm/latest/crossterm/), |
| 137 | [termion](https://docs.rs/termion/latest/termion/) and [termwiz](https://docs.rs/termwiz/latest/termwiz/). |
| 138 | |
| 139 | | Binding | Action | |
| 140 | | --------------------------------- | ------------------------------------------ | |
| 141 | | `j`, `<DownArrow>` | Move the selection down | |
| 142 | | `k`, `<UpArrow>` | Move the selection up | |
| 143 | | `h`, `<LeftArrow>`, `<Backspace>` | Go to the parent directory | |
| 144 | | `l`, `<RightArrow>`, `<Enter>` | Go to the child directory\* | |
| 145 | | `Home` | Select the first entry | |
| 146 | | `End` | Select the last entry | |
| 147 | | `PageUp` | Scroll the selection up | |
| 148 | | `PageDown` | Scroll the selection down | |
| 149 | | `<Ctrl> + h ` | Toggle between showing hidden files or not | |
| 150 | |
| 151 | _\*if the selected item is a directory_ |