mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-25 09:46:37 +03:00
refactor: new yazi-boot
crate (#728)
This commit is contained in:
parent
42cbedbe42
commit
46516dab74
24
Cargo.lock
generated
24
Cargo.lock
generated
@ -2684,18 +2684,27 @@ dependencies = [
|
||||
"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]]
|
||||
name = "yazi-config"
|
||||
version = "0.2.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"arc-swap",
|
||||
"clap",
|
||||
"clap_complete",
|
||||
"clap_complete_fig",
|
||||
"clap_complete_nushell",
|
||||
"crossterm",
|
||||
"dirs",
|
||||
"futures",
|
||||
"glob",
|
||||
"indexmap",
|
||||
@ -2705,7 +2714,6 @@ dependencies = [
|
||||
"shell-words",
|
||||
"toml",
|
||||
"validator",
|
||||
"vergen",
|
||||
"yazi-shared",
|
||||
]
|
||||
|
||||
@ -2732,6 +2740,7 @@ dependencies = [
|
||||
"tracing",
|
||||
"unicode-width",
|
||||
"yazi-adaptor",
|
||||
"yazi-boot",
|
||||
"yazi-config",
|
||||
"yazi-plugin",
|
||||
"yazi-scheduler",
|
||||
@ -2760,6 +2769,7 @@ dependencies = [
|
||||
"tracing-subscriber",
|
||||
"unicode-width",
|
||||
"yazi-adaptor",
|
||||
"yazi-boot",
|
||||
"yazi-config",
|
||||
"yazi-core",
|
||||
"yazi-plugin",
|
||||
@ -2791,6 +2801,7 @@ dependencies = [
|
||||
"unicode-width",
|
||||
"uzers",
|
||||
"yazi-adaptor",
|
||||
"yazi-boot",
|
||||
"yazi-config",
|
||||
"yazi-prebuild",
|
||||
"yazi-shared",
|
||||
@ -2830,6 +2841,7 @@ dependencies = [
|
||||
"anyhow",
|
||||
"bitflags 2.4.2",
|
||||
"crossterm",
|
||||
"dirs",
|
||||
"futures",
|
||||
"libc",
|
||||
"parking_lot",
|
||||
|
@ -1,6 +1,7 @@
|
||||
cargo publish -p yazi-shared
|
||||
cargo publish -p yazi-config
|
||||
cargo publish -p yazi-adaptor
|
||||
cargo publish -p yazi-boot
|
||||
cargo publish -p yazi-scheduler
|
||||
cargo publish -p yazi-plugin
|
||||
cargo publish -p yazi-core
|
||||
|
@ -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 crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
||||
@ -197,18 +197,17 @@ impl Adaptor {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Adaptor {
|
||||
fn to_string(&self) -> String {
|
||||
impl Display for Adaptor {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Kitty => "kitty",
|
||||
Self::KittyOld => "kitty",
|
||||
Self::Iterm2 => "iterm2",
|
||||
Self::Sixel => "sixel",
|
||||
Self::X11 => "x11",
|
||||
Self::Wayland => "wayland",
|
||||
Self::Chafa => "chafa",
|
||||
Self::Kitty => write!(f, "kitty"),
|
||||
Self::KittyOld => write!(f, "kitty"),
|
||||
Self::Iterm2 => write!(f, "iterm2"),
|
||||
Self::Sixel => write!(f, "sixel"),
|
||||
Self::X11 => write!(f, "x11"),
|
||||
Self::Wayland => write!(f, "wayland"),
|
||||
Self::Chafa => write!(f, "chafa"),
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
24
yazi-boot/Cargo.toml
Normal file
24
yazi-boot/Cargo.toml
Normal 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" ] }
|
@ -1,4 +1,4 @@
|
||||
#[path = "src/boot/args.rs"]
|
||||
#[path = "src/args.rs"]
|
||||
mod args;
|
||||
|
||||
use std::{env, error::Error, fs};
|
@ -2,10 +2,11 @@ use std::{ffi::OsString, fs, path::{Path, PathBuf}, process};
|
||||
|
||||
use clap::Parser;
|
||||
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 crate::{Xdg, ARGS};
|
||||
use crate::ARGS;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Boot {
|
||||
@ -29,7 +30,7 @@ impl Boot {
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
15
yazi-boot/src/lib.rs
Normal file
15
yazi-boot/src/lib.rs
Normal 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);
|
||||
}
|
@ -14,9 +14,7 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.3" }
|
||||
# External dependencies
|
||||
anyhow = "^1"
|
||||
arc-swap = "^1"
|
||||
clap = { version = "^4", features = [ "derive" ] }
|
||||
crossterm = "^0"
|
||||
dirs = "^5"
|
||||
futures = "^0"
|
||||
glob = "^0"
|
||||
indexmap = "^2"
|
||||
@ -26,10 +24,3 @@ serde = { version = "^1", features = [ "derive" ] }
|
||||
shell-words = "^1"
|
||||
toml = { version = "^0", features = [ "preserve_order" ] }
|
||||
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" ] }
|
||||
|
@ -1,5 +0,0 @@
|
||||
mod args;
|
||||
mod boot;
|
||||
|
||||
pub use args::*;
|
||||
pub use boot::*;
|
@ -2,7 +2,6 @@
|
||||
|
||||
use yazi_shared::RoCell;
|
||||
|
||||
mod boot;
|
||||
pub mod keymap;
|
||||
mod layout;
|
||||
mod log;
|
||||
@ -18,16 +17,12 @@ mod tasks;
|
||||
pub mod theme;
|
||||
mod validation;
|
||||
pub mod which;
|
||||
mod xdg;
|
||||
|
||||
pub use layout::*;
|
||||
pub(crate) use pattern::*;
|
||||
pub(crate) use preset::*;
|
||||
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();
|
||||
|
||||
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 fn init() {
|
||||
ARGS.with(Default::default);
|
||||
BOOT.with(Default::default);
|
||||
LAYOUT.with(Default::default);
|
||||
|
||||
MERGED_KEYMAP.with(Preset::keymap);
|
||||
MERGED_THEME.with(Preset::theme);
|
||||
MERGED_YAZI.with(Preset::yazi);
|
||||
let config_dir = yazi_shared::Xdg::config_dir().unwrap();
|
||||
MERGED_KEYMAP.init(Preset::keymap(&config_dir));
|
||||
MERGED_THEME.init(Preset::theme(&config_dir));
|
||||
MERGED_YAZI.init(Preset::yazi(&config_dir));
|
||||
|
||||
KEYMAP.with(Default::default);
|
||||
LOG.with(Default::default);
|
||||
|
@ -1,25 +1,23 @@
|
||||
use std::{fs, mem};
|
||||
use std::{mem, path::{Path, PathBuf}};
|
||||
|
||||
use toml::Table;
|
||||
|
||||
use crate::BOOT;
|
||||
use toml::{Table, Value};
|
||||
|
||||
pub(crate) struct Preset;
|
||||
|
||||
impl Preset {
|
||||
#[inline]
|
||||
pub(crate) fn keymap() -> String {
|
||||
Self::merge_str("keymap.toml", include_str!("../preset/keymap.toml"))
|
||||
pub(crate) fn keymap(dir: &Path) -> String {
|
||||
Self::merge_str(dir.join("keymap.toml"), include_str!("../preset/keymap.toml"))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn theme() -> String {
|
||||
Self::merge_str("theme.toml", include_str!("../preset/theme.toml"))
|
||||
pub(crate) fn theme(dir: &Path) -> String {
|
||||
Self::merge_str(dir.join("theme.toml"), include_str!("../preset/theme.toml"))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn yazi() -> String {
|
||||
Self::merge_str("yazi.toml", include_str!("../preset/yazi.toml"))
|
||||
pub(crate) fn yazi(dir: &Path) -> String {
|
||||
Self::merge_str(dir.join("yazi.toml"), include_str!("../preset/yazi.toml"))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -27,10 +25,10 @@ impl Preset {
|
||||
*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 {
|
||||
let Some(a) = a.get_mut(k) else {
|
||||
a.insert(k.clone(), v.clone());
|
||||
let Some(a) = a.get_mut(&k) else {
|
||||
a.insert(k, v);
|
||||
continue;
|
||||
};
|
||||
|
||||
@ -39,21 +37,20 @@ impl Preset {
|
||||
}
|
||||
|
||||
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);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
*a = v.clone();
|
||||
*a = v;
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_str(user: &str, base: &str) -> String {
|
||||
let path = BOOT.config_dir.join(user);
|
||||
let mut user = fs::read_to_string(path).unwrap_or_default().parse::<Table>().unwrap();
|
||||
|
||||
fn merge_str(user: PathBuf, base: &str) -> String {
|
||||
let mut user = std::fs::read_to_string(user).unwrap_or_default().parse::<Table>().unwrap();
|
||||
let base = base.parse::<Table>().unwrap();
|
||||
Self::merge(&mut user, &base, 2);
|
||||
|
||||
Self::merge(&mut user, base, 2);
|
||||
user.to_string()
|
||||
}
|
||||
}
|
||||
|
@ -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 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)]
|
||||
pub struct Preview {
|
||||
@ -56,19 +56,6 @@ impl Default for Preview {
|
||||
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 {
|
||||
tab_size: preview.tab_size,
|
||||
max_width: preview.max_width,
|
||||
|
@ -10,6 +10,7 @@ repository = "https://github.com/sxyazi/yazi"
|
||||
|
||||
[dependencies]
|
||||
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-plugin = { path = "../yazi-plugin", version = "0.2.3" }
|
||||
yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.3" }
|
||||
|
@ -1,7 +1,8 @@
|
||||
use std::ffi::OsString;
|
||||
|
||||
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_shared::{emit, event::{Cmd, EventQuit}, fs::{File, Url}, Layer, MIME_DIR};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use yazi_config::BOOT;
|
||||
use yazi_boot::BOOT;
|
||||
use yazi_shared::fs::Url;
|
||||
|
||||
use crate::{manager::Manager, tab::Tab};
|
||||
|
@ -10,6 +10,7 @@ repository = "https://github.com/sxyazi/yazi"
|
||||
|
||||
[dependencies]
|
||||
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-core = { path = "../yazi-core", version = "0.2.3" }
|
||||
yazi-plugin = { path = "../yazi-plugin", version = "0.2.3" }
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::ffi::OsString;
|
||||
|
||||
use yazi_config::ARGS;
|
||||
use yazi_boot::ARGS;
|
||||
use yazi_shared::{event::EventQuit, term::Term};
|
||||
|
||||
use crate::app::App;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use anyhow::{Context, Result};
|
||||
use tracing_appender::non_blocking::WorkerGuard;
|
||||
use tracing_subscriber::{fmt, prelude::__tracing_subscriber_SubscriberExt, Registry};
|
||||
use yazi_config::BOOT;
|
||||
use yazi_boot::BOOT;
|
||||
|
||||
pub(super) struct Logs;
|
||||
|
||||
|
@ -40,13 +40,15 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
yazi_config::init();
|
||||
|
||||
yazi_core::init();
|
||||
yazi_adaptor::init();
|
||||
|
||||
yazi_boot::init();
|
||||
|
||||
yazi_scheduler::init();
|
||||
|
||||
yazi_plugin::init();
|
||||
|
||||
yazi_adaptor::init();
|
||||
yazi_core::init();
|
||||
|
||||
app::App::run().await
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ repository = "https://github.com/sxyazi/yazi"
|
||||
|
||||
[dependencies]
|
||||
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-shared = { path = "../yazi-shared", version = "0.2.3" }
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
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 =
|
||||
SerializeOptions::new().serialize_none_to_null(false).serialize_unit_to_null(false);
|
||||
|
@ -4,7 +4,7 @@ use anyhow::{bail, Result};
|
||||
use mlua::{ExternalError, Table};
|
||||
use parking_lot::RwLock;
|
||||
use tokio::fs;
|
||||
use yazi_config::BOOT;
|
||||
use yazi_boot::BOOT;
|
||||
use yazi_shared::RoCell;
|
||||
|
||||
use crate::LUA;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use mlua::Lua;
|
||||
use yazi_config::BOOT;
|
||||
use yazi_boot::BOOT;
|
||||
use yazi_shared::RoCell;
|
||||
|
||||
pub static LUA: RoCell<Lua> = RoCell::new();
|
||||
|
@ -12,6 +12,7 @@ repository = "https://github.com/sxyazi/yazi"
|
||||
anyhow = "^1"
|
||||
bitflags = "^2"
|
||||
crossterm = "^0"
|
||||
dirs = "^5"
|
||||
futures = "^0"
|
||||
parking_lot = "^0"
|
||||
percent-encoding = "^2"
|
||||
|
@ -16,6 +16,7 @@ mod ro_cell;
|
||||
pub mod term;
|
||||
mod throttle;
|
||||
mod time;
|
||||
mod xdg;
|
||||
|
||||
pub use chars::*;
|
||||
pub use condition::*;
|
||||
@ -30,3 +31,4 @@ pub use os::*;
|
||||
pub use ro_cell::*;
|
||||
pub use throttle::*;
|
||||
pub use time::*;
|
||||
pub use xdg::*;
|
||||
|
@ -1,11 +1,11 @@
|
||||
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 {
|
||||
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()) {
|
||||
return Some(expand_path(s));
|
||||
}
|
||||
@ -25,9 +25,9 @@ impl Xdg {
|
||||
}
|
||||
|
||||
#[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)]
|
||||
{
|
||||
dirs::data_dir().map(|p| p.join("yazi").join("state"))
|
||||
@ -43,5 +43,5 @@ impl Xdg {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn cache_dir() -> PathBuf { env::temp_dir().join("yazi") }
|
||||
pub fn cache_dir() -> PathBuf { env::temp_dir().join("yazi") }
|
||||
}
|
Loading…
Reference in New Issue
Block a user