feat: add prepend_keymap and append_keymap for configuring mixing (#546)

This commit is contained in:
三咲雅 · Misaki Masa 2024-01-20 13:55:57 +08:00 committed by GitHub
parent 97a7eb7d47
commit 93dc1b78e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 100 additions and 78 deletions

14
Cargo.lock generated
View File

@ -2616,7 +2616,7 @@ dependencies = [
[[package]] [[package]]
name = "yazi-adaptor" name = "yazi-adaptor"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"arc-swap", "arc-swap",
@ -2634,7 +2634,7 @@ dependencies = [
[[package]] [[package]]
name = "yazi-config" name = "yazi-config"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"arc-swap", "arc-swap",
@ -2659,7 +2659,7 @@ dependencies = [
[[package]] [[package]]
name = "yazi-core" name = "yazi-core"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64", "base64",
@ -2688,7 +2688,7 @@ dependencies = [
[[package]] [[package]]
name = "yazi-fm" name = "yazi-fm"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"better-panic", "better-panic",
@ -2715,7 +2715,7 @@ dependencies = [
[[package]] [[package]]
name = "yazi-plugin" name = "yazi-plugin"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"ansi-to-tui", "ansi-to-tui",
"anyhow", "anyhow",
@ -2748,7 +2748,7 @@ checksum = "f4b6c8e12e39ac0f79fa96f36e5b88e0da8d230691abd729eec709b43c74f632"
[[package]] [[package]]
name = "yazi-scheduler" name = "yazi-scheduler"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-priority-channel", "async-priority-channel",
@ -2769,7 +2769,7 @@ dependencies = [
[[package]] [[package]]
name = "yazi-shared" name = "yazi-shared"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags 2.4.1", "bitflags 2.4.1",

View File

@ -7,5 +7,7 @@ cd $SCRIPT_DIR/..
echo "Bumping version: $1" echo "Bumping version: $1"
TOML_FILES="$(git ls-files '*Cargo.toml')" TOML_FILES="$(git ls-files '*Cargo.toml')"
perl -pi -e "s/^version .*= .*\$/version = \"$1\"/" $TOML_FILES perl -pi -e "s/^version .*= .*\$/version = \"$1\"/" -- $TOML_FILES
perl -pi -e "s/^(yazi-[a-z]+)\\s*=\\s*{.*\$/\\1 = { path = \"..\/\\1\", version = \"$1\" }/" $TOML_FILES perl -pi -e "s/^(yazi-[a-z]+)\\s*=\\s*{.*\$/\\1 = { path = \"..\/\\1\", version = \"$1\" }/" -- $TOML_FILES
ESLINT_USE_FLAT_CONFIG=true eslint -c ~/.config/rules/eslint/eslint.config.cjs --fix -- $TOML_FILES

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-adaptor" name = "yazi-adaptor"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]
@ -9,8 +9,8 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi" repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-config = { path = "../yazi-config", version = "0.2.1" } yazi-config = { path = "../yazi-config", version = "0.2.2" }
yazi-shared = { path = "../yazi-shared", version = "0.2.1" } yazi-shared = { path = "../yazi-shared", version = "0.2.2" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-config" name = "yazi-config"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]
@ -9,7 +9,7 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi" repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-shared = { path = "../yazi-shared", version = "0.2.1" } yazi-shared = { path = "../yazi-shared", version = "0.2.2" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Deserializer};
use yazi_shared::Layer; use yazi_shared::Layer;
use super::Control; use super::Control;
use crate::MERGED_KEYMAP; use crate::{Preset, MERGED_KEYMAP};
#[derive(Debug)] #[derive(Debug)]
pub struct Keymap { pub struct Keymap {
@ -30,12 +30,29 @@ impl<'de> Deserialize<'de> for Keymap {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
struct Inner { struct Inner {
keymap: Vec<Control>, keymap: Vec<Control>,
#[serde(default)]
prepend_keymap: Vec<Control>,
#[serde(default)]
append_keymap: Vec<Control>,
} }
let shadow = Shadow::deserialize(deserializer)?; let mut shadow = Shadow::deserialize(deserializer)?;
// TODO: remove this when v0.2.0 is released -- #[rustfmt::skip]
Preset::mix(&mut shadow.manager.keymap, shadow.manager.prepend_keymap, shadow.manager.append_keymap);
#[rustfmt::skip]
Preset::mix(&mut shadow.tasks.keymap, shadow.tasks.prepend_keymap, shadow.tasks.append_keymap);
#[rustfmt::skip]
Preset::mix(&mut shadow.select.keymap, shadow.select.prepend_keymap, shadow.select.append_keymap);
#[rustfmt::skip]
Preset::mix(&mut shadow.input.keymap, shadow.input.prepend_keymap, shadow.input.append_keymap);
#[rustfmt::skip]
Preset::mix(&mut shadow.help.keymap, shadow.help.prepend_keymap, shadow.help.append_keymap);
#[rustfmt::skip]
Preset::mix(&mut shadow.completion.keymap, shadow.completion.prepend_keymap, shadow.completion.append_keymap);
// TODO: remove this when v0.2.3 is released --
if !shadow.input.keymap.iter().any(|c| c.on() == "<Backspace>") { if !shadow.input.keymap.iter().any(|c| c.on() == "<Backspace>") {
println!( println!(
"WARNING: Default keybinding for `<Backspace>` is missing, please add a `{}` to the `[input]` section of `keymap.toml`. "WARNING: Default keybinding for `<Backspace>` is missing, please add a `{}` to the `[input]` section of `keymap.toml`.
@ -43,7 +60,15 @@ In Yazi v0.2.0, `<Backspace>` previously hardcoded within the input component ha
r#"{ on = [ "<Backspace>" ], exec = "backspace" }"# r#"{ on = [ "<Backspace>" ], exec = "backspace" }"#
); );
} }
// TODO: -- remove this when v0.2.0 is released // TODO: -- remove this when v0.2.3 is released
// TODO: remove this when v0.2.3 is released --
if shadow.manager.keymap.iter().any(|c| c.exec().contains("--empty=name")) {
println!(
"WARNING: `rename --empty=name` is deprecated in Yazi v0.2.2, please use `rename --empty=stem` instead.",
);
}
// TODO: -- remove this when v0.2.3 is released
Ok(Self { Ok(Self {
manager: shadow.manager.keymap, manager: shadow.manager.keymap,

View File

@ -3,7 +3,7 @@ use std::path::Path;
use serde::Deserialize; use serde::Deserialize;
use yazi_shared::{event::Exec, Condition, MIME_DIR}; use yazi_shared::{event::Exec, Condition, MIME_DIR};
use crate::{pattern::Pattern, plugin::MAX_PRELOADERS, Priority, MERGED_YAZI}; use crate::{pattern::Pattern, plugin::MAX_PRELOADERS, Preset, Priority, MERGED_YAZI};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Plugin { pub struct Plugin {
@ -30,6 +30,11 @@ pub struct PluginRule {
impl Default for Plugin { impl Default for Plugin {
fn default() -> Self { fn default() -> Self {
#[derive(Deserialize)]
struct Outer {
plugin: Shadow,
}
#[derive(Deserialize)] #[derive(Deserialize)]
struct Shadow { struct Shadow {
preloaders: Vec<PluginRule>, preloaders: Vec<PluginRule>,
@ -45,24 +50,9 @@ impl Default for Plugin {
append_previewers: Vec<PluginRule>, append_previewers: Vec<PluginRule>,
} }
#[derive(Deserialize)]
struct Outer {
plugin: Shadow,
}
let mut shadow = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().plugin; let mut shadow = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().plugin;
shadow.preloaders = shadow Preset::mix(&mut shadow.preloaders, shadow.prepend_preloaders, shadow.append_preloaders);
.prepend_preloaders Preset::mix(&mut shadow.previewers, shadow.prepend_previewers, shadow.append_previewers);
.into_iter()
.chain(shadow.preloaders)
.chain(shadow.append_preloaders)
.collect();
shadow.previewers = shadow
.prepend_previewers
.into_iter()
.chain(shadow.previewers)
.chain(shadow.append_previewers)
.collect();
if shadow.preloaders.len() > MAX_PRELOADERS as usize { if shadow.preloaders.len() > MAX_PRELOADERS as usize {
panic!("Too many preloaders"); panic!("Too many preloaders");

View File

@ -1,4 +1,4 @@
use std::fs; use std::{fs, mem};
use toml::Table; use toml::Table;
@ -7,6 +7,26 @@ use crate::BOOT;
pub(crate) struct Preset; pub(crate) struct Preset;
impl Preset { impl Preset {
#[inline]
pub(crate) fn keymap() -> String {
Self::merge_str("keymap.toml", include_str!("../preset/keymap.toml"))
}
#[inline]
pub(crate) fn theme() -> String {
Self::merge_str("theme.toml", include_str!("../preset/theme.toml"))
}
#[inline]
pub(crate) fn yazi() -> String {
Self::merge_str("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 { for (k, v) in b {
let Some(a) = a.get_mut(k) else { let Some(a) = a.get_mut(k) else {
@ -14,7 +34,7 @@ impl Preset {
continue; continue;
}; };
if k == "icons" || max <= 1 { if max <= 1 {
continue; continue;
} }
@ -36,19 +56,4 @@ impl Preset {
Self::merge(&mut user, &base, 2); Self::merge(&mut user, &base, 2);
user.to_string() user.to_string()
} }
#[inline]
pub(crate) fn keymap() -> String {
Self::merge_str("keymap.toml", include_str!("../preset/keymap.toml"))
}
#[inline]
pub(crate) fn theme() -> String {
Self::merge_str("theme.toml", include_str!("../preset/theme.toml"))
}
#[inline]
pub(crate) fn yazi() -> String {
Self::merge_str("yazi.toml", include_str!("../preset/yazi.toml"))
}
} }

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-core" name = "yazi-core"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]
@ -9,11 +9,11 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi" repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.1" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.2" }
yazi-config = { path = "../yazi-config", version = "0.2.1" } yazi-config = { path = "../yazi-config", version = "0.2.2" }
yazi-plugin = { path = "../yazi-plugin", version = "0.2.1" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.2" }
yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.1" } yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.2" }
yazi-shared = { path = "../yazi-shared", version = "0.2.1" } yazi-shared = { path = "../yazi-shared", version = "0.2.2" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"

View File

@ -33,7 +33,7 @@ impl Manager {
let ext = url.extension(); let ext = url.extension();
match by { match by {
"name" => ext.map_or_else(String::new, |s| format!(".{}", s.to_string_lossy().into_owned())), "stem" => ext.map_or_else(String::new, |s| format!(".{}", s.to_string_lossy().into_owned())),
"ext" if ext.is_some() => format!("{}.", url.file_stem().unwrap().to_string_lossy()), "ext" if ext.is_some() => format!("{}.", url.file_stem().unwrap().to_string_lossy()),
"dot_ext" if ext.is_some() => url.file_stem().unwrap().to_string_lossy().into_owned(), "dot_ext" if ext.is_some() => url.file_stem().unwrap().to_string_lossy().into_owned(),
_ => url.file_name().map_or_else(String::new, |s| s.to_string_lossy().into_owned()), _ => url.file_name().map_or_else(String::new, |s| s.to_string_lossy().into_owned()),

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-fm" name = "yazi-fm"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]
@ -9,12 +9,12 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi" repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.1" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.2" }
yazi-config = { path = "../yazi-config", version = "0.2.1" } yazi-config = { path = "../yazi-config", version = "0.2.2" }
yazi-core = { path = "../yazi-core", version = "0.2.1" } yazi-core = { path = "../yazi-core", version = "0.2.2" }
yazi-plugin = { path = "../yazi-plugin", version = "0.2.1" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.2" }
yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.1" } yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.2" }
yazi-shared = { path = "../yazi-shared", version = "0.2.1" } yazi-shared = { path = "../yazi-shared", version = "0.2.2" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-plugin" name = "yazi-plugin"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]
@ -9,9 +9,9 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi" repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.1" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.2" }
yazi-config = { path = "../yazi-config", version = "0.2.1" } yazi-config = { path = "../yazi-config", version = "0.2.2" }
yazi-shared = { path = "../yazi-shared", version = "0.2.1" } yazi-shared = { path = "../yazi-shared", version = "0.2.2" }
# External dependencies # External dependencies
ansi-to-tui = "^3" ansi-to-tui = "^3"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-scheduler" name = "yazi-scheduler"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]
@ -9,10 +9,10 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi" repository = "https://github.com/sxyazi/yazi"
[dependencies] [dependencies]
yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.1" } yazi-adaptor = { path = "../yazi-adaptor", version = "0.2.2" }
yazi-config = { path = "../yazi-config", version = "0.2.1" } yazi-config = { path = "../yazi-config", version = "0.2.2" }
yazi-shared = { path = "../yazi-shared", version = "0.2.1" } yazi-shared = { path = "../yazi-shared", version = "0.2.2" }
yazi-plugin = { path = "../yazi-plugin", version = "0.2.1" } yazi-plugin = { path = "../yazi-plugin", version = "0.2.2" }
# External dependencies # External dependencies
anyhow = "^1" anyhow = "^1"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "yazi-shared" name = "yazi-shared"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = [ "sxyazi <sxyazi@gmail.com>" ] authors = [ "sxyazi <sxyazi@gmail.com>" ]