Skip to content

Commit

Permalink
Added cli argument for layout and autosplitter file
Browse files Browse the repository at this point in the history
  • Loading branch information
flavio-a committed Nov 11, 2024
1 parent 97cb0ee commit 09e6e0f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// Split file to open. If not given, takes it from config file (the last one used)
pub splits: Option<PathBuf>,

/// 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<PathBuf>,

/// 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<PathBuf>,
}
33 changes: 25 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -131,23 +131,40 @@ static CONFIG_PATH: Lazy<PathBuf> = Lazy::new(|| {
});

impl Config {
pub fn load(split_file: Option<PathBuf>) -> 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<PathBuf>) -> Self {
if split_file.is_none() { return self; };
fn load_splits_path(&mut self, split_file: Option<PathBuf>) {
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<PathBuf>) {
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<PathBuf>) {
if autosplitter_file.is_none() { return; };
self.general.auto_splitter = autosplitter_file;
}

fn save_config(&self) -> Option<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Lens<MainState, settings_editor::State> 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);
}

0 comments on commit 09e6e0f

Please sign in to comment.