diff --git a/Cargo.lock b/Cargo.lock index b5f4702..7eda479 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -240,7 +289,7 @@ dependencies = [ "bitflags 2.5.0", "cexpr", "clang-sys", - "itertools 0.11.0", + "itertools 0.12.1", "lazy_static", "lazycell", "proc-macro2", @@ -505,6 +554,46 @@ dependencies = [ "libloading", ] +[[package]] +name = "clap" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + [[package]] name = "cocoa" version = "0.24.1" @@ -557,6 +646,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -1791,6 +1886,12 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -2041,6 +2142,7 @@ dependencies = [ "anyhow", "backtrace", "chrono", + "clap", "directories", "druid", "embed-resource", @@ -3244,6 +3346,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "1.0.109" @@ -3738,6 +3846,12 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14706d2a800ee8ff38c1d3edb873cd616971ea59eb7c0d046bb44ef59b06a1ae" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "v_frame" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index 45af1da..125096b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..df3b839 --- /dev/null +++ b/src/cli.rs @@ -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, +} diff --git a/src/config.rs b/src/config.rs index 4f35e67..4d476c8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -131,23 +131,22 @@ static CONFIG_PATH: Lazy = Lazy::new(|| { }); impl Config { - pub fn load(split_file: Option) -> Self { + pub fn load(split_file: Option) -> 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) -> Self { + fn load_path_splits(mut self, split_file: Option) -> 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 } diff --git a/src/main.rs b/src/main.rs index 0f89b3c..bc04b16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -19,6 +19,7 @@ static GLOBAL: MiMalloc = MiMalloc; use crate::config::Config; +mod cli; mod color_button; mod combo_box; mod config; @@ -170,8 +171,8 @@ impl Lens 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); }