Struct FileDialog

Source
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

Source

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?
examples/demo.rs (line 33)
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}
Source

pub fn set_dir(&mut self, dir: PathBuf) -> Result<()>

The directory to open the file dialog in.

Source

pub fn set_filter(&mut self, filter: FilePattern) -> Result<()>

Sets the filter to use when browsing files.

Source

pub fn reset_filter(&mut self) -> Result<()>

Removes the filter.

Source

pub fn toggle_show_hidden(&mut self) -> Result<()>

Toggles whether hidden files should be shown.

This only checks whether the file name starts with a dot.

Source

pub fn open(&mut self)

Opens the file dialog.

Examples found in repository?
examples/demo.rs (line 58)
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}
Source

pub fn close(&mut self)

Closes the file dialog.

Source

pub fn is_open(&self) -> bool

Returns whether the file dialog is currently open.

Source

pub fn draw<B: Backend>(&mut self, f: &mut Frame<'_, B>)

Draws the file dialog in the TUI application.

Examples found in repository?
examples/demo.rs (line 83)
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}
Source

pub fn next(&mut self)

Goes to the next item in the file list.

Source

pub fn previous(&mut self)

Goes to the previous item in the file list.

Source

pub fn up(&mut self) -> Result<()>

Moves one directory up.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.