refactor: new yazi-boot crate (#728)

This commit is contained in:
三咲雅 · Misaki Masa 2024-02-26 09:14:04 +08:00 committed by GitHub
parent 42cbedbe42
commit 46516dab74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 135 additions and 96 deletions

24
Cargo.lock generated
View File

@ -2684,18 +2684,27 @@ dependencies = [
"yazi-shared", "yazi-shared",
] ]
[[package]]
name = "yazi-boot"
version = "0.2.3"
dependencies = [
"clap",
"clap_complete",
"clap_complete_fig",
"clap_complete_nushell",
"serde",
"vergen",
"yazi-config",
"yazi-shared",
]
[[package]] [[package]]
name = "yazi-config" name = "yazi-config"
version = "0.2.3" version = "0.2.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"arc-swap", "arc-swap",
"clap",
"clap_complete",
"clap_complete_fig",
"clap_complete_nushell",
"crossterm", "crossterm",
"dirs",
"futures", "futures",
"glob", "glob",
"indexmap", "indexmap",
@ -2705,7 +2714,6 @@ dependencies = [
"shell-words", "shell-words",
"toml", "toml",
"validator", "validator",
"vergen",
"yazi-shared", "yazi-shared",
] ]
@ -2732,6 +2740,7 @@ dependencies = [
"tracing", "tracing",
"unicode-width", "unicode-width",
"yazi-adaptor", "yazi-adaptor",
"yazi-boot",
"yazi-config", "yazi-config",
"yazi-plugin", "yazi-plugin",
"yazi-scheduler", "yazi-scheduler",
@ -2760,6 +2769,7 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
"unicode-width", "unicode-width",
"yazi-adaptor", "yazi-adaptor",
"yazi-boot",
"yazi-config", "yazi-config",
"yazi-core", "yazi-core",
"yazi-plugin", "yazi-plugin",
@ -2791,6 +2801,7 @@ dependencies = [
"unicode-width", "unicode-width",
"uzers", "uzers",
"yazi-adaptor", "yazi-adaptor",
"yazi-boot",
"yazi-config", "yazi-config",
"yazi-prebuild", "yazi-prebuild",
"yazi-shared", "yazi-shared",
@ -2830,6 +2841,7 @@ dependencies = [
"anyhow", "anyhow",
"bitflags 2.4.2", "bitflags 2.4.2",
"crossterm", "crossterm",
"dirs",
"futures", "futures",
"libc", "libc",
"parking_lot", "parking_lot",

View File

@ -1,6 +1,7 @@
cargo publish -p yazi-shared cargo publish -p yazi-shared
cargo publish -p yazi-config cargo publish -p yazi-config
cargo publish -p yazi-adaptor cargo publish -p yazi-adaptor
cargo publish -p yazi-boot
cargo publish -p yazi-scheduler cargo publish -p yazi-scheduler
cargo publish -p yazi-plugin cargo publish -p yazi-plugin
cargo publish -p yazi-core cargo publish -p yazi-core

View File

@ -1,4 +1,4 @@
use std::{env, io::{Read, Write}, path::Path, sync::Arc}; use std::{env, fmt::Display, io::{Read, Write}, path::Path, sync::Arc};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
@ -197,18 +197,17 @@ impl Adaptor {
} }
} }
impl ToString for Adaptor { impl Display for Adaptor {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::Kitty => "kitty", Self::Kitty => write!(f, "kitty"),
Self::KittyOld => "kitty", Self::KittyOld => write!(f, "kitty"),
Self::Iterm2 => "iterm2", Self::Iterm2 => write!(f, "iterm2"),
Self::Sixel => "sixel", Self::Sixel => write!(f, "sixel"),
Self::X11 => "x11", Self::X11 => write!(f, "x11"),
Self::Wayland => "wayland", Self::Wayland => write!(f, "wayland"),
Self::Chafa => "chafa", Self::Chafa => write!(f, "chafa"),
} }
.to_string()
} }
} }

