diff --git a/src/cli.rs b/src/cli.rs index df3b839..eefc1c8 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -5,6 +5,14 @@ use clap::Parser; #[derive(Parser)] #[command(version, about, long_about = None)] pub struct Cli { - /// Split file to open. If not given, opens the last split file used - pub split_file: Option, + /// Split file to open. If not given, takes it from config file (the last one used) + pub splits: Option, + + /// Autosplitter file to open. If not given, takes it from config file (the last one used) + #[arg(short, long, value_name = "FILE")] + pub autosplitter: Option, + + /// Layout file to open. If not given, takes it from config file (the last one used) + #[arg(short, long, value_name = "FILE")] + pub layout: Option, } diff --git a/src/config.rs b/src/config.rs index 4d476c8..7fbd5d5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,7 +22,7 @@ use std::{ sync::Arc, }; -use crate::{timer_form, LayoutData, MainState}; +use crate::{timer_form, LayoutData, MainState, cli}; #[derive(Default, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] @@ -131,23 +131,40 @@ static CONFIG_PATH: Lazy = Lazy::new(|| { }); impl Config { - pub fn load(split_file: Option) -> Self { - let cfg = Self::parse().unwrap_or_default(); - cfg.load_path_splits(split_file) + pub fn load(cli: cli::Cli) -> Self { + let mut cfg = Self::parse().unwrap_or_default(); + cfg.load_splits_path(cli.splits); + cfg.load_layout_path(cli.layout); + cfg.load_autosplitter_path(cli.autosplitter); + cfg.save_config(); + cfg } /// Replace the current splits file with the given path during load, before the window is initialized /// If the file path is invalid it keeps the split file specified in the config - fn load_path_splits(mut self, split_file: Option) -> Self { - if split_file.is_none() { return self; }; + fn load_splits_path(&mut self, split_file: Option) { + if split_file.is_none() { return; }; let split_file = split_file.unwrap(); // This reads the file twice, once now and again below when splits are opened let maybe_run = Config::parse_run_from_path(&split_file); - if maybe_run.is_none() { return self; }; + if maybe_run.is_none() { return; }; let (run, _) = maybe_run.unwrap(); self.splits.add_to_history(&run); self.splits.current = Some(split_file); - self + } + + fn load_layout_path(&mut self, layout_file: Option) { + if layout_file.is_none() { return; }; + let layout_file = layout_file.unwrap(); + let maybe_layout = Self::parse_layout_with_path(&layout_file); + if maybe_layout.is_err() { return; } + self.general.can_save_layout = true; + self.general.layout = Some(layout_file); + } + + fn load_autosplitter_path(&mut self, autosplitter_file: Option) { + if autosplitter_file.is_none() { return; }; + self.general.auto_splitter = autosplitter_file; } fn save_config(&self) -> Option<()> { diff --git a/src/main.rs b/src/main.rs index bc04b16..f5df2cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -172,7 +172,7 @@ impl Lens for SettingsEditorLens { fn main() { let cli = cli::Cli::parse(); - let config = Config::load(cli.split_file); + let config = Config::load(cli); let window = config.build_window(); timer_form::launch(MainState::new(config), window); }