basic/
basic.rs

1use std::io::{self, stdout};
2use std::process;
3use crossterm::{
4    event::{read, Event, KeyCode},
5    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
6    ExecutableCommand,
7};
8use ratatui::prelude::*;
9use fpicker::{FileExplorer, Theme};
10
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}