24
yazi-boot/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "yazi-boot"
version = "0.2.3"
edition = "2021"
license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ]
description = "Yazi bootstrapper"
homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi"
[dependencies]
yazi-config = { path = "../yazi-config", version = "0.2.3" }
yazi-shared = { path = "../yazi-shared", version = "0.2.3" }
# External dependencies
clap = { version = "^4", features = [ "derive" ] }
serde = { version = "^1", features = [ "derive" ] }
[build-dependencies]
clap = { version = "^4", features = [ "derive" ] }
clap_complete = "^4"
clap_complete_nushell = "^4"
clap_complete_fig = "^4"
vergen = { version = "^8", features = [ "build", "git", "gitcl" ] }

View File

@ -1,4 +1,4 @@
#[path = "src/boot/args.rs"] #[path = "src/args.rs"]
mod args; mod args;
use std::{env, error::Error, fs}; use std::{env, error::Error, fs};

View File

@ -2,10 +2,11 @@ use std::{ffi::OsString, fs, path::{Path, PathBuf}, process};
use clap::Parser; use clap::Parser;
use serde::Serialize; use serde::Serialize;
use yazi_shared::fs::{current_cwd, expand_path}; use yazi_config::PREVIEW;
use yazi_shared::{fs::{current_cwd, expand_path}, Xdg};
use super::Args; use super::Args;
use crate::{Xdg, ARGS}; use crate::ARGS;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct Boot { pub struct Boot {
@ -29,7 +30,7 @@ impl Boot {
return (entry, None); return (entry, None);
} }
return (parent.unwrap().to_owned(), Some(entry.file_name().unwrap().to_owned())); (parent.unwrap().to_owned(), Some(entry.file_name().unwrap().to_owned()))
} }
} }
@ -67,6 +68,19 @@ impl Default for Args {
process::exit(0); process::exit(0);
} }
if args.clear_cache {
if PREVIEW.cache_dir == Xdg::cache_dir() {
println!("Clearing cache directory: \n{:?}", PREVIEW.cache_dir);
fs::remove_dir_all(&PREVIEW.cache_dir).unwrap();
} else {
println!(
"You've changed the default cache directory, for your data's safety, please clear it manually: \n{:?}",
PREVIEW.cache_dir
);
}
process::exit(0);
}
args args
} }
} }

15
yazi-boot/src/lib.rs Normal file
View File

@ -0,0 +1,15 @@
use yazi_shared::RoCell;
mod args;
mod boot;
pub use args::*;
pub use boot::*;
pub static ARGS: RoCell<Args> = RoCell::new();
pub static BOOT: RoCell<Boot> = RoCell::new();
pub fn init() {
ARGS.with(Default::default);
BOOT.with(Default::default);
}

View File

@ -14,9 +14,7 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.3" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"
arc-swap = "^1" arc-swap = "^1"
clap = { version = "^4", features = [ "derive" ] }
crossterm = "^0" crossterm = "^0"
dirs = "^5"
futures = "^0" futures = "^0"
glob = "^0" glob = "^0"
indexmap = "^2" indexmap = "^2"
@ -26,10 +24,3 @@ serde = { version = "^1", features = [ "derive" ] }
shell-words = "^1" shell-words = "^1"
toml = { version = "^0", features = [ "preserve_order" ] } toml = { version = "^0", features = [ "preserve_order" ] }
validator = { version = "^0", features = [ "derive" ] } validator = { version = "^0", features = [ "derive" ] }
[build-dependencies]
clap = { version = "^4", features = [ "derive" ] }
clap_complete = "^4"
clap_complete_nushell = "^4"
clap_complete_fig = "^4"
vergen = { version = "^8", features = [ "build", "git", "gitcl" ] }

View File

@ -1,5 +0,0 @@
mod args;
mod boot;
pub use args::*;
pub use boot::*;

View File

