Skip to content

Commit

Permalink
Improve interactive dialogues
Browse files Browse the repository at this point in the history
  • Loading branch information
theRookieCoder committed Oct 1, 2024
1 parent 5e2a69d commit 779e8cd
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 129 deletions.
19 changes: 9 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ferinth = "2.11"
colored = "2.1"
futures = "0.3"
inquire = "0.7"
libium = { git = "https://github.com/gorilla-devs/libium", rev = "b80b16412018e11ae7adde0727eb32065a32ddc7" }
libium = { git = "https://github.com/gorilla-devs/libium", rev = "2fe13808755141b6169ed32013198a7ccfdf109b" }
# libium = { path = "../libium" }
# libium = "1.32"
anyhow = "1.0"
Expand Down
12 changes: 7 additions & 5 deletions src/subcommands/modpack/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::check_output_directory;
use crate::TICK;
use anyhow::{anyhow, Result};
use anyhow::{Context as _, Result};
use colored::Colorize as _;
use inquire::Confirm;
use libium::{
Expand Down Expand Up @@ -29,14 +29,15 @@ pub async fn curseforge(
"Pick an output directory",
"Output Directory",
)?
.ok_or_else(|| anyhow!("Please pick an output directory"))?,
.context("Please pick an output directory")?,
};
check_output_directory(&output_dir)?;
let install_overrides = match install_overrides {
Some(some) => some,
None => Confirm::new("Should overrides be installed?")
.with_default(true)
.prompt()?,
.prompt()
.unwrap_or_default(),
};
if install_overrides {
println!(
Expand Down Expand Up @@ -74,14 +75,15 @@ pub async fn modrinth(
"Pick an output directory",
"Output Directory",
)?
.ok_or_else(|| anyhow!("Please pick an output directory"))?,
.context("Please pick an output directory")?,
};
check_output_directory(&output_dir)?;
let install_overrides = match install_overrides {
Some(some) => some,
None => Confirm::new("Should overrides be installed?")
.with_default(true)
.prompt()?,
.prompt()
.unwrap_or_default(),
};
if install_overrides {
println!(
Expand Down
3 changes: 2 additions & 1 deletion src/subcommands/modpack/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub fn configure(
} else {
let install_overrides = Confirm::new("Should overrides be installed?")
.with_default(modpack.install_overrides)
.prompt()?;
.prompt()
.unwrap_or(modpack.install_overrides);
if install_overrides {
println!(
"{}",
Expand Down
17 changes: 10 additions & 7 deletions src/subcommands/modpack/delete.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::cmp::Ordering;

use super::switch;
use anyhow::{anyhow, Result};
use anyhow::{Context as _, Result};
use colored::Colorize as _;
use inquire::Select;
use libium::{
config::structs::{Config, ModpackIdentifier},
iter_ext::IterExt as _,
};
use std::cmp::Ordering;

pub fn delete(
config: &mut Config,
Expand All @@ -20,7 +19,7 @@ pub fn delete(
.modpacks
.iter()
.position(|modpack| modpack.name == modpack_name)
.ok_or_else(|| anyhow!("The modpack name provided does not exist"))?
.context("The modpack name provided does not exist")?
} else {
let modpack_names = config
.modpacks
Expand All @@ -39,10 +38,14 @@ pub fn delete(
})
.collect_vec();

Select::new("Select which modpack to delete", modpack_names)
if let Ok(selection) = Select::new("Select which modpack to delete", modpack_names)
.with_starting_cursor(config.active_modpack)
.raw_prompt()?
.index
.raw_prompt()
{
selection.index
} else {
return Ok(());
}
};
config.modpacks.remove(selection);

Expand Down
9 changes: 6 additions & 3 deletions src/subcommands/modpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use info::info;
pub use switch::switch;
pub use upgrade::upgrade;

use anyhow::{anyhow, ensure, Context as _, Result};
use anyhow::{ensure, Context as _, Result};
use fs_extra::dir::{copy, CopyOptions};
use inquire::Confirm;
use libium::{file_picker::pick_folder, HOME};
Expand Down Expand Up @@ -38,13 +38,16 @@ pub fn check_output_directory(output_dir: &Path) -> Result<()> {
"There are files in the {} folder in your output directory, these will be deleted when you upgrade.",
check_dir.file_name().context("Unable to get folder name")?.to_string_lossy()
);
if Confirm::new("Would like to create a backup?").prompt()? {
if Confirm::new("Would like to create a backup?")
.prompt()
.unwrap_or_default()
{
let backup_dir = pick_folder(
&*HOME,
"Where should the backup be made?",
"Output Directory",
)?
.ok_or_else(|| anyhow!("Please pick an output directory"))?;
.context("Please pick an output directory")?;
copy(check_dir, backup_dir, &CopyOptions::new())?;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/subcommands/modpack/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ pub fn switch(config: &mut Config, modpack_name: Option<String>) -> Result<()> {
})
.collect_vec();

config.active_modpack = Select::new("Select which modpack to switch to", modpack_info)
if let Ok(selection) = Select::new("Select which modpack to switch to", modpack_info)
.with_starting_cursor(config.active_modpack)
.raw_prompt()?
.index;
.raw_prompt()
{
config.active_modpack = selection.index;
}
Ok(())
}
}
85 changes: 41 additions & 44 deletions src/subcommands/profile/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,60 +56,57 @@ pub async fn configure(
"Quit",
];

loop {
// TODO: raw_prompt_skippable
let selection = Select::new("Which setting would you like to change", items.clone())
.raw_prompt()
.ok()
.map(|x| x.index);

if let Some(index) = selection {
match index {
0 => {
if let Some(dir) = pick_folder(
&profile.output_dir,
"Pick an output directory",
"Output Directory",
)? {
check_output_directory(&dir).await?;
profile.output_dir = dir;
}
while let Ok(selection) =
Select::new("Which setting would you like to change", items.clone()).raw_prompt()
{
match selection.index {
0 => {
if let Some(dir) = pick_folder(
&profile.output_dir,
"Pick an output directory",
"Output Directory",
)? {
check_output_directory(&dir).await?;
profile.output_dir = dir;
}
1 => {
let Some(versions) = profile.filters.game_versions_mut() else {
println!("Active profile does not filter by game version");
continue;
};
}
1 => {
let Some(versions) = profile.filters.game_versions_mut() else {
println!("Active profile does not filter by game version");
continue;
};

*versions = pick_minecraft_versions().await?;
if let Ok(selection) = pick_minecraft_versions(versions).await {
*versions = selection;
}
2 => {
let Some(loaders) = profile.filters.mod_loaders_mut() else {
println!("Active profile does not filter mod loader");
continue;
};
*loaders = match pick_mod_loader(loaders.first())? {
}
2 => {
let Some(loaders) = profile.filters.mod_loaders_mut() else {
println!("Active profile does not filter mod loader");
continue;
};

if let Ok(selection) = pick_mod_loader(loaders.first()) {
*loaders = match selection {
ModLoader::Quilt => vec![ModLoader::Quilt, ModLoader::Fabric],
loader => vec![loader],
}
}
3 => {
if let Ok(new_name) = Text::new("Change the profile's name")
.with_default(&profile.name)
.prompt()
{
profile.name = new_name;
} else {
continue;
}
}
3 => {
if let Ok(new_name) = Text::new("Change the profile's name")
.with_default(&profile.name)
.prompt()
{
profile.name = new_name;
} else {
continue;
}
4 => break,
_ => unreachable!(),
}
println!();
} else {
break;
4 => break,
_ => unreachable!(),
}
println!();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/subcommands/profile/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn create(
);
if Confirm::new("Would you like to specify a custom mods directory?")
.prompt()
.unwrap_or(false)
.unwrap_or_default()
{
if let Some(dir) = pick_folder(
&selected_mods_dir,
Expand Down Expand Up @@ -74,7 +74,7 @@ pub async fn create(
Profile::new(
name,
selected_mods_dir,
pick_minecraft_versions().await?,
pick_minecraft_versions(&[]).await?,
pick_mod_loader(None)?,
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/subcommands/profile/delete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::cmp::Ordering;

use super::switch;
use anyhow::{Context as _, Result};
use colored::Colorize as _;
Expand All @@ -8,6 +6,7 @@ use libium::{
config::{filters::ProfileParameters as _, structs::Config},
iter_ext::IterExt as _,
};
use std::cmp::Ordering;

pub fn delete(
config: &mut Config,
Expand Down
Loading

0 comments on commit 779e8cd

Please sign in to comment.