Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Dec 12, 2023
1 parent a326998 commit 134b943
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 139 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable"],"version":"0.2"}
{"flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders"],"language":"en","version":"0.2"}
62 changes: 29 additions & 33 deletions yazi-config/preset/yazi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,6 @@ max_height = 900
cache_dir = ""
ueberzug_scale = 1
ueberzug_offset = [ 0, 0, 0, 0 ]
provider = [
{ name = "*/", exec = "plugin folder.lua --sync" },

# Code
{ mime = "text/*", exec = "plugin code.lua" },
{ mime = "*/xml", exec = "plugin code.lua" },
{ mime = "*/javascript", exec = "plugin code.lua" },
{ mime = "*/x-wine-extension-ini", exec = "plugin code.lua" },

# Image
{ mime = "image/vnd.djvu", exec = "" },
{ mime = "image/*", exec = "plugin image.lua" },

# Video
{ mime = "video/*", exec = "plugin video.lua" },

# JSON
{ mime = "application/json", exec = "plugin json.lua" },

# PDF
{ mime = "application/pdf", exec = "plugin pdf.lua" },

# Archive
{ mime = "application/zip", exec = "plugin archive.lua" },
{ mime = "application/gzip", exec = "plugin archive.lua" },
{ mime = "application/x-tar", exec = "plugin archive.lua" },
{ mime = "application/x-bzip", exec = "plugin archive.lua" },
{ mime = "application/x-bzip2", exec = "plugin archive.lua" },
{ mime = "application/x-7z-compressed", exec = "plugin archive.lua" },
{ mime = "application/x-rar", exec = "plugin archive.lua" },
]

[opener]
edit = [
Expand Down Expand Up @@ -104,8 +73,35 @@ image_alloc = 536870912 # 512MB
image_bound = [ 0, 0 ]
suppress_preload = false

[plugins]
preload = []
[plugin]

preloaders = [
# Image
{ mime = "image/vnd.djvu", exec = "noop.lua" },
{ mime = "image/*", exec = "image.lua" },
# Video
{ mime = "video/*", exec = "video.lua" },
# PDF
{ mime = "application/pdf", exec = "pdf.lua" },
]
previewers = [
{ name = "*/", exec = "folder.lua", sync = true },
# Code
{ mime = "text/*", exec = "code.lua" },
{ mime = "*/xml", exec = "code.lua" },
{ mime = "*/javascript", exec = "code.lua" },
{ mime = "*/x-wine-extension-ini", exec = "code.lua" },
# JSON
{ mime = "application/json", exec = "json.lua" },
# Archive
{ mime = "application/zip", exec = "archive.lua" },
{ mime = "application/gzip", exec = "archive.lua" },
{ mime = "application/x-tar", exec = "archive.lua" },
{ mime = "application/x-bzip", exec = "archive.lua" },
{ mime = "application/x-bzip2", exec = "archive.lua" },
{ mime = "application/x-7z-compressed", exec = "archive.lua" },
{ mime = "application/x-rar", exec = "archive.lua" },
]

[input]
# cd
Expand Down
5 changes: 4 additions & 1 deletion yazi-config/src/keymap/exec.rs → yazi-config/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
type Value = Vec<Exec>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a exec string, e.g. `tab_switch 0`")
formatter.write_str("a `exec` string or array of strings")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
Expand All @@ -45,6 +45,9 @@ where
while let Some(value) = &seq.next_element::<String>()? {
execs.push(parse(value).map_err(de::Error::custom)?);
}
if execs.is_empty() {
return Err(de::Error::custom("`exec` cannot be empty"));
}
Ok(execs)
}

Expand Down
2 changes: 1 addition & 1 deletion yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::Key;
#[derive(Debug, Deserialize)]
pub struct Control {
pub on: Vec<Key>,
#[serde(deserialize_with = "super::exec_deserialize")]
#[serde(deserialize_with = "crate::exec_deserialize")]
pub exec: Vec<Exec>,
pub desc: Option<String>,
}
Expand Down
2 changes: 0 additions & 2 deletions yazi-config/src/keymap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod control;
mod exec;
mod key;
mod keymap;

pub use control::*;
#[allow(unused_imports)]
pub use exec::*;
pub use key::*;
pub use keymap::*;
8 changes: 5 additions & 3 deletions yazi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
use yazi_shared::RoCell;

mod boot;
mod exec;
pub mod keymap;
mod layout;
mod log;
pub mod manager;
pub mod open;
mod pattern;
pub mod plugins;
pub mod plugin;
pub mod popup;
mod preset;
pub mod preview;
Expand All @@ -18,6 +19,7 @@ pub mod theme;
mod validation;
mod xdg;

