From c06ca6a9283993d5783e0d9cb92bac03cf0f2949 Mon Sep 17 00:00:00 2001 From: panekj Date: Mon, 4 Jul 2022 16:24:41 +0200 Subject: [PATCH] feat: parse commandline args --- lapce-data/src/data.rs | 46 ++++++++++++++++++++++++++++++++++++++++-- lapce-ui/src/app.rs | 29 +++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/lapce-data/src/data.rs b/lapce-data/src/data.rs index b0c705ff..17bf1874 100644 --- a/lapce-data/src/data.rs +++ b/lapce-data/src/data.rs @@ -8,6 +8,9 @@ time::Instant, }; +#[cfg(target_os = "windows")] +use std::env; + use anyhow::Result; use crossbeam_channel::{unbounded, Receiver, Sender}; use directories::BaseDirs; @@ -91,7 +94,7 @@ pub struct LapceData { impl LapceData { /// Create a new `LapceData` struct by loading configuration, and state /// previously written to the Lapce database. - pub fn load(event_sink: ExtEventSink) -> Self { + pub fn load(event_sink: ExtEventSink, path: Option) -> Self { let db = Arc::new(LapceDb::new().unwrap()); let mut windows = im::HashMap::new(); let config = Config::load(&LapceWorkspace::default()).unwrap_or_default(); @@ -100,7 +103,46 @@ pub fn load(event_sink: ExtEventSink) -> Self { .get_panel_orders() .unwrap_or_else(|_| Self::default_panel_orders()); - if let Ok(app) = db.get_app() { + if let Some(path) = path { + let path = PathBuf::from(path).canonicalize().unwrap(); + if path.is_dir() { + #[cfg(target_os = "windows")] + let workspace_type = + if !env::var("WSL_DISTRO_NAME").unwrap_or_default().is_empty() + || !env::var("WSLENV").unwrap_or_default().is_empty() + || !env::var("WSL_INTEROP").unwrap_or_default().is_empty() + { + LapceWorkspaceType::RemoteWSL + } else { + LapceWorkspaceType::Local + }; + + #[cfg(not(target_os = "windows"))] + let workspace_type = LapceWorkspaceType::Local; + + let info = WindowInfo { + size: Size::new(800.0, 600.0), + pos: Point::new(0.0, 0.0), + maximised: false, + tabs: TabsInfo { + active_tab: 0, + workspaces: vec![LapceWorkspace { + kind: workspace_type, + path: Some(path), + last_open: 0, + }], + }, + }; + let window = LapceWindowData::new( + keypress.clone(), + panel_orders.clone(), + event_sink.clone(), + &info, + db.clone(), + ); + windows.insert(window.window_id, window); + } + } else if let Ok(app) = db.get_app() { for info in app.windows.iter() { let window = LapceWindowData::new( keypress.clone(), diff --git a/lapce-ui/src/app.rs b/lapce-ui/src/app.rs index 52e26c57..515183fe 100644 --- a/lapce-ui/src/app.rs +++ b/lapce-ui/src/app.rs @@ -11,6 +11,7 @@ config::Config, data::{LapceData, LapceWindowData, LapceWindowLens}, db::{TabsInfo, WindowInfo}, + proxy::VERSION, }; use crate::logging::override_log_levels; @@ -26,6 +27,32 @@ pub fn build_window(data: &mut LapceWindowData) -> impl Widget { } pub fn launch() { + let mut args = std::env::args(); + let mut path = None; + if args.len() > 1 { + args.next(); + for arg in args { + match arg.as_str() { + "-v" | "--version" => { + println!("lapce v{VERSION}"); + return; + } + "-h" | "--help" => { + println!("lapce [-h|--help] [-v|--version] [PATH]"); + return; + } + v => { + if v.starts_with('-') { + eprintln!("lapce: unrecognized option: {v}"); + std::process::exit(1) + } else { + path = Some(v.to_string()) + } + } + } + } + } + let mut log_dispatch = fern::Dispatch::new() .format(|out, message, record| { out.finish(format_args!( @@ -54,7 +81,7 @@ pub fn launch() { } let mut launcher = AppLauncher::new().delegate(LapceAppDelegate::new()); - let mut data = LapceData::load(launcher.get_external_handle()); + let mut data = LapceData::load(launcher.get_external_handle(), path); for (_window_id, window_data) in data.windows.iter_mut() { let root = build_window(window_data); let window = new_window_desc(