mirror of https://github.com/lapce/lapce.git
feat: parse commandline args
This commit is contained in:
parent
1268fb9ec8
commit
c06ca6a928
|
@ -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<String>) -> 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(),
|
||||
|
|
|
@ -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<LapceData> {
|
|||
}
|
||||
|
||||
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(
|
||||
|
|
Loading…
Reference in New Issue