Skip to content

Commit

Permalink
Added cli argument for split file
Browse files Browse the repository at this point in the history
  • Loading branch information
flavio-a committed Nov 11, 2024
1 parent 0b4c333 commit 97cb0ee
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 9 deletions.
116 changes: 115 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ once_cell = "1.16.0"
native-dialog = "0.7.0"
anyhow = "1.0.68"
fontdb = "0.15.0"
clap = { version = "4.5.20", features = ["derive"] }

[build-dependencies]
embed-resource = "2.4"
Expand Down
10 changes: 10 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::path::PathBuf;

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>,
}
9 changes: 4 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,22 @@ static CONFIG_PATH: Lazy<PathBuf> = Lazy::new(|| {
});

impl Config {
pub fn load(split_file: Option<String>) -> Self {
pub fn load(split_file: Option<PathBuf>) -> 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 {
fn load_path_splits(mut self, split_file: Option<PathBuf>) -> 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);
let maybe_run = Config::parse_run_from_path(&split_file);
if maybe_run.is_none() { return self; };
let (run, _) = maybe_run.unwrap();
self.splits.add_to_history(&run);
self.splits.current = Some(path);
self.splits.current = Some(split_file);
self
}

Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::{
cell::RefCell,
rc::Rc,
sync::{Arc, RwLock},
env,
};

use clap::Parser;
use druid::{Data, Lens, WindowId};
use livesplit_core::{
layout::LayoutState, settings::ImageCache, HotkeySystem, Layout, SharedTimer, Timer,
Expand All @@ -19,6 +19,7 @@ static GLOBAL: MiMalloc = MiMalloc;

use crate::config::Config;

mod cli;
mod color_button;
mod combo_box;
mod config;
Expand Down Expand Up @@ -170,8 +171,8 @@ impl Lens<MainState, settings_editor::State> for SettingsEditorLens {
}

fn main() {
// 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 cli = cli::Cli::parse();
let config = Config::load(cli.split_file);
let window = config.build_window();
timer_form::launch(MainState::new(config), window);
}

0 comments on commit 97cb0ee

Please sign in to comment.