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
use std::sync::Arc;
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Style},
text::{Line, Span, Text},
widgets::{Block, Borders, HighlightSpacing, List, ListState, WidgetRef},
};
use crate::{File, FileExplorer};
type LineFactory = Arc<dyn Fn(&FileExplorer) -> Line<'_> + Send + Sync>;
pub struct Renderer<'a>(pub(crate) &'a FileExplorer);
impl WidgetRef for Renderer<'_> {
fn render_ref(&self, area: Rect, buf: &mut Buffer)
where
Self: Sized,
{
let mut state = ListState::default().with_selected(Some(self.0.selected_idx()));
let highlight_style = if self.0.current().is_dir {
self.0.theme().highlight_dir_style
} else {
self.0.theme().highlight_item_style
};
let mut list = List::new(self.0.files().iter().map(|file| file.text(self.0.theme())))
.style(self.0.theme().style)
.highlight_spacing(self.0.theme().highlight_spacing.clone())
.highlight_style(highlight_style)
.scroll_padding(self.0.theme().scroll_padding);
if let Some(symbol) = self.0.theme().highlight_symbol.as_deref() {
list = list.highlight_symbol(symbol);
}
if let Some(block) = self.0.theme().block.as_ref() {
let mut block = block.clone();
for title_top in self.0.theme().title_top(self.0) {
block = block.title_top(title_top);
}
for title_bottom in self.0.theme().title_bottom(self.0) {
block = block.title_bottom(title_bottom);
}
list = list.block(block);
}
ratatui::widgets::StatefulWidget::render(&list, area, buf, &mut state);
}
}
impl File {
/// Returns the text with the appropriate style to be displayed for the file.
fn text(&self, theme: &Theme) -> Text<'_> {
let style = if self.is_dir {
*theme.dir_style()
} else {
*theme.item_style()
};
Span::styled(&self.name, style).into()
}
}
/// 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 [`with_block`](Theme::with_block)
/// method and add dinamic titles to it with [`with_title_top`](Theme::with_title_top)
/// and [`with_title_bottom`](Theme::with_title_bottom).
#[derive(Clone, educe::Educe)]
#[educe(Debug, PartialEq, Eq, Hash)]
pub struct Theme {
block: Option<Block<'static>>,
#[educe(Debug(ignore), PartialEq(ignore), Hash(ignore))]
title_top: Vec<LineFactory>,
#[educe(Debug(ignore), PartialEq(ignore), Hash(ignore))]
title_bottom: Vec<LineFactory>,
style: Style,
item_style: Style,
dir_style: Style,
highlight_spacing: HighlightSpacing,
highlight_item_style: Style,
highlight_dir_style: Style,
highlight_symbol: Option<String>,
scroll_padding: usize,
}
impl Theme {
/// Create a new empty theme.
///
/// The theme will not have any style set. To get a theme with the default style, use [`default`](Theme::default).
///
/// # Example
/// ```no_run
/// # use ratatui_explorer::Theme;
/// let theme = Theme::new();
/// ```
#[must_use]
pub const fn new() -> Self {
Self {
block: None,
title_top: Vec::new(),
title_bottom: Vec::new(),
style: Style::new(),
item_style: Style::new(),
dir_style: Style::new(),
highlight_spacing: HighlightSpacing::WhenSelected,
highlight_item_style: Style::new(),
highlight_dir_style: Style::new(),
highlight_symbol: None,
scroll_padding: 0,
}
}
/// 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`:
/// ```plaintext
/// /
/// ├── .git
/// └── Documents
/// ├── passport.png <- selected
/// └── resume.pdf
/// ```
/// You will end up with something like this:
/// ```plaintext
/// ┌/Documents────────────────────────┐
/// │ ../ │
/// │ passport.png │
/// │ resume.pdf │
/// └──────────────────────────────────┘
/// ```
/// With this code:
/// ```no_run
/// use ratatui::widgets::*;
/// use ratatui_explorer::{FileExplorerBuilder, Theme};
///
/// let theme = Theme::default()
/// .with_block(Block::default().borders(Borders::ALL))
/// .add_default_title();
///
/// let file_explorer = FileExplorerBuilder::build_with_theme(theme).unwrap();
///
/// /* user select `password.png` */
///
/// let widget = file_explorer.widget();
/// /* render the widget */
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn add_default_title(self) -> Self {
self.with_title_top(|file_explorer: &FileExplorer| {
Line::from(file_explorer.cwd().display().to_string())
})
}
/// Wrap the file explorer with a custom [`Block`](https://docs.rs/ratatui/latest/ratatui/widgets/block/struct.Block.html) widget.
///
/// Behind the scene, it use the [`List::block`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.block) method.
/// See its documentation for more.
///
/// You can use [`with_title_top`](Theme::with_title_top) and [`with_title_bottom`](Theme::with_title_top) to add dynamic titles to the block.
///
/// # Example
/// ```no_run
/// # use ratatui::widgets::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_block(Block::default().borders(Borders::ALL));
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_block(mut self, block: Block<'static>) -> Self {
self.block = Some(block);
self
}
/// Set the style of the widget.
///
/// Behind the scene, it use the [`List::style`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.style) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_style(Style::default().fg(Color::Yellow));
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_style<S: Into<Style>>(mut self, style: S) -> Self {
self.style = style.into();
self
}
/// Set the style of all non directories items. To set the style of the directories, use [`with_dir_style`](Theme::with_dir_style).
///
/// Behind the scene, it use the [`Span::styled`](https://docs.rs/ratatui/latest/ratatui/text/struct.Span.html#method.styled) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_item_style(Style::default().fg(Color::White));
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_item_style<S: Into<Style>>(mut self, item_style: S) -> Self {
self.item_style = item_style.into();
self
}
/// Set the style of all directories items. To set the style of the non directories, use [`with_item_style`](Theme::with_item_style).
///
/// Behind the scene, it use the [`Span::styled`](https://docs.rs/ratatui/latest/ratatui/text/struct.Span.html#method.styled) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_dir_style(Style::default().fg(Color::Blue));
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_dir_style<S: Into<Style>>(mut self, dir_style: S) -> Self {
self.dir_style = dir_style.into();
self
}
/// Set the style of all highlighted non directories items. To set the style of the highlighted directories, use [`with_highlight_dir_style`](Theme::with_highlight_dir_style).
///
/// Behind the scene, it use the [`List::highlight_style`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.highlight_style) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_highlight_item_style(Style::default().add_modifier(Modifier::BOLD));
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_highlight_item_style<S: Into<Style>>(mut self, highlight_item_style: S) -> Self {
self.highlight_item_style = highlight_item_style.into();
self
}
/// Set the style of all highlighted directories items. To set the style of the highlighted non directories, use [`with_highlight_item_style`](Theme::with_highlight_item_style).
///
/// Behind the scene, it use the [`List::highlight_style`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.highlight_style) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_highlight_dir_style(Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD));
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_highlight_dir_style<S: Into<Style>>(mut self, highlight_dir_style: S) -> Self {
self.highlight_dir_style = highlight_dir_style.into();
self
}
/// Set the symbol used to highlight the selected item.
///
/// Behind the scene, it use the [`List::highlight_symbol`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.highlight_symbol) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_highlight_symbol("> ");
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_highlight_symbol(mut self, highlight_symbol: &str) -> Self {
self.highlight_symbol = Some(highlight_symbol.to_owned());
self
}
/// Set the spacing between the highlighted item and the other items.
///
/// Behind the scene, it use the [`List::highlight_spacing`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.highlight_spacing) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::widgets::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_highlight_spacing(HighlightSpacing::Never);
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_highlight_spacing(mut self, highlight_spacing: HighlightSpacing) -> Self {
self.highlight_spacing = highlight_spacing;
self
}
/// Sets the number of items around the currently selected item that should be kept visible.
///
/// /// Behind the scene, it use the [`List::scroll_padding`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.List.html#method.scroll_padding) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::widgets::*;
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default().with_scroll_padding(1);
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_scroll_padding(mut self, scroll_padding: usize) -> Self {
self.scroll_padding = scroll_padding;
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`](https://docs.rs/ratatui/latest/ratatui/text/struct.Line.html)
/// 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`](https://docs.rs/ratatui/latest/ratatui/widgets/block/struct.Block.html#method.title_top) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::{FileExplorer, Theme};
/// 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()
/// });
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_title_top(
mut self,
title_top: impl Fn(&FileExplorer) -> Line<'_> + 'static + Send + Sync,
) -> Self {
self.title_top.push(Arc::new(title_top));
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`](https://docs.rs/ratatui/latest/ratatui/text/struct.Line.html)
/// 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`](https://docs.rs/ratatui/latest/ratatui/widgets/block/struct.Block.html#method.title_bottom) method.
/// See its documentation for more.
///
/// # Example
/// ```no_run
/// # use ratatui::prelude::*;
/// # use ratatui_explorer::{FileExplorer, Theme};
/// 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()
/// });
/// ```
#[inline]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn with_title_bottom(
mut self,
title_bottom: impl Fn(&FileExplorer) -> Line<'_> + 'static + Send + Sync,
) -> Self {
self.title_bottom.push(Arc::new(title_bottom));
self
}
/// Returns the wrapping block (if it exist) of the file explorer of the theme.
#[inline]
#[must_use]
pub const fn block(&self) -> Option<&Block<'static>> {
self.block.as_ref()
}
/// Returns the style of the widget of the theme.
#[inline]
#[must_use]
pub const fn style(&self) -> &Style {
&self.style
}
/// Returns the style of the non directories items of the theme.
#[inline]
#[must_use]
pub const fn item_style(&self) -> &Style {
&self.item_style
}
/// Returns the style of the directories items of the theme.
#[inline]
#[must_use]
pub const fn dir_style(&self) -> &Style {
&self.dir_style
}
/// Returns the style of the highlighted non directories items of the theme.
#[inline]
#[must_use]
pub const fn highlight_item_style(&self) -> &Style {
&self.highlight_item_style
}
/// Returns the style of the highlighted directories items of the theme.
#[inline]
#[must_use]
pub const fn highlight_dir_style(&self) -> &Style {
&self.highlight_dir_style
}
/// Returns the symbol used to highlight the selected item of the theme.
#[inline]
#[must_use]
pub fn highlight_symbol(&self) -> Option<&str> {
self.highlight_symbol.as_deref()
}
/// Returns the spacing between the highlighted item and the other items of the theme.
#[inline]
#[must_use]
pub const fn highlight_spacing(&self) -> &HighlightSpacing {
&self.highlight_spacing
}
/// Returns the number of items around the currently selected item that should be kept visible.
#[inline]
#[must_use]
pub const fn scroll_padding(&self) -> usize {
self.scroll_padding
}
/// Returns the generated top titles of the theme.
#[inline]
#[must_use]
pub fn title_top<'a>(&self, file_explorer: &'a FileExplorer) -> Vec<Line<'a>> {
self.title_top
.iter()
.map(|title_top| title_top(file_explorer))
.collect()
}
/// Returns the generated bottom titles of the theme.
#[inline]
#[must_use]
pub fn title_bottom<'a>(&self, file_explorer: &'a FileExplorer) -> Vec<Line<'a>> {
self.title_bottom
.iter()
.map(|title_bottom| title_bottom(file_explorer))
.collect()
}
}
impl Default for Theme {
/// Return a slightly customized default theme. To get a theme with no style set, use [`new`](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
/// ```no_run
/// # use ratatui_explorer::Theme;
/// let theme = Theme::default();
/// ```
fn default() -> Self {
Self {
block: Some(Block::default().borders(Borders::ALL)),
title_top: Vec::new(),
title_bottom: Vec::new(),
style: Style::default(),
item_style: Style::default().fg(Color::White),
dir_style: Style::default().fg(Color::LightBlue),
highlight_spacing: HighlightSpacing::Always,
highlight_item_style: Style::default().fg(Color::White).bg(Color::DarkGray),
highlight_dir_style: Style::default().fg(Color::LightBlue).bg(Color::DarkGray),
highlight_symbol: None,
scroll_padding: 0,
}
}
}