@ -2,7 +2,6 @@
use yazi_shared::RoCell; use yazi_shared::RoCell;
mod boot;
pub mod keymap; pub mod keymap;
mod layout; mod layout;
mod log; mod log;
@ -18,16 +17,12 @@ mod tasks;
pub mod theme; pub mod theme;
mod validation; mod validation;
pub mod which; pub mod which;
mod xdg;
pub use layout::*; pub use layout::*;
pub(crate) use pattern::*; pub(crate) use pattern::*;
pub(crate) use preset::*; pub(crate) use preset::*;
pub use priority::*; pub use priority::*;
pub(crate) use xdg::*;
pub static ARGS: RoCell<boot::Args> = RoCell::new();
pub static BOOT: RoCell<boot::Boot> = RoCell::new();
pub static LAYOUT: RoCell<arc_swap::ArcSwap<Layout>> = RoCell::new(); pub static LAYOUT: RoCell<arc_swap::ArcSwap<Layout>> = RoCell::new();
static MERGED_KEYMAP: RoCell<String> = RoCell::new(); static MERGED_KEYMAP: RoCell<String> = RoCell::new();
@ -47,13 +42,12 @@ pub static SELECT: RoCell<popup::Select> = RoCell::new();
pub static WHICH: RoCell<which::Which> = RoCell::new(); pub static WHICH: RoCell<which::Which> = RoCell::new();
pub fn init() { pub fn init() {
ARGS.with(Default::default);
BOOT.with(Default::default);
LAYOUT.with(Default::default); LAYOUT.with(Default::default);
MERGED_KEYMAP.with(Preset::keymap); let config_dir = yazi_shared::Xdg::config_dir().unwrap();
MERGED_THEME.with(Preset::theme); MERGED_KEYMAP.init(Preset::keymap(&config_dir));
MERGED_YAZI.with(Preset::yazi); MERGED_THEME.init(Preset::theme(&config_dir));
MERGED_YAZI.init(Preset::yazi(&config_dir));
KEYMAP.with(Default::default); KEYMAP.with(Default::default);
LOG.with(Default::default); LOG.with(Default::default);

View File

@ -1,25 +1,23 @@
use std::{fs, mem}; use std::{mem, path::{Path, PathBuf}};
use toml::Table; use toml::{Table, Value};
use crate::BOOT;
pub(crate) struct Preset; pub(crate) struct Preset;
impl Preset { impl Preset {
#[inline] #[inline]
pub(crate) fn keymap() -> String { pub(crate) fn keymap(dir: &Path) -> String {
Self::merge_str("keymap.toml", include_str!("../preset/keymap.toml")) Self::merge_str(dir.join("keymap.toml"), include_str!("../preset/keymap.toml"))
} }
#[inline] #[inline]
pub(crate) fn theme() -> String { pub(crate) fn theme(dir: &Path) -> String {
Self::merge_str("theme.toml", include_str!("../preset/theme.toml")) Self::merge_str(dir.join("theme.toml"), include_str!("../preset/theme.toml"))
} }
#[inline] #[inline]
pub(crate) fn yazi() -> String { pub(crate) fn yazi(dir: &Path) -> String {
Self::merge_str("yazi.toml", include_str!("../preset/yazi.toml")) Self::merge_str(dir.join("yazi.toml"), include_str!("../preset/yazi.toml"))
} }
#[inline] #[inline]
@ -27,10 +25,10 @@ impl Preset {
*a = b.into_iter().chain(mem::take(a)).chain(c).collect(); *a = b.into_iter().chain(mem::take(a)).chain(c).collect();
} }
fn merge(a: &mut Table, b: &Table, max: u8) { fn merge(a: &mut Table, b: Table, max: u8) {
for (k, v) in b { for (k, v) in b {
let Some(a) = a.get_mut(k) else { let Some(a) = a.get_mut(&k) else {
a.insert(k.clone(), v.clone()); a.insert(k, v);
continue; continue;
}; };
@ -39,21 +37,20 @@ impl Preset {
} }
if let Some(a) = a.as_table_mut() { if let Some(a) = a.as_table_mut() {
if let Some(b) = v.as_table() { if let Value::Table(b) = v {
Self::merge(a, b, max - 1); Self::merge(a, b, max - 1);
continue; continue;
} }
} }
*a = v.clone(); *a = v;
} }
} }
fn merge_str(user: &str, base: &str) -> String { fn merge_str(user: PathBuf, base: &str) -> String {
let path = BOOT.config_dir.join(user); let mut user = std::fs::read_to_string(user).unwrap_or_default().parse::<Table>().unwrap();
let mut user = fs::read_to_string(path).unwrap_or_default().parse::<Table>().unwrap();
let base = base.parse::<Table>().unwrap(); let base = base.parse::<Table>().unwrap();
Self::merge(&mut user, &base, 2);
Self::merge(&mut user, base, 2);
user.to_string() user.to_string()
} }
} }

