From 2f3e2b5474834e3d733edf09b831c5607d451f49 Mon Sep 17 00:00:00 2001 From: Reyk Floeter Date: Tue, 10 Dec 2019 15:24:36 +0000 Subject: Add FileSelection dialog type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds the FileSelection struct representing a file selection dialog. It can be displayed using the backend’s show_file_selection function. Currently, we only support file open dialogs (i. e. choosing an existing file). Support for save dialogs should be added in the future. --- src/backends/dialog.rs | 9 ++++++++- src/backends/mod.rs | 3 +++ src/backends/stdio.rs | 15 ++++++++++++++- src/backends/zenity.rs | 9 ++++++++- 4 files changed, 33 insertions(+), 3 deletions(-) (limited to 'src/backends') diff --git a/src/backends/dialog.rs b/src/backends/dialog.rs index 8061d98..668cf57 100644 --- a/src/backends/dialog.rs +++ b/src/backends/dialog.rs @@ -3,7 +3,7 @@ use std::process; -use crate::{Choice, Error, Input, Message, Password, Question, Result}; +use crate::{Choice, Error, FileSelection, Input, Message, Password, Question, Result}; /// The `dialog` backend. /// @@ -160,4 +160,11 @@ impl super::Backend for Dialog { self.execute(args, vec![], &question.title) .and_then(|output| get_choice(output.status)) } + + fn show_file_selection(&self, file_selection: &FileSelection) -> Result> { + let dir = file_selection.path_to_string().ok_or("path not valid")?; + let args = vec!["--fselect", &dir]; + self.execute(args, vec![], &file_selection.title) + .and_then(get_stderr) + } } diff --git a/src/backends/mod.rs b/src/backends/mod.rs index 1331323..09013b1 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -37,6 +37,9 @@ pub trait Backend { /// Shows the given question dialog and returns the choice. fn show_question(&self, question: &super::Question) -> Result; + + /// Shows the given file selection dialog and returns the file name. + fn show_file_selection(&self, file_selection: &super::FileSelection) -> Result>; } pub(crate) fn is_available(name: &str) -> bool { diff --git a/src/backends/stdio.rs b/src/backends/stdio.rs index 9a153df..627714a 100644 --- a/src/backends/stdio.rs +++ b/src/backends/stdio.rs @@ -3,7 +3,7 @@ use std::io::{self, Write}; -use crate::{Choice, Input, Message, Password, Question, Result}; +use crate::{Choice, FileSelection, Input, Message, Password, Question, Result}; /// The fallback backend using standard input and output. /// @@ -86,4 +86,17 @@ impl super::Backend for Stdio { io::stdout().flush()?; Ok(parse_choice(&read_input()?)) } + + fn show_file_selection(&self, file_selection: &FileSelection) -> Result> { + let dir = file_selection.path_to_string().ok_or("path not valid")?; + print_title(&file_selection.title); + print!("{} [{}]: ", file_selection.text, dir); + io::stdout().flush()?; + let result = read_input()?; + if result.starts_with('/') { + Ok(Some(result)) + } else { + Ok(Some(dir + &result)) + } + } } diff --git a/src/backends/zenity.rs b/src/backends/zenity.rs index 0deca0a..4ee7785 100644 --- a/src/backends/zenity.rs +++ b/src/backends/zenity.rs @@ -3,7 +3,7 @@ use std::process; -use crate::{Choice, Error, Input, Message, Password, Question, Result}; +use crate::{Choice, Error, FileSelection, Input, Message, Password, Question, Result}; /// The `zenity` backend. /// @@ -163,4 +163,11 @@ impl super::Backend for Zenity { self.execute(args, &question.title) .and_then(|output| get_choice(output.status)) } + + fn show_file_selection(&self, file_selection: &FileSelection) -> Result> { + let dir = file_selection.path_to_string().ok_or("path not valid")?; + let args = vec!["--file-selection", "--filename", &dir]; + self.execute(args, &file_selection.title) + .and_then(get_stdout) + } } -- cgit v1.2.1