mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: automatically change the current working directory after closing yazi
This commit is contained in:
parent
d198f142e2
commit
a94b0f249a
8 changed files with 43 additions and 35 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::{path::{Path, PathBuf}, sync::Arc};
|
||||
|
||||
use anyhow::Result;
|
||||
use config::{MANAGER, PREVIEW};
|
||||
use config::{BOOT, PREVIEW};
|
||||
use image::{imageops::FilterType, DynamicImage, ImageFormat};
|
||||
use md5::{Digest, Md5};
|
||||
use shared::tty_ratio;
|
||||
|
|
@ -57,8 +57,8 @@ impl Image {
|
|||
|
||||
#[inline]
|
||||
pub fn cache(path: &Path) -> PathBuf {
|
||||
MANAGER
|
||||
.cache
|
||||
BOOT
|
||||
.cache_dir
|
||||
.join(format!("{:x}", Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize()))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use anyhow::{Context, Result};
|
||||
use config::LOG;
|
||||
use config::BOOT;
|
||||
use tracing_appender::non_blocking::WorkerGuard;
|
||||
use tracing_subscriber::{fmt, prelude::__tracing_subscriber_SubscriberExt, Registry};
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ pub(super) struct Logs;
|
|||
|
||||
impl Logs {
|
||||
pub(super) fn init() -> Result<WorkerGuard> {
|
||||
let appender = tracing_appender::rolling::never(&LOG.root, "yazi.log");
|
||||
let appender = tracing_appender::rolling::never(&BOOT.state_dir, "yazi.log");
|
||||
let (handle, guard) = tracing_appender::non_blocking(appender);
|
||||
|
||||
// let filter = EnvFilter::from_default_env();
|
||||
|
|
|
|||
27
config/src/boot/boot.rs
Normal file
27
config/src/boot/boot.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use std::{env, fs, path::PathBuf};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Boot {
|
||||
pub cwd: PathBuf,
|
||||
pub cache_dir: PathBuf,
|
||||
pub state_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Boot {
|
||||
fn default() -> Self {
|
||||
let boot = Self {
|
||||
cwd: env::current_dir().unwrap_or("/".into()),
|
||||
cache_dir: env::temp_dir().join("yazi"),
|
||||
state_dir: xdg::BaseDirectories::with_prefix("yazi").unwrap().get_state_home(),
|
||||
};
|
||||
|
||||
if !boot.cache_dir.is_dir() {
|
||||
fs::create_dir(&boot.cache_dir).unwrap();
|
||||
}
|
||||
if !boot.state_dir.is_dir() {
|
||||
fs::create_dir_all(&boot.state_dir).unwrap();
|
||||
}
|
||||
|
||||
boot
|
||||
}
|
||||
}
|
||||
3
config/src/boot/mod.rs
Normal file
3
config/src/boot/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mod boot;
|
||||
|
||||
pub use boot::*;
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
mod boot;
|
||||
pub mod keymap;
|
||||
mod log;
|
||||
pub mod manager;
|
||||
|
|
@ -18,6 +19,7 @@ static MERGED_KEYMAP: Lazy<String> = Lazy::new(Preset::keymap);
|
|||
static MERGED_THEME: Lazy<String> = Lazy::new(Preset::theme);
|
||||
static MERGED_YAZI: Lazy<String> = Lazy::new(Preset::yazi);
|
||||
|
||||
pub static BOOT: Lazy<boot::Boot> = Lazy::new(Default::default);
|
||||
pub static KEYMAP: Lazy<keymap::Keymap> = Lazy::new(Default::default);
|
||||
pub static LOG: Lazy<log::Log> = Lazy::new(Default::default);
|
||||
pub static MANAGER: Lazy<manager::Manager> = Lazy::new(Default::default);
|
||||
|
|
@ -26,6 +28,7 @@ pub static PREVIEW: Lazy<preview::Preview> = Lazy::new(Default::default);
|
|||
pub static THEME: Lazy<theme::Theme> = Lazy::new(Default::default);
|
||||
|
||||
pub fn init() {
|
||||
Lazy::force(&BOOT);
|
||||
Lazy::force(&KEYMAP);
|
||||
Lazy::force(&LOG);
|
||||
Lazy::force(&MANAGER);
|
||||
|
|
|
|||
|
|
@ -1,13 +1,10 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use serde::{de, Deserialize, Deserializer};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
use crate::MERGED_YAZI;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Log {
|
||||
pub enabled: bool,
|
||||
pub root: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Log {
|
||||
|
|
@ -30,13 +27,6 @@ impl<'de> Deserialize<'de> for Log {
|
|||
|
||||
let outer = Outer::deserialize(deserializer)?;
|
||||
|
||||
let root = xdg::BaseDirectories::with_prefix("yazi")
|
||||
.map_err(|e| de::Error::custom(e.to_string()))?
|
||||
.get_state_home();
|
||||
if !root.is_dir() {
|
||||
std::fs::create_dir_all(&root).map_err(|e| de::Error::custom(e.to_string()))?;
|
||||
}
|
||||
|
||||
Ok(Self { enabled: outer.log.enabled, root })
|
||||
Ok(Self { enabled: outer.log.enabled })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
use std::{env, fs, path::PathBuf};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::SortBy;
|
||||
|
|
@ -7,11 +5,6 @@ use crate::MERGED_YAZI;
|
|||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Manager {
|
||||
#[serde(skip)]
|
||||
pub cwd: PathBuf,
|
||||
#[serde(skip)]
|
||||
pub cache: PathBuf,
|
||||
|
||||
// Sorting
|
||||
pub sort_by: SortBy,
|
||||
pub sort_reverse: bool,
|
||||
|
|
@ -27,14 +20,6 @@ impl Default for Manager {
|
|||
manager: Manager,
|
||||
}
|
||||
|
||||
let mut manager = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().manager;
|
||||
|
||||
manager.cwd = env::current_dir().unwrap_or("/".into());
|
||||
manager.cache = env::temp_dir().join("yazi");
|
||||
if !manager.cache.is_dir() {
|
||||
fs::create_dir(&manager.cache).unwrap();
|
||||
}
|
||||
|
||||
manager
|
||||
toml::from_str::<Outer>(&MERGED_YAZI).unwrap().manager
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use config::MANAGER;
|
||||
use config::BOOT;
|
||||
|
||||
use super::Tab;
|
||||
use crate::emit;
|
||||
|
|
@ -14,7 +14,7 @@ pub struct Tabs {
|
|||
|
||||
impl Tabs {
|
||||
pub fn make() -> Self {
|
||||
let mut tabs = Self { idx: usize::MAX, items: vec![Tab::new(&MANAGER.cwd)] };
|
||||
let mut tabs = Self { idx: usize::MAX, items: vec![Tab::new(&BOOT.cwd)] };
|
||||
tabs.set_idx(0);
|
||||
tabs
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue