From 276248a47fba7194dd8157f6bc980056838fd39b Mon Sep 17 00:00:00 2001 From: MeexReay Date: Mon, 10 Feb 2025 22:21:33 +0300 Subject: [PATCH] configure param --- readme.md | 25 +++++++++++++------------ src/config.rs | 47 +++++++++++++++++++++++++++-------------------- src/main.rs | 31 ++++++++++++++++++++----------- 3 files changed, 60 insertions(+), 43 deletions(-) diff --git a/readme.md b/readme.md index 88c7061..63f307e 100644 --- a/readme.md +++ b/readme.md @@ -47,18 +47,19 @@ max_messages: 100 # chat messages limit ## command args ``` - -p, --config-path Print config path - -H, --host Use specified host - -n, --name Use specified name - -F, --message-format Use specified message format - -r, --read-messages Print unformatted messages from chat and exit - -s, --send-message Send unformatted message to chat and exit - -f, --disable-formatting Disable message formatting and sanitizing - -c, --disable-commands Disable slash commands - -i, --disable-ip-hiding Disable ip hiding - -v, --enable-users-ip-viewing Enable users IP viewing - -h, --help Print help - -V, --version Print version +-p, --config-path Print config path +-H, --host Use specified host +-n, --name Use specified name +-F, --message-format Use specified message format +-r, --read-messages Print unformatted messages from chat and exit +-s, --send-message Send unformatted message to chat and exit +-f, --disable-formatting Disable message formatting and sanitizing +-c, --disable-commands Disable slash commands +-i, --disable-ip-hiding Disable ip hiding +-v, --enable-users-ip-viewing Enable users IP viewing +-C, --configure Configure client +-h, --help Print help +-V, --version Print version ``` ## commands diff --git a/src/config.rs b/src/config.rs index b7ff50d..2ecbf8d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,28 +29,35 @@ fn default_update_time() -> usize { 50 } fn default_host() -> String { "meex.lol:11234".to_string() } fn default_message_format() -> String { MESSAGE_FORMAT.to_string() } +pub fn configure(path: PathBuf) -> Config { + let host = get_input("Host (default: meex.lol:11234) > ").unwrap_or("meex.lol:11234".to_string()); + let name = get_input("Name (default: ask every time) > "); + let update_time = get_input("Update interval (default: 50) > ").map(|o| o.parse().ok()).flatten().unwrap_or(50); + let max_messages = get_input("Max messages (default: 100) > ").map(|o| o.parse().ok()).flatten().unwrap_or(100); + let enable_ip_viewing = get_input("Enable users IP viewing? (Y/N, default: N) > ").map(|o| o.to_lowercase() != "n").unwrap_or(false); + let disable_ip_hiding = get_input("Enable your IP viewing? (Y/N, default: N) > ").map(|o| o.to_lowercase() != "n").unwrap_or(false); + + let config = Config { + host, + name, + message_format: MESSAGE_FORMAT.to_string(), + update_time, + max_messages, + enable_ip_viewing, + disable_ip_hiding + }; + + let config_text = serde_yml::to_string(&config).expect("Config save error"); + fs::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error"); + fs::write(&path, config_text).expect("Config save error"); + println!("Config saved! You can edit it in the path got with `bRAC --config-path`"); + + config +} + pub fn load_config(path: PathBuf) -> Config { if !fs::exists(&path).unwrap_or_default() { - let host = get_input("Host (default: meex.lol:11234) > ").unwrap_or("meex.lol:11234".to_string()); - let name = get_input("Name (default: ask every time) > "); - let update_time = get_input("Update interval (default: 50) > ").map(|o| o.parse().ok()).flatten().unwrap_or(50); - let max_messages = get_input("Max messages (default: 100) > ").map(|o| o.parse().ok()).flatten().unwrap_or(100); - let enable_ip_viewing = get_input("Enable users IP viewing? (Y/N, default: N) > ").map(|o| o.to_lowercase() != "n").unwrap_or(false); - let disable_ip_hiding = get_input("Enable your IP viewing? (Y/N, default: N) > ").map(|o| o.to_lowercase() != "n").unwrap_or(false); - - let config = Config { - host, - name, - message_format: MESSAGE_FORMAT.to_string(), - update_time, - max_messages, - enable_ip_viewing, - disable_ip_hiding - }; - let config_text = serde_yml::to_string(&config).expect("Config save error"); - fs::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error"); - fs::write(&path, config_text).expect("Config save error"); - println!("Config saved! You can edit it in the path got with `bRAC --config-path`"); + let config = configure(path.clone()); thread::sleep(Duration::from_secs(4)); config } else { diff --git a/src/main.rs b/src/main.rs index 4ac891d..1247c43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::{ }; use colored::Color; -use config::{get_config_path, load_config}; +use config::{configure, get_config_path, load_config}; use rac::{read_messages, run_recv_loop, send_message}; use rand::random; use regex::Regex; @@ -103,7 +103,7 @@ struct Args { host: Option, /// Use specified name - #[arg(short, long)] + #[arg(short='n', long)] name: Option, /// Use specified message format @@ -111,11 +111,11 @@ struct Args { message_format: Option, /// Print unformatted messages from chat and exit - #[arg(short, long)] + #[arg(short='r', long)] read_messages: bool, /// Send unformatted message to chat and exit - #[arg(short, long, value_name="MESSAGE")] + #[arg(short='s', long, value_name="MESSAGE")] send_message: Option, /// Disable message formatting and sanitizing @@ -133,6 +133,10 @@ struct Args { /// Enable users IP viewing #[arg(short='v', long)] enable_users_ip_viewing: bool, + + /// Configure client + #[arg(short='C', long)] + configure: bool, } @@ -153,15 +157,20 @@ struct Context { fn main() { let args = Args::parse(); + + let config_path = get_config_path(); + + if args.config_path { + print!("{}", config_path.to_string_lossy()); + return; + } + + if args.configure { + configure(config_path); + return; + } let context = { - let config_path = get_config_path(); - - if args.config_path { - print!("{}", config_path.to_string_lossy()); - return; - } - let config = load_config(config_path); Context {