diff --git a/yazi-config/src/boot/boot.rs b/yazi-config/src/boot/boot.rs index bf6a882f..7673d753 100644 --- a/yazi-config/src/boot/boot.rs +++ b/yazi-config/src/boot/boot.rs @@ -1,4 +1,4 @@ -use std::{env, fs, path::PathBuf, process}; +use std::{env, ffi::OsString, fs, path::PathBuf, process}; use clap::Parser; use yazi_shared::expand_path; @@ -8,24 +8,43 @@ use crate::{Xdg, PREVIEW}; #[derive(Debug)] pub struct Boot { - pub cwd: PathBuf, + pub cwd: PathBuf, + pub file: Option, + pub state_dir: PathBuf, pub cwd_file: Option, pub chooser_file: Option, } +impl Boot { + fn parse_entry(entry: Option) -> (PathBuf, Option) { + let Some(entry) = entry else { + return (env::current_dir().unwrap(), None); + }; + + let entry = expand_path(entry); + let parent = entry.parent(); + if parent.is_none() || entry.is_dir() { + return (entry, None); + } + + return (parent.unwrap().to_owned(), Some(entry.file_name().unwrap().to_owned())); + } +} + impl Default for Boot { fn default() -> Self { let args = Args::parse(); - - let cwd = args.cwd.map(expand_path).filter(|p| p.is_dir()).or_else(|| env::current_dir().ok()); + let (cwd, file) = Self::parse_entry(args.entry); let boot = Self { - cwd: cwd.unwrap_or("/".into()), + cwd, + file, + state_dir: Xdg::state_dir().unwrap(), - cwd_file: args.cwd_file, + cwd_file: args.cwd_file, chooser_file: args.chooser_file, }; diff --git a/yazi-config/src/boot/cli.rs b/yazi-config/src/boot/cli.rs index bd1f3b8e..f18cf09c 100644 --- a/yazi-config/src/boot/cli.rs +++ b/yazi-config/src/boot/cli.rs @@ -5,9 +5,9 @@ use clap::{command, Parser}; #[derive(Debug, Parser)] #[command(name = "yazi", version)] pub(super) struct Args { - /// Set the current working directory + /// Set the current working entry #[arg(index = 1)] - pub cwd: Option, + pub entry: Option, /// Write the cwd on exit to this file #[arg(long)] diff --git a/yazi-core/src/manager/commands/create.rs b/yazi-core/src/manager/commands/create.rs index b46099ea..3548f4b8 100644 --- a/yazi-core/src/manager/commands/create.rs +++ b/yazi-core/src/manager/commands/create.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use tokio::fs::{self}; +use tokio::fs; use yazi_config::keymap::Exec; use yazi_shared::Url; diff --git a/yazi-core/src/manager/tabs.rs b/yazi-core/src/manager/tabs.rs index 5bd2ab6d..f3c63427 100644 --- a/yazi-core/src/manager/tabs.rs +++ b/yazi-core/src/manager/tabs.rs @@ -11,6 +11,10 @@ pub struct Tabs { impl Tabs { pub fn make() -> Self { let mut tabs = Self { idx: usize::MAX, items: vec![Tab::from(Url::from(&BOOT.cwd))] }; + if let Some(file) = &BOOT.file { + tabs.items[0].reveal(Url::from(BOOT.cwd.join(file))); + } + tabs.set_idx(0); tabs } diff --git a/yazi-core/src/tab/commands/reveal.rs b/yazi-core/src/tab/commands/reveal.rs index e6f5781b..539e9ece 100644 --- a/yazi-core/src/tab/commands/reveal.rs +++ b/yazi-core/src/tab/commands/reveal.rs @@ -12,6 +12,9 @@ impl From<&Exec> for Opt { Self { target: Url::from(expand_path(e.args.first().map(|s| s.as_str()).unwrap_or(""))) } } } +impl From for Opt { + fn from(target: Url) -> Self { Self { target } } +} impl Tab { pub fn reveal(&mut self, opt: impl Into) -> bool {