configure param

This commit is contained in:
MeexReay 2025-02-10 22:21:33 +03:00
parent ea396bab51
commit 276248a47f
3 changed files with 60 additions and 43 deletions

View File

@ -47,18 +47,19 @@ max_messages: 100 # chat messages limit
## command args ## command args
``` ```
-p, --config-path Print config path -p, --config-path Print config path
-H, --host <HOST> Use specified host -H, --host <HOST> Use specified host
-n, --name <NAME> Use specified name -n, --name <NAME> Use specified name
-F, --message-format <MESSAGE_FORMAT> Use specified message format -F, --message-format <MESSAGE_FORMAT> Use specified message format
-r, --read-messages Print unformatted messages from chat and exit -r, --read-messages Print unformatted messages from chat and exit
-s, --send-message <MESSAGE> Send unformatted message to chat and exit -s, --send-message <MESSAGE> Send unformatted message to chat and exit
-f, --disable-formatting Disable message formatting and sanitizing -f, --disable-formatting Disable message formatting and sanitizing
-c, --disable-commands Disable slash commands -c, --disable-commands Disable slash commands
-i, --disable-ip-hiding Disable ip hiding -i, --disable-ip-hiding Disable ip hiding
-v, --enable-users-ip-viewing Enable users IP viewing -v, --enable-users-ip-viewing Enable users IP viewing
-h, --help Print help -C, --configure Configure client
-V, --version Print version -h, --help Print help
-V, --version Print version
``` ```
## commands ## commands

View File

@ -29,8 +29,7 @@ fn default_update_time() -> usize { 50 }
fn default_host() -> String { "meex.lol:11234".to_string() } fn default_host() -> String { "meex.lol:11234".to_string() }
fn default_message_format() -> String { MESSAGE_FORMAT.to_string() } fn default_message_format() -> String { MESSAGE_FORMAT.to_string() }
pub fn load_config(path: PathBuf) -> Config { pub fn configure(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 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 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 update_time = get_input("Update interval (default: 50) > ").map(|o| o.parse().ok()).flatten().unwrap_or(50);
@ -47,10 +46,18 @@ pub fn load_config(path: PathBuf) -> Config {
enable_ip_viewing, enable_ip_viewing,
disable_ip_hiding disable_ip_hiding
}; };
let config_text = serde_yml::to_string(&config).expect("Config save error"); 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::create_dir_all(&path.parent().expect("Config save error")).expect("Config save error");
fs::write(&path, config_text).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`"); 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 config = configure(path.clone());
thread::sleep(Duration::from_secs(4)); thread::sleep(Duration::from_secs(4));
config config
} else { } else {

View File

@ -3,7 +3,7 @@ use std::{
}; };
use colored::Color; 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 rac::{read_messages, run_recv_loop, send_message};
use rand::random; use rand::random;
use regex::Regex; use regex::Regex;
@ -103,7 +103,7 @@ struct Args {
host: Option<String>, host: Option<String>,
/// Use specified name /// Use specified name
#[arg(short, long)] #[arg(short='n', long)]
name: Option<String>, name: Option<String>,
/// Use specified message format /// Use specified message format
@ -111,11 +111,11 @@ struct Args {
message_format: Option<String>, message_format: Option<String>,
/// Print unformatted messages from chat and exit /// Print unformatted messages from chat and exit
#[arg(short, long)] #[arg(short='r', long)]
read_messages: bool, read_messages: bool,
/// Send unformatted message to chat and exit /// Send unformatted message to chat and exit
#[arg(short, long, value_name="MESSAGE")] #[arg(short='s', long, value_name="MESSAGE")]
send_message: Option<String>, send_message: Option<String>,
/// Disable message formatting and sanitizing /// Disable message formatting and sanitizing
@ -133,6 +133,10 @@ struct Args {
/// Enable users IP viewing /// Enable users IP viewing
#[arg(short='v', long)] #[arg(short='v', long)]
enable_users_ip_viewing: bool, enable_users_ip_viewing: bool,
/// Configure client
#[arg(short='C', long)]
configure: bool,
} }
@ -154,7 +158,6 @@ struct Context {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let context = {
let config_path = get_config_path(); let config_path = get_config_path();
if args.config_path { if args.config_path {
@ -162,6 +165,12 @@ fn main() {
return; return;
} }
if args.configure {
configure(config_path);
return;
}
let context = {
let config = load_config(config_path); let config = load_config(config_path);
Context { Context {