pub struct FileDialog {
pub selected_file: Option<PathBuf>,
/* private fields */
}
Expand description
The file dialog.
This manages the state of the file dialog. After selecting a file, the absolute path to that file will be stored in the file dialog.
The file dialog is opened with the current working directory by default. To start the file
dialog with a different directory, use FileDialog::set_dir
.
Fields§
§selected_file: Option<PathBuf>
The file that was selected when the file dialog was open the last time.
This will reset when re-opening the file dialog.
Implementations§
Source§impl FileDialog
impl FileDialog
Sourcepub fn new(width: u16, height: u16) -> Result<Self>
pub fn new(width: u16, height: u16) -> Result<Self>
Create a new file dialog.
The width and height are the size of the file dialog in percent of the terminal size. They are clamped to 100%.
Examples found in repository?
26fn main() -> Result<()> {
27 enable_raw_mode()?;
28 let mut stdout = io::stdout();
29 execute!(stdout, EnterAlternateScreen)?;
30 let backend = CrosstermBackend::new(stdout);
31 let mut terminal = Terminal::new(backend)?;
32
33 let res = run_app(&mut terminal, App::new(FileDialog::new(60, 40)?));
34
35 disable_raw_mode()?;
36 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
37 terminal.show_cursor()?;
38
39 if let Err(err) = res {
40 println!("{:?}", err)
41 }
42
43 Ok(())
44}
Sourcepub fn set_dir(&mut self, dir: PathBuf) -> Result<()>
pub fn set_dir(&mut self, dir: PathBuf) -> Result<()>
The directory to open the file dialog in.
Sourcepub fn set_filter(&mut self, filter: FilePattern) -> Result<()>
pub fn set_filter(&mut self, filter: FilePattern) -> Result<()>
Sets the filter to use when browsing files.
Sourcepub fn reset_filter(&mut self) -> Result<()>
pub fn reset_filter(&mut self) -> Result<()>
Removes the filter.
Toggles whether hidden files should be shown.
This only checks whether the file name starts with a dot.
Sourcepub fn open(&mut self)
pub fn open(&mut self)
Opens the file dialog.
Examples found in repository?
46fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
47 loop {
48 terminal.draw(|f| ui(f, &mut app))?;
49
50 // 2. Use the `bind_keys` macro to overwrite key bindings, when the file dialog is open.
51 // The first argument of the macro is the expression that should be used to access the file
52 // dialog.
53 bind_keys!(
54 app.file_dialog,
55 if let Event::Key(key) = event::read()? {
56 match key.code {
57 KeyCode::Char('o') if key.modifiers == KeyModifiers::CONTROL => {
58 app.file_dialog.open()
59 }
60 KeyCode::Char('q') | KeyCode::Esc => {
61 return Ok(());
62 }
63 _ => {}
64 }
65 }
66 )
67 }
68}
Sourcepub fn draw<B: Backend>(&mut self, f: &mut Frame<'_, B>)
pub fn draw<B: Backend>(&mut self, f: &mut Frame<'_, B>)
Draws the file dialog in the TUI application.
Examples found in repository?
70fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
71 let block = Block::default()
72 .title(format!(
73 "Selected file: {}",
74 app.file_dialog
75 .selected_file
76 .as_ref()
77 .map_or("None".to_string(), |f| f.to_string_lossy().to_string())
78 ))
79 .borders(Borders::ALL);
80 f.render_widget(block, f.size());
81
82 // 3. Call the draw function of the file dialog in order to render it.
83 app.file_dialog.draw(f);
84}
Sourcepub fn select(&mut self) -> Result<()>
pub fn select(&mut self) -> Result<()>
Selects an item in the file list.
If the item is a directory, the file dialog will move into that directory. If the item is a
file, the file dialog will close and the path to the file will be stored in
FileDialog::selected_file
.