From 5ca18b8cd7d5173e6f46dee57e5dcde381a885df Mon Sep 17 00:00:00 2001 From: khyernet Date: Fri, 3 May 2024 13:07:13 -0800 Subject: [PATCH] start making config file --- src/args/config.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++ src/args/mod.rs | 7 ++++++ 2 files changed, 62 insertions(+) create mode 100644 src/args/config.rs diff --git a/src/args/config.rs b/src/args/config.rs new file mode 100644 index 0000000..36a8282 --- /dev/null +++ b/src/args/config.rs @@ -0,0 +1,55 @@ +use std::path::PathBuf; + +#[cfg(unix)] +const CONFIG_PATH: &str = "$HOME/.config/tcobalt.conf"; +#[cfg(target_os = "windows")] +const CONFIG_PATH: &str = "$LOCALAPPDATA/tcobalt.conf"; + +pub fn load_config_into(args: &mut Vec, instance_list: &mut String) { + let path = PathBuf::from(CONFIG_PATH); + + if path.exists() { + let options = get_config("default"); + for line in options.lines() { + let option: Vec<&str> = line.split('=').map(|s| s.trim()).collect(); + if option.len() != 2 { + continue; + } + + match option[0] { + "vcodec" => {}, + "vquality" => {}, + "aformat" => {}, + "audio-only" => {}, + "mute-audio" => {}, + "twitter-gif" => {}, + "tt-full-audio" => {}, + "tt-h265" => {}, + "dublang" => {}, + "no-metadata" => {}, + "fname-style" => {}, + "instance" => {}, + _ => () + } + } + } +} + +fn get_config(symbol: &str) -> String { + let text = CONFIG_PATH; + + let mut string = String::new(); + let mut select = false; + let mut brackets = 0; + for line in text.lines().into_iter() { + if brackets == 2 { break } + if !select && crate::strings::remove_trailing_whitespace(line) != format!("[{symbol}]") { continue } + select = true; + if line.chars().collect::>().first() == Some(&'[') { brackets += 1; continue } + + string.push_str(line); + string.push('\n'); + } + crate::strings::remove_trailing_whitespace(string) +} + diff --git a/src/args/mod.rs b/src/args/mod.rs index 1fa774d..fc3d714 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -1,6 +1,7 @@ use std::io::Read; pub mod types; +mod config; #[derive(Debug, Clone, PartialEq)] pub struct Args { @@ -64,6 +65,7 @@ impl Args { str.into() } }).collect(); + match self.raw.get(1) { Some(method) => match method.as_str() { "help" | "-h" | "--help" | "h" => { @@ -82,6 +84,11 @@ impl Args { }, "get" | "g" => { self.method = Some(types::Method::Get); + + let mut instance_list = String::new(); + let mut default_args: Vec = Vec::new(); + config::load_config_into(&mut default_args, &mut instance_list); + let mut idx = 1; let mut expected: Vec = Vec::new(); let mut stdin = false;