-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
779e8cd
commit 8230ed8
Showing
11 changed files
with
296 additions
and
81 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use libium::HOME; | ||
use std::{ | ||
io::Result, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
#[cfg(feature = "gui")] | ||
/// Uses the system file picker to pick a file, with a `default` path | ||
fn show_folder_picker(default: impl AsRef<Path>, prompt: impl Into<String>) -> Option<PathBuf> { | ||
rfd::FileDialog::new() | ||
.set_can_create_directories(true) | ||
.set_directory(default) | ||
.set_title(prompt) | ||
.pick_folder() | ||
} | ||
|
||
#[cfg(not(feature = "gui"))] | ||
/// Uses a terminal input to pick a file, with a `default` path | ||
fn show_folder_picker(default: impl AsRef<Path>, prompt: impl Into<String>) -> Option<PathBuf> { | ||
inquire::Text::new(&prompt.into()) | ||
.with_default(&default.as_ref().display().to_string()) | ||
.prompt() | ||
.ok() | ||
.map(Into::into) | ||
} | ||
|
||
/// Picks a folder using the terminal or system file picker (depending on the feature flag `gui`) | ||
/// | ||
/// The `default` path is shown/opened at first and the `name` is what folder the user is supposed to be picking (e.g. output directory) | ||
pub fn pick_folder( | ||
default: impl AsRef<Path>, | ||
prompt: impl Into<String>, | ||
name: impl AsRef<str>, | ||
) -> Result<Option<PathBuf>> { | ||
show_folder_picker(default, prompt) | ||
.map(|raw_in| { | ||
let path = raw_in | ||
.components() | ||
.map(|c| { | ||
if c.as_os_str() == "~" { | ||
HOME.as_os_str() | ||
} else { | ||
c.as_os_str() | ||
} | ||
}) | ||
.collect::<PathBuf>() | ||
.canonicalize()?; | ||
|
||
println!( | ||
"✔ \x1b[01m{}\x1b[0m · \x1b[32m{}\x1b[0m", | ||
name.as_ref(), | ||
path.display(), | ||
); | ||
|
||
Ok(path) | ||
}) | ||
.transpose() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters