feat: new YAZI_CONFIG_HOME env variable (#97)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-31 18:52:13 +08:00 committed by GitHub
parent c0c1a6cae6
commit 018ba56f62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 13 deletions

View file

@ -121,7 +121,7 @@ impl App {
match event { match event {
Event::Cd(path) => { Event::Cd(path) => {
futures::executor::block_on(async { futures::executor::block_on(async {
manager.active_mut().cd(absolute_path(path).await).await; manager.active_mut().cd(absolute_path(path)).await;
}); });
} }
Event::Refresh => { Event::Refresh => {

View file

@ -38,11 +38,8 @@ impl Default for Boot {
fn default() -> Self { fn default() -> Self {
let args = Args::parse(); let args = Args::parse();
let cwd = args let cwd =
.cwd args.cwd.map(absolute_path).filter(|p| p.is_dir()).or_else(|| env::current_dir().ok());
.map(|p| futures::executor::block_on(absolute_path(p)))
.filter(|p| p.is_dir())
.or_else(|| env::current_dir().ok());
let boot = Self { let boot = Self {
cwd: cwd.unwrap_or("/".into()), cwd: cwd.unwrap_or("/".into()),

View file

@ -2,6 +2,7 @@ use std::{path::{Path, PathBuf}, time::{self, SystemTime}};
use md5::{Digest, Md5}; use md5::{Digest, Md5};
use serde::Deserialize; use serde::Deserialize;
use shared::absolute_path;
use super::PreviewAdaptor; use super::PreviewAdaptor;
use crate::{xdg::Xdg, MERGED_YAZI}; use crate::{xdg::Xdg, MERGED_YAZI};
@ -35,7 +36,7 @@ impl Default for Preview {
let preview = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().preview; let preview = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().preview;
let cache_dir = let cache_dir =
preview.cache_dir.filter(|p| !p.is_empty()).map_or_else(Xdg::cache_dir, PathBuf::from); preview.cache_dir.filter(|p| !p.is_empty()).map_or_else(Xdg::cache_dir, absolute_path);
Preview { Preview {
adaptor: Default::default(), adaptor: Default::default(),

View file

@ -78,8 +78,7 @@ impl Default for Theme {
check_validation(theme.tab.validate()); check_validation(theme.tab.validate());
theme.preview.syntect_theme = theme.preview.syntect_theme = absolute_path(&theme.preview.syntect_theme);
futures::executor::block_on(absolute_path(&theme.preview.syntect_theme));
theme theme
} }

View file

@ -1,16 +1,22 @@
use std::{env, path::PathBuf}; use std::{env, path::PathBuf};
use shared::absolute_path;
pub(super) struct Xdg; pub(super) struct Xdg;
impl Xdg { impl Xdg {
pub(super) fn config_dir() -> Option<PathBuf> { pub(super) fn config_dir() -> Option<PathBuf> {
if let Some(s) = env::var_os("YAZI_CONFIG_HOME").filter(|s| !s.is_empty()) {
return Some(absolute_path(s));
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
dirs::config_dir().map(|p| p.join("yazi").join("config")) dirs::config_dir().map(|p| p.join("yazi").join("config"))
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
{ {
std::env::var_os("XDG_CONFIG_HOME") env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from) .map(PathBuf::from)
.filter(|p| p.is_absolute()) .filter(|p| p.is_absolute())
.or_else(|| dirs::home_dir().map(|h| h.join(".config"))) .or_else(|| dirs::home_dir().map(|h| h.join(".config")))
@ -25,7 +31,7 @@ impl Xdg {
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
{ {
std::env::var_os("XDG_STATE_HOME") env::var_os("XDG_STATE_HOME")
.map(PathBuf::from) .map(PathBuf::from)
.filter(|p| p.is_absolute()) .filter(|p| p.is_absolute())
.or_else(|| dirs::home_dir().map(|h| h.join(".local/state"))) .or_else(|| dirs::home_dir().map(|h| h.join(".local/state")))

View file

@ -2,14 +2,14 @@ use std::{env, path::{Path, PathBuf}};
use tokio::fs; use tokio::fs;
pub async fn absolute_path(p: impl AsRef<Path>) -> PathBuf { pub fn absolute_path(p: impl AsRef<Path>) -> PathBuf {
let p = p.as_ref(); let p = p.as_ref();
if let Ok(p) = p.strip_prefix("~") { if let Ok(p) = p.strip_prefix("~") {
if let Some(home) = env::var_os("HOME") { if let Some(home) = env::var_os("HOME") {
return PathBuf::from_iter([&home, p.as_os_str()]); return PathBuf::from_iter([&home, p.as_os_str()]);
} }
} }
fs::canonicalize(p).await.unwrap_or_else(|_| p.to_path_buf()) std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
} }
pub fn readable_path(p: &Path, base: &Path) -> String { pub fn readable_path(p: &Path, base: &Path) -> String {