Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Nov 15, 2024
1 parent 453fa45 commit d19381c
Show file tree
Hide file tree
Showing 33 changed files with 336 additions and 136 deletions.
11 changes: 10 additions & 1 deletion yazi-config/preset/yazi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ fetchers = [
{ id = "mime", name = "*", run = "mime", if = "!mime", prio = "high" },
]
spotters = [
{ name = "*/", run = "folder" },
# Code
{ mime = "text/*", run = "code" },
{ mime = "*/{xml,javascript,x-wine-extension-ini}", run = "code" },
# Image
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
{ mime = "image/*", run = "image" },
# Video
{ mime = "video/*", run = "video" },
# Fallback
{ name = "*", run = "file" }
{ name = "*", run = "file" },
]
preloaders = [
# Image
Expand Down
14 changes: 7 additions & 7 deletions yazi-config/src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub struct Plugin {
}

impl Plugin {
pub fn fetchers<'a>(
&'a self,
pub fn fetchers<'a, 'b: 'a>(
&'b self,
path: &'a Path,
mime: &'a str,
factor: impl Fn(&str) -> bool + Copy,
) -> impl Iterator<Item = &'a Fetcher> {
factor: impl Fn(&str) -> bool + Copy + 'a,
) -> impl Iterator<Item = &'b Fetcher> + 'a {
let mut seen = HashSet::new();
self.fetchers.iter().filter(move |&f| {
if seen.contains(&f.id) || !f.matches(path, mime, factor) {
Expand All @@ -39,11 +39,11 @@ impl Plugin {
self.spotters.iter().find(|&p| p.matches(path, mime))
}

pub fn preloaders<'a>(
&'static self,
pub fn preloaders<'a, 'b: 'a>(
&'b self,
path: &'a Path,
mime: &'a str,
) -> impl Iterator<Item = &'static Preloader> + use<'a> {
) -> impl Iterator<Item = &'b Preloader> + 'a {
let mut next = true;
self.preloaders.iter().filter(move |&p| {
if !next || !p.matches(path, mime) {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/manager/commands/tab_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ impl Tabs {
if !opt.current {
tab.cd(opt.url);
} else if let Some(h) = self.active().hovered() {
tab.conf = self.active().conf.clone();
tab.pref = self.active().pref.clone();
tab.apply_files_attrs();
tab.reveal(h.url.to_regular());
} else {
tab.conf = self.active().conf.clone();
tab.pref = self.active().pref.clone();
tab.apply_files_attrs();
tab.cd(self.active().cwd().to_regular());
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/tab/commands/hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::tab::Tab;

impl Tab {
pub fn hidden(&mut self, mut c: Cmd) {
self.conf.show_hidden = match c.take_first_str().as_deref() {
self.pref.show_hidden = match c.take_first_str().as_deref() {
Some("show") => true,
Some("hide") => false,
_ => !self.conf.show_hidden,
_ => !self.pref.show_hidden,
};

let hovered = self.hovered().map(|f| f.url_owned());
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tab/commands/linemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::tab::Tab;

impl Tab {
pub fn linemode(&mut self, mut c: Cmd) {
render!(self.conf.patch(|new| {
render!(self.pref.patch(|new| {
let Some(mode) = c.take_first_str() else {
return;
};
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tab/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Tab {
}

let cwd = self.cwd().to_search(&opt.subject);
let hidden = self.conf.show_hidden;
let hidden = self.pref.show_hidden;

self.search = Some(tokio::spawn(async move {
let rx = if opt.via == SearchOptVia::Rg {
Expand Down
12 changes: 6 additions & 6 deletions yazi-core/src/tab/commands/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use crate::{tab::Tab, tasks::Tasks};

impl Tab {
pub fn sort(&mut self, mut c: Cmd, tasks: &Tasks) {
let conf = &mut self.conf;
let pref = &mut self.pref;
if let Some(by) = c.take_first_str() {
conf.sort_by = SortBy::from_str(&by).unwrap_or_default();
pref.sort_by = SortBy::from_str(&by).unwrap_or_default();
}

conf.sort_reverse = c.maybe_bool("reverse").unwrap_or(conf.sort_reverse);
conf.sort_dir_first = c.maybe_bool("dir-first").unwrap_or(conf.sort_dir_first);
conf.sort_sensitive = c.maybe_bool("sensitive").unwrap_or(conf.sort_sensitive);
conf.sort_translit = c.maybe_bool("translit").unwrap_or(conf.sort_translit);
pref.sort_reverse = c.maybe_bool("reverse").unwrap_or(pref.sort_reverse);
pref.sort_dir_first = c.maybe_bool("dir-first").unwrap_or(pref.sort_dir_first);
pref.sort_sensitive = c.maybe_bool("sensitive").unwrap_or(pref.sort_sensitive);
pref.sort_translit = c.maybe_bool("translit").unwrap_or(pref.sort_translit);

self.apply_files_attrs();
ManagerProxy::update_paged();
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tab/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
yazi_macro::mod_pub!(commands);

yazi_macro::mod_flat!(backstack config finder history mode preview selected tab);
yazi_macro::mod_flat!(backstack preference finder history mode preview selected tab);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use yazi_config::{MANAGER, manager::SortBy};
use yazi_fs::FilesSorter;

#[derive(Clone, PartialEq)]
pub struct Config {
pub struct Preference {
// Sorting
pub sort_by: SortBy,
pub sort_sensitive: bool,
Expand All @@ -15,7 +15,7 @@ pub struct Config {
pub show_hidden: bool,
}

impl Default for Config {
impl Default for Preference {
fn default() -> Self {
Self {
// Sorting
Expand All @@ -32,7 +32,7 @@ impl Default for Config {
}
}

impl Config {
impl Preference {
pub(super) fn patch<F: FnOnce(&mut Self)>(&mut self, f: F) -> bool {
let old = self.clone();
f(self);
Expand Down
10 changes: 5 additions & 5 deletions yazi-core/src/tab/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use yazi_fs::{Folder, FolderStage};
use yazi_macro::render;
use yazi_shared::{Id, Ids, fs::{File, Url}};

use super::{Backstack, Config, Finder, History, Mode, Preview};
use super::{Backstack, Finder, History, Mode, Preference, Preview};
use crate::{spot::Spot, tab::Selected};

pub struct Tab {
pub id: Id,
pub mode: Mode,
pub conf: Config,
pub pref: Preference,
pub current: Folder,
pub parent: Option<Folder>,

Expand All @@ -36,7 +36,7 @@ impl Default for Tab {
Self {
id: IDS.next(),
mode: Default::default(),
conf: Default::default(),
pref: Default::default(),
current: Default::default(),
parent: Default::default(),

Expand Down Expand Up @@ -125,8 +125,8 @@ impl Tab {
}

let hovered = f.hovered().filter(|_| f.tracing).map(|h| h.urn_owned());
f.files.set_show_hidden(self.conf.show_hidden);
f.files.set_sorter(self.conf.sorter());
f.files.set_show_hidden(self.pref.show_hidden);
f.files.set_sorter(self.pref.sorter());

render!(f.files.catchup_revision());
render!(f.repos(hovered.as_ref().map(|u| u.as_urn())));
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/lives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::module_inception)]

yazi_macro::mod_flat!(
config file files filter finder folder iter lives mode preview selected tab tabs
preference file files filter finder folder iter lives mode preview selected tab tabs
tasks yanked
);
14 changes: 7 additions & 7 deletions yazi-fm/src/lives/config.rs → yazi-fm/src/lives/preference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ use mlua::{AnyUserData, UserData, UserDataFields};

use super::Lives;

pub(super) struct Config {
inner: *const yazi_core::tab::Config,
pub(super) struct Preference {
inner: *const yazi_core::tab::Preference,
}

impl Deref for Config {
type Target = yazi_core::tab::Config;
impl Deref for Preference {
type Target = yazi_core::tab::Preference;

fn deref(&self) -> &Self::Target { unsafe { &*self.inner } }
}

impl Config {
impl Preference {
#[inline]
pub(super) fn make(inner: &yazi_core::tab::Config) -> mlua::Result<AnyUserData> {
pub(super) fn make(inner: &yazi_core::tab::Preference) -> mlua::Result<AnyUserData> {
Lives::scoped_userdata(Self { inner })
}
}

impl UserData for Config {
impl UserData for Preference {
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("sort_by", |_, me| Ok(me.sort_by.to_string()));
fields.add_field_method_get("sort_sensitive", |_, me| Ok(me.sort_sensitive));
Expand Down
26 changes: 24 additions & 2 deletions yazi-fm/src/lives/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Deref;
use mlua::{AnyUserData, UserData, UserDataFields, UserDataMethods};
use yazi_plugin::url::UrlRef;

use super::{Config, Finder, Folder, Lives, Mode, Preview, Selected};
use super::{Finder, Folder, Lives, Mode, Preference, Preview, Selected};

pub(super) struct Tab {
inner: *const yazi_core::tab::Tab,
Expand All @@ -26,7 +26,29 @@ impl UserData for Tab {
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("id", |_, me| Ok(me.id.get()));
fields.add_field_method_get("mode", |_, me| Mode::make(&me.mode));
fields.add_field_method_get("conf", |_, me| Config::make(&me.conf));
// TODO: remove `conf` once v0.4 is released
fields.add_field_method_get("conf", |lua, me| {
static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
let id = match lua.named_registry_value::<yazi_plugin::RtRef>("rt")?.current() {
Some(id) => format!("`{id}.yazi` plugin"),
None => "`init.lua` config".to_owned(),
};
let s = "The `conf` property of `tab::Tab` has been deprecated in Yazi v0.4.
Please use `pref` instead, e.g. `cx.active.conf` => `cx.active.pref`, in your {id}.";
yazi_proxy::AppProxy::notify(yazi_proxy::options::NotifyOpt {
title: "Deprecated API".to_owned(),
content: s.replace("{id}", &id),
level: yazi_proxy::options::NotifyLevel::Warn,
timeout: std::time::Duration::from_secs(20),
});
}

Preference::make(&me.pref)
});
fields.add_field_method_get("pref", |_, me| Preference::make(&me.pref));
fields.add_field_method_get("current", |_, me| Folder::make(None, &me.current, me));
fields.add_field_method_get("parent", |_, me| {
me.parent.as_ref().map(|f| Folder::make(None, f, me)).transpose()
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/preset/components/linemode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Linemode:new(file) return setmetatable({ _file = file }, { __index = se
function Linemode:space() return ui.Line(" ") end

function Linemode:solo()
local mode = cx.active.conf.linemode
local mode = cx.active.pref.linemode
if mode == "none" or mode == "solo" then
return ui.Line("")
elseif not self[mode] then
Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/preset/plugins/code.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ function M:seek(units)
})
end

function M:spot(skip) require("file").spot(self, skip) end

return M
45 changes: 41 additions & 4 deletions yazi-plugin/preset/plugins/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,50 @@ end

function M:seek() end

local hovered_mime = ya.sync(function()
local h = cx.active.current.hovered
return h and h:mime()
end)

function M:spot(skip)
local rect = ui.Rect { x = 10, y = 10, w = 20, h = 20 }
local area = ui.Position({ "center", w = 60, h = 20 }):rect(self._window)

local mime = hovered_mime()
local spotter = PLUGIN.spotter(self._file.url, mime)
local previewer = PLUGIN.previewer(self._file.url, mime)
local fetchers = PLUGIN.fetchers(self._file.url, mime)
local preloaders = PLUGIN.preloaders(self._file.url, mime)

for i, v in ipairs(fetchers) do
fetchers[i] = v.cmd
end
for i, v in ipairs(preloaders) do
preloaders[i] = v.cmd
end

local rows = {}
local row = function(key, value)
local h = type(value) == "table" and #value or 1
rows[#rows + 1] = ui.Row({ ui.Span(key):bold(), value }):height(h)
end

row("Mime:", mime)
row("Spotter:", spotter and spotter.cmd or "-")
row("Previewer:", previewer and previewer.cmd or "-")
row("Fetchers:", #fetchers ~= 0 and fetchers or "-")
row("Preloaders:", #preloaders ~= 0 and preloaders or "-")

ya.spot_widgets(self, {
ui.Text("Hello world"):area(rect),
-- ui.Clear(rect),
-- ui.Table(rect),
ui.Clear(area),
ui.Border(ui.Border.ALL):area(area):type(ui.Border.ROUNDED),
ui.Table(rows)
:area(area:padding(ui.Padding.xy(1)))
:row(skip)
:col(1)
:row_style(ui.Style():fg("red"))
:col_style(ui.Style():fg("blue"))
:cell_style(ui.Style():fg("yellow"))
:widths { ui.Constraint.Length(12), ui.Constraint.Fill(1) },
})
end

Expand Down
2 changes: 2 additions & 0 deletions yazi-plugin/preset/plugins/folder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ function M:seek(units)
end
end

function M:spot(skip) require("file").spot(self, skip) end

return M
2 changes: 2 additions & 0 deletions yazi-plugin/preset/plugins/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ function M:preload()
return ya.image_precache(self.file.url, cache) and 1 or 2
end

function M:spot(skip) require("file").spot(self, skip) end

return M
2 changes: 2 additions & 0 deletions yazi-plugin/preset/plugins/magick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ function M:preload()
return status and status.success and 1 or 2
end

function M:spot(skip) require("file").spot(self, skip) end

return M
2 changes: 2 additions & 0 deletions yazi-plugin/preset/plugins/video.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ function M:preload()
return status and status.success and 1 or 2
end

function M:spot(skip) require("file").spot(self, skip) end

return M
2 changes: 1 addition & 1 deletion yazi-plugin/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#![allow(clippy::module_inception)]

yazi_macro::mod_flat!(bindings cha icon input mouse permit position range window);
yazi_macro::mod_flat!(bindings cha icon input mouse permit range window);
Loading

0 comments on commit d19381c

Please sign in to comment.