View File

@ -1,10 +1,10 @@
use std::{fs, path::PathBuf, process, time::{self, SystemTime}}; use std::{fs, path::PathBuf, time::{self, SystemTime}};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use validator::Validate; use validator::Validate;
use yazi_shared::fs::expand_path; use yazi_shared::{fs::expand_path, Xdg};
use crate::{validation::check_validation, xdg::Xdg, ARGS, MERGED_YAZI}; use crate::{validation::check_validation, MERGED_YAZI};
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct Preview { pub struct Preview {
@ -56,19 +56,6 @@ impl Default for Preview {
fs::create_dir(&cache_dir).unwrap(); fs::create_dir(&cache_dir).unwrap();
} }
if ARGS.clear_cache {
if cache_dir == Xdg::cache_dir() {
println!("Clearing cache directory: \n{:?}", cache_dir);
fs::remove_dir_all(&cache_dir).unwrap();
} else {
println!(
"You've changed the default cache directory, for your data's safety, please clear it manually: \n{:?}",
cache_dir
);
}
process::exit(0);
}
Preview { Preview {
tab_size: preview.tab_size, tab_size: preview.tab_size,
max_width: preview.max_width, max_width: preview.max_width,

View File

@ -10,6 +10,7 @@ repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" }
yazi-boot = { path = "../yazi-boot", version = "0.2.3" }
yazi-config = { path = "../yazi-config", version = "0.2.3" } yazi-config = { path = "../yazi-config", version = "0.2.3" }
yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" }
yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.3" } yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.3" }

View File

@ -1,7 +1,8 @@
use std::ffi::OsString; use std::ffi::OsString;
use tracing::error; use tracing::error;
use yazi_config::{popup::SelectCfg, ARGS, OPEN}; use yazi_boot::ARGS;
use yazi_config::{popup::SelectCfg, OPEN};
use yazi_plugin::isolate; use yazi_plugin::isolate;
use yazi_shared::{emit, event::{Cmd, EventQuit}, fs::{File, Url}, Layer, MIME_DIR}; use yazi_shared::{emit, event::{Cmd, EventQuit}, fs::{File, Url}, Layer, MIME_DIR};

View File

@ -1,6 +1,6 @@
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use yazi_config::BOOT; use yazi_boot::BOOT;
use yazi_shared::fs::Url; use yazi_shared::fs::Url;
use crate::{manager::Manager, tab::Tab}; use crate::{manager::Manager, tab::Tab};

View File

@ -10,6 +10,7 @@ repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" }
yazi-boot = { path = "../yazi-boot", version = "0.2.3" }
yazi-config = { path = "../yazi-config", version = "0.2.3" } yazi-config = { path = "../yazi-config", version = "0.2.3" }
yazi-core = { path = "../yazi-core", version = "0.2.3" } yazi-core = { path = "../yazi-core", version = "0.2.3" }
yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" }

View File

@ -1,6 +1,6 @@
use std::ffi::OsString; use std::ffi::OsString;
use yazi_config::ARGS; use yazi_boot::ARGS;
use yazi_shared::{event::EventQuit, term::Term}; use yazi_shared::{event::EventQuit, term::Term};
use crate::app::App; use crate::app::App;

View File

@ -1,7 +1,7 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use tracing_appender::non_blocking::WorkerGuard; use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{fmt, prelude::__tracing_subscriber_SubscriberExt, Registry}; use tracing_subscriber::{fmt, prelude::__tracing_subscriber_SubscriberExt, Registry};
use yazi_config::BOOT; use yazi_boot::BOOT;
pub(super) struct Logs; pub(super) struct Logs;

View File

@ -40,13 +40,15 @@ async fn main() -> anyhow::Result<()> {
yazi_config::init(); yazi_config::init();
yazi_core::init(); yazi_adaptor::init();
yazi_boot::init();
yazi_scheduler::init(); yazi_scheduler::init();
yazi_plugin::init(); yazi_plugin::init();
yazi_adaptor::init(); yazi_core::init();
app::App::run().await app::App::run().await
} }

View File

@ -10,6 +10,7 @@ repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.3" }
yazi-boot = { path = "../yazi-boot", version = "0.2.3" }
yazi-config = { path = "../yazi-config", version = "0.2.3" } yazi-config = { path = "../yazi-config", version = "0.2.3" }
yazi-shared = { path = "../yazi-shared", version = "0.2.3" } yazi-shared = { path = "../yazi-shared", version = "0.2.3" }

View File

@ -1,5 +1,6 @@
use mlua::{Lua, LuaSerdeExt, SerializeOptions}; use mlua::{Lua, LuaSerdeExt, SerializeOptions};
use yazi_config::{BOOT, MANAGER, PREVIEW, THEME}; use yazi_boot::BOOT;
use yazi_config::{MANAGER, PREVIEW, THEME};
const OPTIONS: SerializeOptions = const OPTIONS: SerializeOptions =
SerializeOptions::new().serialize_none_to_null(false).serialize_unit_to_null(false); SerializeOptions::new().serialize_none_to_null(false).serialize_unit_to_null(false);

View File

@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use mlua::{ExternalError, Table}; use mlua::{ExternalError, Table};
use parking_lot::RwLock; use parking_lot::RwLock;
use tokio::fs; use tokio::fs;
use yazi_config::BOOT; use yazi_boot::BOOT;
use yazi_shared::RoCell; use yazi_shared::RoCell;
use crate::LUA; use crate::LUA;

View File

@ -1,6 +1,6 @@
use anyhow::Result; use anyhow::Result;
use mlua::Lua; use mlua::Lua;
use yazi_config::BOOT; use yazi_boot::BOOT;
use yazi_shared::RoCell; use yazi_shared::RoCell;
pub static LUA: RoCell<Lua> = RoCell::new(); pub static LUA: RoCell<Lua> = RoCell::new();

View File

@ -12,6 +12,7 @@ repository = "https://github.com/sxyazi/yazi"
anyhow = "^1" anyhow = "^1"
bitflags = "^2" bitflags = "^2"
crossterm = "^0" crossterm = "^0"
dirs = "^5"
futures = "^0" futures = "^0"
parking_lot = "^0" parking_lot = "^0"
percent-encoding = "^2" percent-encoding = "^2"

View File

@ -16,6 +16,7 @@ mod ro_cell;
pub mod term; pub mod term;
mod throttle; mod throttle;
mod time; mod time;
mod xdg;
pub use chars::*; pub use chars::*;
pub use condition::*; pub use condition::*;
@ -30,3 +31,4 @@ pub use os::*;
pub use ro_cell::*; pub use ro_cell::*;
pub use throttle::*; pub use throttle::*;
pub use time::*; pub use time::*;
pub use xdg::*;

View File

@ -1,11 +1,11 @@
use std::{env, path::PathBuf}; use std::{env, path::PathBuf};
use yazi_shared::fs::expand_path; use crate::fs::expand_path;
pub(super) struct Xdg; pub struct Xdg;
impl Xdg { impl Xdg {
pub(super) fn config_dir() -> Option<PathBuf> { pub fn config_dir() -> Option<PathBuf> {
if let Some(s) = env::var_os("YAZI_CONFIG_HOME").filter(|s| !s.is_empty()) { if let Some(s) = env::var_os("YAZI_CONFIG_HOME").filter(|s| !s.is_empty()) {
return Some(expand_path(s)); return Some(expand_path(s));
} }
@ -25,9 +25,9 @@ impl Xdg {
} }
#[inline] #[inline]
pub(super) fn plugin_dir() -> Option<PathBuf> { Self::config_dir().map(|p| p.join("plugins")) } pub fn plugin_dir() -> Option<PathBuf> { Self::config_dir().map(|p| p.join("plugins")) }
pub(super) fn state_dir() -> Option<PathBuf> { pub fn state_dir() -> Option<PathBuf> {
#[cfg(windows)] #[cfg(windows)]
{ {
dirs::data_dir().map(|p| p.join("yazi").join("state")) dirs::data_dir().map(|p| p.join("yazi").join("state"))
@ -43,5 +43,5 @@ impl Xdg {
} }
#[inline] #[inline]
pub(super) fn cache_dir() -> PathBuf { env::temp_dir().join("yazi") } pub fn cache_dir() -> PathBuf { env::temp_dir().join("yazi") }
} }