Skip to content

Commit

Permalink
Move file picker from libium
Browse files Browse the repository at this point in the history
  • Loading branch information
theRookieCoder committed Oct 1, 2024
1 parent 779e8cd commit 8230ed8
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 81 deletions.
287 changes: 219 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ exclude = [".github", "tests", "media"]
default = ["gui"]

# Replaces the CLI text input with a GUI file dialogue for picking folders
gui = ["libium/gui"]
gui = ["rfd"]


[dependencies]
rfd = { version = "0.14", optional = true, default-features = false, features = [
"xdg-portal",
"tokio",
] }
reqwest = { version = "0.12", default-features = false, features = [
"macos-system-configuration",
"rustls-tls",
Expand All @@ -52,13 +56,13 @@ clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
serde_json = "1.0"
indicatif = "0.17"
octocrab = "0.40"
octocrab = "0.41"
fs_extra = "1.3"
ferinth = "2.11"
colored = "2.1"
futures = "0.3"
inquire = "0.7"
libium = { git = "https://github.com/gorilla-devs/libium", rev = "2fe13808755141b6169ed32013198a7ccfdf109b" }
libium = { git = "https://github.com/gorilla-devs/libium", rev = "0e297821e536f3e83790fbd784a916b2bcdb1cc2" }
# libium = { path = "../libium" }
# libium = "1.32"
anyhow = "1.0"
Expand Down
3 changes: 1 addition & 2 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use colored::Colorize as _;
use libium::{add::Error, iter_ext::IterExt as _};
use std::collections::HashMap;

pub fn display_successes_failures(successes: &[String], failures: Vec<(String, Error)>) -> bool {
if !successes.is_empty() {
Expand Down
58 changes: 58 additions & 0 deletions src/file_picker.rs
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()
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
mod add;
mod cli;
mod download;
mod file_picker;
mod subcommands;

use anyhow::{anyhow, bail, ensure, Result};
Expand Down Expand Up @@ -65,7 +66,7 @@ fn main() -> ExitCode {
#[cfg(windows)]
// Enable colours on conhost (command prompt or powershell)
{
#[expect(clippy::unwrap_used)] // There is actually no error
#[expect(clippy::unwrap_used, reason = "There is actually no error")]
colored::control::set_virtual_terminal(true).unwrap();
}

Expand Down
3 changes: 1 addition & 2 deletions src/subcommands/modpack/add.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::check_output_directory;
use crate::TICK;
use crate::{file_picker::pick_folder, TICK};
use anyhow::{Context as _, Result};
use colored::Colorize as _;
use inquire::Confirm;
use libium::{
config::structs::{Config, Modpack, ModpackIdentifier},
file_picker::pick_folder,
get_minecraft_dir,
iter_ext::IterExt as _,
modpack::add,
Expand Down
3 changes: 2 additions & 1 deletion src/subcommands/modpack/configure.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::check_output_directory;
use crate::file_picker::pick_folder;
use anyhow::Result;
use colored::Colorize as _;
use inquire::Confirm;
use libium::{config::structs::Modpack, file_picker::pick_folder};
use libium::config::structs::Modpack;
use std::path::PathBuf;

pub fn configure(
Expand Down
3 changes: 2 additions & 1 deletion src/subcommands/modpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ pub use info::info;
pub use switch::switch;
pub use upgrade::upgrade;

use crate::file_picker::pick_folder;
use anyhow::{ensure, Context as _, Result};
use fs_extra::dir::{copy, CopyOptions};
use inquire::Confirm;
use libium::{file_picker::pick_folder, HOME};
use libium::HOME;
use std::{fs::read_dir, path::Path};

pub fn check_output_directory(output_dir: &Path) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/profile/configure.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{check_output_directory, pick_minecraft_versions, pick_mod_loader};
use crate::file_picker::pick_folder;
use anyhow::{Context as _, Result};
use inquire::{Select, Text};
use libium::{
config::filters::ProfileParameters as _,
config::structs::{ModLoader, Profile},
file_picker::pick_folder,
};
use std::path::PathBuf;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/profile/create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{check_output_directory, pick_minecraft_versions, pick_mod_loader};
use crate::file_picker::pick_folder;
use anyhow::{bail, ensure, Context as _, Result};
use colored::Colorize as _;
use inquire::{
Expand All @@ -7,7 +8,6 @@ use inquire::{
};
use libium::{
config::structs::{Config, ModLoader, Profile},
file_picker::pick_folder,
get_minecraft_dir,
iter_ext::IterExt as _,
};
Expand Down
3 changes: 2 additions & 1 deletion src/subcommands/profile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ pub use delete::delete;
pub use info::info;
pub use switch::switch;

use crate::file_picker::pick_folder;
use anyhow::{ensure, Context as _, Result};
use colored::Colorize as _;
use ferinth::Ferinth;
use fs_extra::dir::{copy, CopyOptions};
use inquire::{Confirm, MultiSelect, Select};
use libium::{config::structs::ModLoader, file_picker::pick_folder, iter_ext::IterExt as _, HOME};
use libium::{config::structs::ModLoader, iter_ext::IterExt as _, HOME};
use std::{
fs::{create_dir_all, read_dir},
path::PathBuf,
Expand Down

0 comments on commit 8230ed8

Please sign in to comment.