Skip to content

Commit

Permalink
remove input.rs and move its stuff to logging.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Mar 6, 2023
1 parent c3817bb commit 872674c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/indexer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
19 changes: 7 additions & 12 deletions src/sdk.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ pub fn build_template(config: &mut Config, location: Option<PathBuf>) {
final_name.to_lowercase().replace(' ', "_")
);

let strip = ask_yesno(
let strip = ask_confirm(
"Do you want to remove comments from the default template?", false
);

Expand Down
40 changes: 0 additions & 40 deletions src/util/input.rs

This file was deleted.

32 changes: 26 additions & 6 deletions src/util/logging.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io::Write;

use rustyline::Editor;

#[macro_export]
macro_rules! info {
($x:expr $(, $more:expr)*) => {{
Expand Down Expand Up @@ -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!(
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 872674c

Please sign in to comment.