diff --git a/src/core/app.rs b/src/core/app.rs index bf75374..59ccbe0 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -66,7 +66,7 @@ impl Calcifer { } } - pub fn from_app_state(app_state: core::AppState) -> Self { + pub fn from_app_state(app_state: core::AppState, file_to_open: Option) -> Self { let mut new = Self { theme: DEFAULT_THEMES[min(app_state.theme, DEFAULT_THEMES.len() - 1)], tabs: Vec::new(), @@ -86,6 +86,10 @@ impl Calcifer { new.open_file(Some(&path)); } } + + if let Some(path) = file_to_open { + new.open_file(Some(&path)); + } if new.tabs == vec![] { new.open_file(None); diff --git a/src/main.rs b/src/main.rs index 24c72e7..2193fb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use egui::{ }; use homedir::get_my_home; use std::{ops::Range, path::PathBuf, sync::Arc, thread, time}; +use std::env; mod core; mod editor; @@ -50,11 +51,21 @@ fn main() -> Result<(), eframe::Error> { } else { core::AppState::default() }; + + let args: Vec = env::args().collect(); + let file_to_open = if args.len() > 1 { + println!("Opening file: {}", args[1].clone()); + let mut path = env::current_dir().unwrap_or_default(); + path.push(args[1].clone()); + Some(path) + } else { + None + }; eframe::run_native( &format!("Calcifer{}", TITLE), options, - Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))), + Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state, file_to_open))), ) }