Skip to content

Commit 8230ed8

Browse files
Move file picker from libium
1 parent 779e8cd commit 8230ed8

File tree

11 files changed

+296
-81
lines changed

11 files changed

+296
-81
lines changed

Cargo.lock

Lines changed: 219 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ exclude = [".github", "tests", "media"]
3535
default = ["gui"]
3636

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

4040

4141
[dependencies]
42+
rfd = { version = "0.14", optional = true, default-features = false, features = [
43+
"xdg-portal",
44+
"tokio",
45+
] }
4246
reqwest = { version = "0.12", default-features = false, features = [
4347
"macos-system-configuration",
4448
"rustls-tls",
@@ -52,13 +56,13 @@ clap = { version = "4.5", features = ["derive"] }
5256
clap_complete = "4.5"
5357
serde_json = "1.0"
5458
indicatif = "0.17"
55-
octocrab = "0.40"
59+
octocrab = "0.41"
5660
fs_extra = "1.3"
5761
ferinth = "2.11"
5862
colored = "2.1"
5963
futures = "0.3"
6064
inquire = "0.7"
61-
libium = { git = "https://github.com/gorilla-devs/libium", rev = "2fe13808755141b6169ed32013198a7ccfdf109b" }
65+
libium = { git = "https://github.com/gorilla-devs/libium", rev = "0e297821e536f3e83790fbd784a916b2bcdb1cc2" }
6266
# libium = { path = "../libium" }
6367
# libium = "1.32"
6468
anyhow = "1.0"

src/add.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::collections::HashMap;
2-
31
use colored::Colorize as _;
42
use libium::{add::Error, iter_ext::IterExt as _};
3+
use std::collections::HashMap;
54

65
pub fn display_successes_failures(successes: &[String], failures: Vec<(String, Error)>) -> bool {
76
if !successes.is_empty() {

src/file_picker.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use libium::HOME;
2+
use std::{
3+
io::Result,
4+
path::{Path, PathBuf},
5+
};
6+
7+
#[cfg(feature = "gui")]
8+
/// Uses the system file picker to pick a file, with a `default` path
9+
fn show_folder_picker(default: impl AsRef<Path>, prompt: impl Into<String>) -> Option<PathBuf> {
10+
rfd::FileDialog::new()
11+
.set_can_create_directories(true)
12+
.set_directory(default)
13+
.set_title(prompt)
14+
.pick_folder()
15+
}
16+
17+
#[cfg(not(feature = "gui"))]
18+
/// Uses a terminal input to pick a file, with a `default` path
19+
fn show_folder_picker(default: impl AsRef<Path>, prompt: impl Into<String>) -> Option<PathBuf> {
20+
inquire::Text::new(&prompt.into())
21+
.with_default(&default.as_ref().display().to_string())
22+
.prompt()
23+
.ok()
24+
.map(Into::into)
25+
}
26+
27+
/// Picks a folder using the terminal or system file picker (depending on the feature flag `gui`)
28+
///
29+
/// 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)
30+
pub fn pick_folder(
31+
default: impl AsRef<Path>,
32+
prompt: impl Into<String>,
33+
name: impl AsRef<str>,
34+
) -> Result<Option<PathBuf>> {
35+
show_folder_picker(default, prompt)
36+
.map(|raw_in| {
37+
let path = raw_in
38+
.components()
39+
.map(|c| {
40+
if c.as_os_str() == "~" {
41+
HOME.as_os_str()
42+
} else {
43+
c.as_os_str()
44+
}
45+
})
46+
.collect::<PathBuf>()
47+
.canonicalize()?;
48+
49+
println!(
50+
"✔ \x1b[01m{}\x1b[0m · \x1b[32m{}\x1b[0m",
51+
name.as_ref(),
52+
path.display(),
53+
);
54+
55+
Ok(path)
56+
})
57+
.transpose()
58+
}

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
mod add;
1919
mod cli;
2020
mod download;
21+
mod file_picker;
2122
mod subcommands;
2223

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

src/subcommands/modpack/add.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::check_output_directory;
2-
use crate::TICK;
2+
use crate::{file_picker::pick_folder, TICK};
33
use anyhow::{Context as _, Result};
44
use colored::Colorize as _;
55
use inquire::Confirm;
66
use libium::{
77
config::structs::{Config, Modpack, ModpackIdentifier},
8-
file_picker::pick_folder,
98
get_minecraft_dir,
109
iter_ext::IterExt as _,
1110
modpack::add,

src/subcommands/modpack/configure.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::check_output_directory;
2+
use crate::file_picker::pick_folder;
23
use anyhow::Result;
34
use colored::Colorize as _;
45
use inquire::Confirm;
5-
use libium::{config::structs::Modpack, file_picker::pick_folder};
6+
use libium::config::structs::Modpack;
67
use std::path::PathBuf;
78

89
pub fn configure(

src/subcommands/modpack/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ pub use info::info;
1010
pub use switch::switch;
1111
pub use upgrade::upgrade;
1212

13+
use crate::file_picker::pick_folder;
1314
use anyhow::{ensure, Context as _, Result};
1415
use fs_extra::dir::{copy, CopyOptions};
1516
use inquire::Confirm;
16-
use libium::{file_picker::pick_folder, HOME};
17+
use libium::HOME;
1718
use std::{fs::read_dir, path::Path};
1819

1920
pub fn check_output_directory(output_dir: &Path) -> Result<()> {

src/subcommands/profile/configure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{check_output_directory, pick_minecraft_versions, pick_mod_loader};
2+
use crate::file_picker::pick_folder;
23
use anyhow::{Context as _, Result};
34
use inquire::{Select, Text};
45
use libium::{
56
config::filters::ProfileParameters as _,
67
config::structs::{ModLoader, Profile},
7-
file_picker::pick_folder,
88
};
99
use std::path::PathBuf;
1010

src/subcommands/profile/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{check_output_directory, pick_minecraft_versions, pick_mod_loader};
2+
use crate::file_picker::pick_folder;
23
use anyhow::{bail, ensure, Context as _, Result};
34
use colored::Colorize as _;
45
use inquire::{
@@ -7,7 +8,6 @@ use inquire::{
78
};
89
use libium::{
910
config::structs::{Config, ModLoader, Profile},
10-
file_picker::pick_folder,
1111
get_minecraft_dir,
1212
iter_ext::IterExt as _,
1313
};

0 commit comments

Comments
 (0)