| 1 |
use std::io::{self, stdout}; |
| 2 |
|
| 3 |
use crossterm::{ |
| 4 |
ExecutableCommand, |
| 5 |
event::{Event, KeyCode, read}, |
| 6 |
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode}, |
| 7 |
}; |
| 8 |
use ratatui::crossterm; |
| 9 |
use ratatui::{ |
| 10 |
prelude::*, |
| 11 |
widgets::{Block, BorderType, Borders, FrameExt}, |
| 12 |
}; |
| 13 |
|
| 14 |
use ratatui_explorer::{FileExplorerBuilder, Theme}; |
| 15 |
|
| 16 |
fn main() -> io::Result<()> { |
| 17 |
enable_raw_mode()?; |
| 18 |
stdout().execute(EnterAlternateScreen)?; |
| 19 |
|
| 20 |
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?; |
| 21 |
|
| 22 |
|
| 23 |
let mut dark_theme = false; |
| 24 |
let mut file_explorer = FileExplorerBuilder::build_with_theme(get_light_theme())?; |
| 25 |
|
| 26 |
loop { |
| 27 |
|
| 28 |
terminal.draw(|f| { |
| 29 |
f.render_widget_ref(file_explorer.widget(), f.area()); |
| 30 |
})?; |
| 31 |
|
| 32 |
|
| 33 |
let event = read()?; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
if let Event::Key(key) = event |
| 38 |
&& key.modifiers == crossterm::event::KeyModifiers::CONTROL |
| 39 |
{ |
| 40 |
match key.code { |
| 41 |
KeyCode::Char('s') => { |
| 42 |
dark_theme = !dark_theme; |
| 43 |
if dark_theme { |
| 44 |
file_explorer.set_theme(get_dark_theme()); |
| 45 |
} else { |
| 46 |
file_explorer.set_theme(get_light_theme()); |
| 47 |
} |
| 48 |
} |
| 49 |
KeyCode::Char('q') => { |
| 50 |
break; |
| 51 |
} |
| 52 |
_ => {} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
file_explorer.handle(&event)?; |
| 57 |
} |
| 58 |
|
| 59 |
disable_raw_mode()?; |
| 60 |
stdout().execute(LeaveAlternateScreen)?; |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
|
| 64 |
fn get_light_theme() -> Theme { |
| 65 |
Theme::new() |
| 66 |
.with_block( |
| 67 |
Block::default() |
| 68 |
.borders(Borders::ALL) |
| 69 |
.border_type(BorderType::Rounded) |
| 70 |
.style(Style::default().fg(Color::Black).bg(Color::White)), |
| 71 |
) |
| 72 |
.with_item_style(Style::default().fg(Color::Yellow)) |
| 73 |
.with_dir_style( |
| 74 |
Style::default() |
| 75 |
.fg(Color::Cyan) |
| 76 |
.add_modifier(Modifier::BOLD), |
| 77 |
) |
| 78 |
.with_highlight_symbol("> ") |
| 79 |
.add_default_title() |
| 80 |
.with_title_top(|_| Line::from(" ☀ Theme ").right_aligned()) |
| 81 |
.with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into()) |
| 82 |
} |
| 83 |
|
| 84 |
fn get_dark_theme() -> Theme { |
| 85 |
Theme::new() |
| 86 |
.with_block( |
| 87 |
Block::default() |
| 88 |
.borders(Borders::ALL) |
| 89 |
.border_type(BorderType::Rounded) |
| 90 |
.style(Style::default().fg(Color::White).bg(Color::Black)), |
| 91 |
) |
| 92 |
.with_item_style(Style::default().fg(Color::Yellow)) |
| 93 |
.with_dir_style( |
| 94 |
Style::default() |
| 95 |
.fg(Color::Cyan) |
| 96 |
.add_modifier(Modifier::BOLD), |
| 97 |
) |
| 98 |
.with_highlight_symbol("> ") |
| 99 |
.add_default_title() |
| 100 |
.with_title_top(|_| Line::from(" ☾ Theme ").right_aligned()) |
| 101 |
.with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into()) |
| 102 |
} |