feat: add support for opening a file on startup (#358)

This commit is contained in:
三咲雅 · Misaki Masa 2023-11-14 08:28:56 +08:00 committed by GitHub
parent 5336f005ce
commit 0931bc2bc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 9 deletions

View file

@ -1,4 +1,4 @@
use std::{env, fs, path::PathBuf, process}; use std::{env, ffi::OsString, fs, path::PathBuf, process};
use clap::Parser; use clap::Parser;
use yazi_shared::expand_path; use yazi_shared::expand_path;
@ -8,24 +8,43 @@ use crate::{Xdg, PREVIEW};
#[derive(Debug)] #[derive(Debug)]
pub struct Boot { pub struct Boot {
pub cwd: PathBuf, pub cwd: PathBuf,
pub file: Option<OsString>,
pub state_dir: PathBuf, pub state_dir: PathBuf,
pub cwd_file: Option<PathBuf>, pub cwd_file: Option<PathBuf>,
pub chooser_file: Option<PathBuf>, pub chooser_file: Option<PathBuf>,
} }
impl Boot {
fn parse_entry(entry: Option<PathBuf>) -> (PathBuf, Option<OsString>) {
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 { impl Default for Boot {
fn default() -> Self { fn default() -> Self {
let args = Args::parse(); let args = Args::parse();
let (cwd, file) = Self::parse_entry(args.entry);
let cwd = args.cwd.map(expand_path).filter(|p| p.is_dir()).or_else(|| env::current_dir().ok());
let boot = Self { let boot = Self {
cwd: cwd.unwrap_or("/".into()), cwd,
file,
state_dir: Xdg::state_dir().unwrap(), state_dir: Xdg::state_dir().unwrap(),
cwd_file: args.cwd_file, cwd_file: args.cwd_file,
chooser_file: args.chooser_file, chooser_file: args.chooser_file,
}; };

View file

@ -5,9 +5,9 @@ use clap::{command, Parser};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(name = "yazi", version)] #[command(name = "yazi", version)]
pub(super) struct Args { pub(super) struct Args {
/// Set the current working directory /// Set the current working entry
#[arg(index = 1)] #[arg(index = 1)]
pub cwd: Option<PathBuf>, pub entry: Option<PathBuf>,
/// Write the cwd on exit to this file /// Write the cwd on exit to this file
#[arg(long)] #[arg(long)]

View file

@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use tokio::fs::{self}; use tokio::fs;
use yazi_config::keymap::Exec; use yazi_config::keymap::Exec;
use yazi_shared::Url; use yazi_shared::Url;

View file

@ -11,6 +11,10 @@ pub struct Tabs {
impl Tabs { impl Tabs {
pub fn make() -> Self { pub fn make() -> Self {
let mut tabs = Self { idx: usize::MAX, items: vec![Tab::from(Url::from(&BOOT.cwd))] }; 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.set_idx(0);
tabs tabs
} }

View file

@ -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(""))) } Self { target: Url::from(expand_path(e.args.first().map(|s| s.as_str()).unwrap_or(""))) }
} }
} }
impl From<Url> for Opt {
fn from(target: Url) -> Self { Self { target } }
}
impl Tab { impl Tab {
pub fn reveal(&mut self, opt: impl Into<Opt>) -> bool { pub fn reveal(&mut self, opt: impl Into<Opt>) -> bool {