Skip to content

Commit

Permalink
Setting split file as cli argument
Browse files Browse the repository at this point in the history
  • Loading branch information
flavio-a committed Nov 8, 2024
1 parent 0e6855d commit 0b4c333
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,24 @@ static CONFIG_PATH: Lazy<PathBuf> = Lazy::new(|| {
});

impl Config {
pub fn load() -> Self {
Self::parse().unwrap_or_default()
pub fn load(split_file: Option<String>) -> Self {
let cfg = Self::parse().unwrap_or_default();
cfg.load_path_splits(split_file)
}

/// 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<String>) -> Self {
if split_file.is_none() { return self; };
let split_file = split_file.unwrap();
let path = PathBuf::from(split_file);
// This reads the file twice, once now and again below when splits are opened
let maybe_run = Config::parse_run_from_path(&path);
if maybe_run.is_none() { return self; };
let (run, _) = maybe_run.unwrap();
self.splits.add_to_history(&run);
self.splits.current = Some(path);
self
}

fn save_config(&self) -> Option<()> {
Expand All @@ -154,15 +170,19 @@ impl Config {
&self.splits.history
}

fn parse_run(&self) -> Option<(Run, bool)> {
let path = self.splits.current.clone()?;
fn parse_run_from_path(path: &Path) -> Option<(Run, bool)> {
let file = fs::read(&path).ok()?;
let parsed_run = composite::parse(&file, Some(&path)).ok()?;
let run = parsed_run.run;
let can_save = parsed_run.kind == TimerKind::LiveSplit;
Some((run, can_save))
}

fn parse_run(&self) -> Option<(Run, bool)> {
let path = self.splits.current.clone()?;
Config::parse_run_from_path(&path)
}

pub fn parse_run_or_default(&mut self) -> Run {
match self.parse_run() {
Some((run, can_save)) => {
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
cell::RefCell,
rc::Rc,
sync::{Arc, RwLock},
env,
};

use druid::{Data, Lens, WindowId};
Expand Down Expand Up @@ -169,7 +170,8 @@ impl Lens<MainState, settings_editor::State> for SettingsEditorLens {
}

fn main() {
let config = Config::load();
// This is clearly super fragile with cli options, but I don't know how to handle them properly
let config = Config::load(env::args().nth(1));
let window = config.build_window();
timer_form::launch(MainState::new(config), window);
}

0 comments on commit 0b4c333

Please sign in to comment.