diff --git a/src/index.rs b/src/index.rs index 9fc0f07..1b4b50d 100644 --- a/src/index.rs +++ b/src/index.rs @@ -3,7 +3,7 @@ use semver::VersionReq; use zip::ZipArchive; use crate::config::Config; use crate::file::copy_dir_recursive; -use crate::input::ask_value; +use crate::util::logging::ask_value; use crate::util::mod_file::{parse_mod_info, try_parse_mod_info}; use crate::{done, info, warn, fatal}; use sha3::{Digest, Sha3_256}; diff --git a/src/indexer.rs b/src/indexer.rs index c7c3fe3..385aea7 100644 --- a/src/indexer.rs +++ b/src/indexer.rs @@ -1,5 +1,5 @@ use crate::config::{geode_root}; -use crate::input::ask_value; +use crate::util::logging::ask_value; use std::fs; use std::path::PathBuf; use git2::{Repository, ResetType, IndexAddOption, Signature}; diff --git a/src/sdk.rs b/src/sdk.rs index 7dcea7b..651489a 100644 --- a/src/sdk.rs +++ b/src/sdk.rs @@ -1,13 +1,13 @@ use clap::Subcommand; use colored::Colorize; use crate::config::Config; +use crate::util::logging::ask_confirm; use git2::build::RepoBuilder; use git2::{FetchOptions, RemoteCallbacks, Repository, SubmoduleUpdateOptions}; use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; use semver::{Version, Prerelease}; use serde::Deserialize; use std::fs; -use std::io::{stdin, stdout, Write}; use std::path::{Path, PathBuf}; #[cfg(target_os = "macos")] @@ -93,15 +93,10 @@ pub enum Sdk { fn uninstall() -> bool { let sdk_path = Config::sdk_path(); - warn!("Are you sure you want to uninstall SDK? (GEODE_SDK={sdk_path:?})"); - print!(" (type 'Yes' to proceed) "); - - stdout().flush().unwrap(); - - let mut ans = String::new(); - stdin().read_line(&mut ans).unwrap(); - ans = ans.trim().to_string(); - if ans != "Yes" { + if !ask_confirm( + &format!("Are you sure you want to uninstall Geode SDK? (Installed at {sdk_path:?})"), + false + ) { fail!("Aborting"); return false; } @@ -204,13 +199,13 @@ fn install(config: &mut Config, path: PathBuf, force: bool) { if !force && std::env::var("GEODE_SDK").is_ok() { if Config::try_sdk_path().is_ok() { - fail!("SDK is already installed"); + fail!("SDK is already installed at {}", Config::sdk_path().display()); info!("Use --reinstall if you want to remove the existing installation"); return; } else { let env_sdk_path = std::env::var("GEODE_SDK").unwrap(); info!("GEODE_SDK ({env_sdk_path}) is already set, but seems to point to an invalid sdk installation."); - if !crate::logging::ask_confirm(&"Do you wish to proceed?".into(), true) { + if !crate::logging::ask_confirm("Do you wish to proceed?", true) { fatal!("Aborting"); } } diff --git a/src/template.rs b/src/template.rs index c905456..e26dc72 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,7 +1,7 @@ -use crate::input::ask_value; + use crate::config::Config; use crate::sdk::get_version; -use crate::util::input::ask_yesno; +use crate::util::logging::{ask_confirm, ask_value}; use crate::{done, info, warn}; use git2::Repository; use path_absolutize::Absolutize; @@ -23,7 +23,7 @@ fn create_template( ) { if project_location.exists() { warn!("The provided location already exists."); - if !ask_yesno("Are you sure you want to proceed?", false) { + if !ask_confirm("Are you sure you want to proceed?", false) { info!("Aborting"); return; } @@ -157,7 +157,7 @@ pub fn build_template(config: &mut Config, location: Option) { final_name.to_lowercase().replace(' ', "_") ); - let strip = ask_yesno( + let strip = ask_confirm( "Do you want to remove comments from the default template?", false ); diff --git a/src/util/input.rs b/src/util/input.rs deleted file mode 100644 index 9141983..0000000 --- a/src/util/input.rs +++ /dev/null @@ -1,40 +0,0 @@ -use std::io::{stdout, stdin, Write}; - -use crate::fail; -use rustyline::Editor; - -pub fn ask_value(prompt: &str, default: Option<&str>, required: bool) -> String { - let text = format!("{}{}: ", prompt, if required { "" } else { " (optional)" }); - let mut line_reader = Editor::<()>::new(); - loop { - let line = line_reader - .readline_with_initial(&text, (default.unwrap_or(""), "")) - .expect("Error reading line"); - line_reader.add_history_entry(&line); - - if line.is_empty() { - if required { - fail!("Please enter a value"); - } else { - return default.unwrap_or("").to_string(); - } - } else { - return line.trim().to_string(); - } - } -} - -pub fn ask_yesno(prompt: &str, default: bool) -> bool { - print!("{} ({}) ", prompt, if default { "Y/n" } else { "y/N" }); - - stdout().flush().unwrap(); - - let mut ans = String::new(); - stdin().read_line(&mut ans).unwrap(); - ans = ans.trim().to_string().to_lowercase(); - match ans.as_str() { - "y" | "ye" | "yes" => true, - "n" | "no" => false, - _ => default - } -} diff --git a/src/util/logging.rs b/src/util/logging.rs index f2cef0b..26b8870 100644 --- a/src/util/logging.rs +++ b/src/util/logging.rs @@ -1,5 +1,7 @@ use std::io::Write; +use rustyline::Editor; + #[macro_export] macro_rules! info { ($x:expr $(, $more:expr)*) => {{ @@ -48,7 +50,28 @@ macro_rules! confirm { }; } -pub fn ask_confirm(text: &String, default: bool) -> bool { +pub fn ask_value(prompt: &str, default: Option<&str>, required: bool) -> String { + let text = format!("{}{}: ", prompt, if required { "" } else { " (optional)" }); + let mut line_reader = Editor::<()>::new(); + loop { + let line = line_reader + .readline_with_initial(&text, (default.unwrap_or(""), "")) + .expect("Error reading line"); + line_reader.add_history_entry(&line); + + if line.is_empty() { + if required { + fail!("Please enter a value"); + } else { + return default.unwrap_or("").to_string(); + } + } else { + return line.trim().to_string(); + } + } +} + +pub fn ask_confirm(text: &str, default: bool) -> bool { use ::colored::Colorize; // print question print!( @@ -61,11 +84,8 @@ pub fn ask_confirm(text: &String, default: bool) -> bool { let mut yes = String::new(); match std::io::stdin().read_line(&mut yes) { Ok(_) => match yes.trim().to_lowercase().as_str() { - "yes" => true, - "ye" => true, - "y" => true, - "no" => false, - "n" => false, + "yes" | "ye" | "y" => true, + "no" | "n" => false, _ => default, }, Err(_) => default, diff --git a/src/util/mod.rs b/src/util/mod.rs index 9349bbc..072241b 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -5,7 +5,6 @@ pub mod logging; pub mod mod_file; pub mod rgba4444; pub mod spritesheet; -pub mod input; #[cfg(target_os = "macos")] pub mod launchctl;