pub struct Theme { /* private fields */ }Expand description
The theme of the file explorer.
This struct is used to customize the look of the file explorer. It allows to set the style of the widget and the style of the files. You can also wrap the widget in a block with the Theme::with_block method and add customizable titles to it with Theme::with_title_top and Theme::with_title_bottom.
Implementations§
Source§impl Theme
impl Theme
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new empty theme.
The theme will not have any style set. To get a theme with the default style, use Theme::default.
§Example
let theme = Theme::new();Examples found in repository?
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub fn add_default_title(self) -> Self
pub fn add_default_title(self) -> Self
Add a top title to the theme. The title is the current working directory.
§Example
Suppose you have this tree file, with passport.png selected inside file_explorer:
/
├── .git
└── Documents
├── passport.png <- selected
└── resume.pdfYou will end up with something like this:
┌/Documents────────────────────────┐
│ ../ │
│ passport.png │
│ resume.pdf │
└──────────────────────────────────┘With this code:
use ratatui::widgets::*;
use fpicker::{FileExplorer, Theme};
let theme = Theme::default()
.with_block(Block::default().borders(Borders::ALL))
.add_default_title();
let file_explorer = FileExplorer::with_theme(theme).unwrap();
/* user select `password.png` */
let widget = file_explorer.widget();
/* render the widget */Examples found in repository?
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}More examples
11fn main() -> io::Result<()> {
12 enable_raw_mode()?;
13 stdout().execute(EnterAlternateScreen)?;
14
15 let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
16
17 // Create a new file explorer with the default theme and title.
18 let theme = Theme::default().add_default_title();
19 let mut file_explorer = FileExplorer::with_theme(theme)?;
20
21 let mut selected_paths = vec![];
22
23 loop {
24 // Render the file explorer widget.
25 terminal.draw(|f| {
26 f.render_widget(&file_explorer.widget(), f.area());
27 })?;
28
29 // Read the next event from the terminal.
30 let event = read()?;
31 if let Event::Key(key) = event {
32 if key.code == KeyCode::Char('q') {
33 // Collect selected file paths.
34 selected_paths = file_explorer
35 .selected_files()
36 .iter()
37 .map(|file| file.path().display().to_string())
38 .collect();
39 break;
40 }
41 }
42 // Handle the event in the file explorer.
43 file_explorer.handle(&event)?;
44 }
45
46 // Restore the terminal to normal mode.
47 disable_raw_mode()?;
48 stdout().execute(LeaveAlternateScreen)?;
49
50 // Print the selected file paths to stdout.
51 for path in selected_paths {
52 println!("{}", path);
53 }
54
55 // Return exit code 0 explicitly.
56 process::exit(0);
57}Sourcepub fn with_block(self, block: Block<'static>) -> Self
pub fn with_block(self, block: Block<'static>) -> Self
Wrap the file explorer with a custom Block widget.
Behind the scene, it use the List::block method. See its documentation for more.
You can use Theme::with_title_top and Theme::with_title_bottom to add customizable titles to the block.
§Example
let theme = Theme::default().with_block(Block::default().borders(Borders::ALL));Examples found in repository?
137fn get_theme() -> Theme {
138 Theme::default()
139 .with_block(Block::default().borders(Borders::ALL))
140 .with_dir_style(
141 Style::default()
142 .fg(Color::White)
143 .add_modifier(Modifier::BOLD),
144 )
145 .with_highlight_dir_style(
146 Style::default()
147 .fg(Color::White)
148 .add_modifier(Modifier::BOLD)
149 .bg(Color::DarkGray),
150 )
151}More examples
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub fn with_style<S: Into<Style>>(self, style: S) -> Self
pub fn with_style<S: Into<Style>>(self, style: S) -> Self
Set the style of the widget.
Behind the scene, it use the List::style method. See its documentation for more.
§Example
let theme = Theme::default().with_style(Style::default().fg(Color::Yellow));Sourcepub fn with_item_style<S: Into<Style>>(self, item_style: S) -> Self
pub fn with_item_style<S: Into<Style>>(self, item_style: S) -> Self
Set the style of all non directories items. To set the style of the directories, use Theme::with_dir_style.
Behind the scene, it use the Span::styled method. See its documentation for more.
§Example
let theme = Theme::default().with_item_style(Style::default().fg(Color::White));Examples found in repository?
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub fn with_dir_style<S: Into<Style>>(self, dir_style: S) -> Self
pub fn with_dir_style<S: Into<Style>>(self, dir_style: S) -> Self
Set the style of all directories items. To set the style of the non directories, use Theme::with_item_style.
Behind the scene, it use the Span::styled method. See its documentation for more.
§Example
let theme = Theme::default().with_dir_style(Style::default().fg(Color::Blue));Examples found in repository?
137fn get_theme() -> Theme {
138 Theme::default()
139 .with_block(Block::default().borders(Borders::ALL))
140 .with_dir_style(
141 Style::default()
142 .fg(Color::White)
143 .add_modifier(Modifier::BOLD),
144 )
145 .with_highlight_dir_style(
146 Style::default()
147 .fg(Color::White)
148 .add_modifier(Modifier::BOLD)
149 .bg(Color::DarkGray),
150 )
151}More examples
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub fn with_highlight_item_style<S: Into<Style>>(
self,
highlight_item_style: S,
) -> Self
pub fn with_highlight_item_style<S: Into<Style>>( self, highlight_item_style: S, ) -> Self
Set the style of all highlighted non directories items. To set the style of the highlighted directories, use Theme::with_highlight_dir_style.
Behind the scene, it use the List::highlight_style method. See its documentation for more.
§Example
let theme = Theme::default().with_highlight_item_style(Style::default().add_modifier(Modifier::BOLD));Sourcepub fn with_highlight_dir_style<S: Into<Style>>(
self,
highlight_dir_style: S,
) -> Self
pub fn with_highlight_dir_style<S: Into<Style>>( self, highlight_dir_style: S, ) -> Self
Set the style of all highlighted directories items. To set the style of the highlighted non directories, use Theme::with_highlight_item_style.
Behind the scene, it use the List::highlight_style method. See its documentation for more.
§Example
let theme = Theme::default().with_highlight_dir_style(Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD));Examples found in repository?
137fn get_theme() -> Theme {
138 Theme::default()
139 .with_block(Block::default().borders(Borders::ALL))
140 .with_dir_style(
141 Style::default()
142 .fg(Color::White)
143 .add_modifier(Modifier::BOLD),
144 )
145 .with_highlight_dir_style(
146 Style::default()
147 .fg(Color::White)
148 .add_modifier(Modifier::BOLD)
149 .bg(Color::DarkGray),
150 )
151}Sourcepub fn with_highlight_symbol(self, highlight_symbol: &str) -> Self
pub fn with_highlight_symbol(self, highlight_symbol: &str) -> Self
Set the symbol used to highlight the selected item.
Behind the scene, it use the List::highlight_symbol method. See its documentation for more.
§Example
let theme = Theme::default().with_highlight_symbol("> ");Examples found in repository?
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub fn with_highlight_spacing(self, highlight_spacing: HighlightSpacing) -> Self
pub fn with_highlight_spacing(self, highlight_spacing: HighlightSpacing) -> Self
Set the spacing between the highlighted item and the other items.
Behind the scene, it use the List::highlight_spacing method. See its documentation for more.
§Example
let theme = Theme::default().with_highlight_spacing(HighlightSpacing::Never);Sourcepub fn with_title_top(
self,
title_top: impl Fn(&FileExplorer) -> Line<'static> + 'static,
) -> Self
pub fn with_title_top( self, title_top: impl Fn(&FileExplorer) -> Line<'static> + 'static, ) -> Self
Add a top title factory to the theme.
title_top is a function that take a reference to the current FileExplorer and returns
a Line
to be displayed as a title at the top of the wrapping block (if it exist) of the file explorer. You can call
this function multiple times to add multiple titles.
Behind the scene, it use the Block::title_top method. See its documentation for more.
§Example
use ratatui::prelude::*;
let theme = Theme::default()
.with_title_top(|file_explorer: &FileExplorer| {
Line::from(format!("cwd - {}", file_explorer.cwd().display()))
})
.with_title_top(|file_explorer: &FileExplorer| {
Line::from(format!("{} files", file_explorer.files().len() - 1)).right_aligned()
});Examples found in repository?
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub fn with_title_bottom(
self,
title_bottom: impl Fn(&FileExplorer) -> Line<'static> + 'static,
) -> Self
pub fn with_title_bottom( self, title_bottom: impl Fn(&FileExplorer) -> Line<'static> + 'static, ) -> Self
Add a bottom title factory to the theme.
title_bottom is a function that take a reference to the current FileExplorer and returns
a Line
to be displayed as a title at the bottom of the wrapping block (if it exist) of the file explorer. You can call
this function multiple times to add multiple titles.
Behind the scene, it use the Block::title_bottom method. See its documentation for more.
§Example
let theme = Theme::default()
.with_title_bottom(|file_explorer: &FileExplorer| {
Line::from(format!("cwd - {}", file_explorer.cwd().display()))
})
.with_title_bottom(|file_explorer: &FileExplorer| {
Line::from(format!("{} files", file_explorer.files().len() - 1)).right_aligned()
});Examples found in repository?
62fn get_light_theme() -> Theme {
63 Theme::new()
64 .with_block(
65 Block::default()
66 .borders(Borders::ALL)
67 .border_type(BorderType::Rounded)
68 .style(Style::default().fg(Color::Black).bg(Color::White)),
69 )
70 .with_item_style(Style::default().fg(Color::Yellow))
71 .with_dir_style(
72 Style::default()
73 .fg(Color::Cyan)
74 .add_modifier(Modifier::BOLD),
75 )
76 .with_highlight_symbol("> ")
77 .add_default_title()
78 .with_title_top(|_| Line::from(" ☀ Theme ").right_aligned())
79 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
80}
81
82fn get_dark_theme() -> Theme {
83 Theme::new()
84 .with_block(
85 Block::default()
86 .borders(Borders::ALL)
87 .border_type(BorderType::Rounded)
88 .style(Style::default().fg(Color::White).bg(Color::Black)),
89 )
90 .with_item_style(Style::default().fg(Color::Yellow))
91 .with_dir_style(
92 Style::default()
93 .fg(Color::Cyan)
94 .add_modifier(Modifier::BOLD),
95 )
96 .with_highlight_symbol("> ")
97 .add_default_title()
98 .with_title_top(|_| Line::from(" ☾ Theme ").right_aligned())
99 .with_title_bottom(|_| " ^q Quit | ^s Switch theme ".into())
100}Sourcepub const fn block(&self) -> Option<&Block<'static>>
pub const fn block(&self) -> Option<&Block<'static>>
Returns the wrapping block (if it exist) of the file explorer of the theme.
Sourcepub const fn item_style(&self) -> &Style
pub const fn item_style(&self) -> &Style
Returns the style of the non directories items of the theme.
Sourcepub const fn dir_style(&self) -> &Style
pub const fn dir_style(&self) -> &Style
Returns the style of the directories items of the theme.
Sourcepub const fn highlight_item_style(&self) -> &Style
pub const fn highlight_item_style(&self) -> &Style
Returns the style of the highlighted non directories items of the theme.
Sourcepub const fn highlight_dir_style(&self) -> &Style
pub const fn highlight_dir_style(&self) -> &Style
Returns the style of the highlighted directories items of the theme.
Sourcepub fn highlight_symbol(&self) -> Option<&str>
pub fn highlight_symbol(&self) -> Option<&str>
Returns the symbol used to highlight the selected item of the theme.
Sourcepub const fn highlight_spacing(&self) -> &HighlightSpacing
pub const fn highlight_spacing(&self) -> &HighlightSpacing
Returns the spacing between the highlighted item and the other items of the theme.
Sourcepub fn title_top(&self, file_explorer: &FileExplorer) -> Vec<Line<'_>>
pub fn title_top(&self, file_explorer: &FileExplorer) -> Vec<Line<'_>>
Returns the generated top titles of the theme.
Sourcepub fn title_bottom(&self, file_explorer: &FileExplorer) -> Vec<Line<'_>>
pub fn title_bottom(&self, file_explorer: &FileExplorer) -> Vec<Line<'_>>
Returns the generated bottom titles of the theme.
Trait Implementations§
Source§impl Default for Theme
impl Default for Theme
Source§fn default() -> Self
fn default() -> Self
Return a slightly customized default theme. To get a theme with no style set, use Theme::new.
The theme will have a block with all borders, a white style for the items, a light blue style for the directories, a dark gray background for all the highlighted items.
§Example
let theme = Theme::default();impl Eq for Theme
Auto Trait Implementations§
impl Freeze for Theme
impl !RefUnwindSafe for Theme
impl !Send for Theme
impl !Sync for Theme
impl Unpin for Theme
impl !UnwindSafe for Theme
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more