Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: new yazi-boot crate #728

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 10 additions & 11 deletions yazi-adaptor/src/adaptor.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
}
}

Expand Down
24 changes: 24 additions & 0 deletions yazi-boot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" ] }
2 changes: 1 addition & 1 deletion yazi-config/build.rs → yazi-boot/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[path = "src/boot/args.rs"]
#[path = "src/args.rs"]
mod args;

use std::{env, error::Error, fs};
Expand Down
File renamed without changes.
20 changes: 17 additions & 3 deletions yazi-config/src/boot/boot.rs → yazi-boot/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()))
}
}

Expand Down Expand Up @@ -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 changes: 15 additions & 0 deletions yazi-boot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 0 additions & 9 deletions yazi-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" ] }
5 changes: 0 additions & 5 deletions yazi-config/src/boot/mod.rs

This file was deleted.

14 changes: 4 additions & 10 deletions yazi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use yazi_shared::RoCell;

mod boot;
pub mod keymap;
mod layout;
mod log;
Expand All @@ -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();
Expand All @@ -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);
Expand Down
37 changes: 17 additions & 20 deletions yazi-config/src/preset.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
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]
pub(crate) fn mix<T>(a: &mut Vec<T>, b: Vec<T>, c: Vec<T>) {
*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;
};

Expand All @@ -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()
}
}
19 changes: 3 additions & 16 deletions yazi-config/src/preview/preview.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions yazi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Loading