pub(crate) use exec::*;
pub use layout::*;
pub(crate) use pattern::*;
pub(crate) use preset::*;
Expand All @@ -35,7 +37,7 @@ pub static KEYMAP: RoCell<keymap::Keymap> = RoCell::new();
pub static LOG: RoCell<log::Log> = RoCell::new();
pub static MANAGER: RoCell<manager::Manager> = RoCell::new();
pub static OPEN: RoCell<open::Open> = RoCell::new();
pub static PLUGINS: RoCell<plugins::Plugins> = RoCell::new();
pub static PLUGIN: RoCell<plugin::Plugin> = RoCell::new();
pub static PREVIEW: RoCell<preview::Preview> = RoCell::new();
pub static TASKS: RoCell<tasks::Tasks> = RoCell::new();
pub static THEME: RoCell<theme::Theme> = RoCell::new();
Expand All @@ -55,7 +57,7 @@ pub fn init() {
LOG.with(Default::default);
MANAGER.with(Default::default);
OPEN.with(Default::default);
PLUGINS.with(Default::default);
PLUGIN.with(Default::default);
PREVIEW.with(Default::default);
TASKS.with(Default::default);
THEME.with(Default::default);
Expand Down
3 changes: 3 additions & 0 deletions yazi-config/src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod plugin;

pub use plugin::*;
62 changes: 62 additions & 0 deletions yazi-config/src/plugin/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::path::Path;

use serde::Deserialize;
use yazi_shared::{event::Exec, MIME_DIR};

use crate::{pattern::Pattern, MERGED_YAZI};

#[derive(Debug, Deserialize)]
pub struct Plugin {
preloaders: Vec<PluginRule>,
previewers: Vec<PluginRule>,
}

#[derive(Debug, Deserialize)]
pub struct PluginRule {
pub name: Option<Pattern>,
pub mime: Option<Pattern>,
#[serde(deserialize_with = "crate::exec_deserialize")]
pub exec: Vec<Exec>,
#[serde(default)]
pub sync: bool,
}

impl Default for Plugin {
fn default() -> Self {
#[derive(Deserialize)]
struct Outer {
plugin: Plugin,
}

let plugin = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().plugin;

if plugin.previewers.iter().any(|rule| rule.exec.len() != 1) {
panic!("`previewers` must have exactly one `exec`");
}

// TODO: plugin system
// plugin.preload.iter_mut().for_each(|p| {
// *p = expand_path(&p);
// });

plugin
}
}

impl Plugin {
pub fn preloader(&self, path: &Path, mime: &str) -> Option<&PluginRule> {
let is_folder = Some(mime == MIME_DIR);
self.preloaders.iter().find(|&rule| {
rule.mime.as_ref().is_some_and(|m| m.matches(mime))
|| rule.name.as_ref().is_some_and(|n| n.match_path(path, is_folder))
})
}

pub fn previewer(&self, path: &Path, mime: String) -> Option<&PluginRule> {
let is_folder = Some(mime == MIME_DIR);
self.previewers.iter().find(|&rule| {
rule.mime.as_ref().is_some_and(|m| m.matches(&mime))
|| rule.name.as_ref().is_some_and(|n| n.match_path(path, is_folder))
})
}
}
3 changes: 0 additions & 3 deletions yazi-config/src/plugins/mod.rs

This file was deleted.

29 changes: 0 additions & 29 deletions yazi-config/src/plugins/plugins.rs

This file was deleted.

2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Manager {
self.active_mut().preview.reset();
}

self.active_mut().preview.go(hovered, &mime);
self.active_mut().preview.go(hovered, mime);
false
}

Expand Down
6 changes: 4 additions & 2 deletions yazi-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ yazi-shared = { path = "../yazi-shared", version = "0.1.5" }
# External dependencies
ansi-to-tui = "^3"
anyhow = "^1"
md-5 = "^0"
parking_lot = "^0"
ratatui = "^0"
tokio = { version = "^1", features = [ "parking_lot", "rt-multi-thread" ] }
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
unicode-width = "^0"

[target.'cfg(any(target_arch = "riscv64", target_arch="loongarch64"))'.dependencies]
mlua = { version = "^0", features = [ "lua52", "vendored", "serialize" ] }
mlua = { version = "^0", features = [ "lua52", "vendored", "serialize", "macros", "async" ] }

[target.'cfg(not(any(target_arch = "riscv64", target_arch="loongarch64")))'.dependencies]
mlua = { version = "^0", features = [ "luajit52", "vendored", "serialize" ] }
mlua = { version = "^0", features = [ "luajit52", "vendored", "serialize", "macros", "async" ] }
4 changes: 0 additions & 4 deletions yazi-plugin/preset/plugins/folder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,4 @@ function Folder_:peek()
return { ui.List(self.area, items) }
end

function Folder_:seek() return self:peek() end

function Folder_:preload() return 1 end

return Folder_
12 changes: 6 additions & 6 deletions yazi-plugin/preset/plugins/image.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local Image = {}

function Image:peek(file)
local url = ya.cache_exists(file.url .. file.modified) or file.url
function Image:peek()
local url = ya.cache_exists(self.file.url .. self.file.modified) or self.file.url
ya.image_show(url, self.area)
end

function Image:preload(file)
if not ya.cache_exists(file.url .. tostring(file.modified)) then
local cache = ya.cache_path(file.url .. file.modified)
return ya.image_precache(file.url, cache) and 1 or 0
function Image:preload()
if not ya.cache_exists(self.file.url .. tostring(self.file.modified)) then
local cache = ya.cache_path(self.file.url .. self.file.modified)
return ya.image_precache(self.file.url, cache) and 1 or 0
end
return 1
end
Expand Down
14 changes: 5 additions & 9 deletions yazi-plugin/preset/plugins/json.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
local Json = {}

function Json:peek(file) return self:seek(file, 0) end

function Json:seek(file, skip)
function Json:peek()
local limit = self.area.h
local child = Command.new("jq")
:args({
"-C",
"--tab",
".",
tostring(file.url),
tostring(self.file.url),
})
:stdin(Command.NULL)
:stdout(Command.PIPED)
Expand All @@ -26,17 +24,15 @@ function Json:seek(file, skip)

lines = lines .. next
i = i + 1
until i >= skip + limit
until i >= self.skip + limit

child:start_kill()
if skip > 0 and i < skip + limit then
if self.skip > 0 and i < self.skip + limit then
-- TODO
emit("seek", math.max(0, i - limit))
else
ya.preview_widgets(file, skip, { ui.Paragraph.parse(self.area, lines) })
ya.preview_widgets(self.file, self.skip, { ui.Paragraph.parse(self.area, lines) })
end
end

function Json:preload() end

return Json
Loading

0 comments on commit 134b943

Please sign in to comment.