mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-21 23:01:05 +00:00
feat: add children_add and children_remove methods to the Header component as well (#1297)
This commit is contained in:
parent
ec33085f67
commit
ed6ae000d6
14 changed files with 184 additions and 134 deletions
|
|
@ -1 +1 @@
|
|||
{"version":"0.2","language":"en","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","prec","imagesize","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink"],"flagWords":[]}
|
||||
{"flagWords":[],"version":"0.2","language":"en","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","prec","imagesize","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes"]}
|
||||
|
|
@ -17,15 +17,15 @@ impl App {
|
|||
};
|
||||
|
||||
if !opt.sync {
|
||||
return self.cx.tasks.plugin_micro(opt.name, opt.args);
|
||||
return self.cx.tasks.plugin_micro(opt.id, opt.args);
|
||||
}
|
||||
|
||||
if LOADER.read().contains_key(&opt.name) {
|
||||
if LOADER.read().contains_key(&opt.id) {
|
||||
return self.plugin_do(opt);
|
||||
}
|
||||
|
||||
tokio::spawn(async move {
|
||||
if LOADER.ensure(&opt.name).await.is_ok() {
|
||||
if LOADER.ensure(&opt.id).await.is_ok() {
|
||||
Self::_plugin_do(opt);
|
||||
}
|
||||
});
|
||||
|
|
@ -44,12 +44,12 @@ impl App {
|
|||
};
|
||||
|
||||
match LUA.named_registry_value::<RtRef>("rt") {
|
||||
Ok(mut r) => r.swap(&opt.name),
|
||||
Ok(mut r) => r.swap(&opt.id),
|
||||
Err(e) => return warn!("{e}"),
|
||||
}
|
||||
|
||||
defer! { LUA.named_registry_value::<RtRef>("rt").map(|mut r| r.reset()).ok(); };
|
||||
let plugin = match LOADER.load(&opt.name) {
|
||||
let plugin = match LOADER.load(&opt.id) {
|
||||
Ok(plugin) => plugin,
|
||||
Err(e) => return warn!("{e}"),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -4,37 +4,43 @@ Manager = {}
|
|||
Folder = {}
|
||||
File = {}
|
||||
|
||||
local b = false
|
||||
function __yazi_check_and_warn_deprecated_api()
|
||||
if b then
|
||||
return
|
||||
end
|
||||
|
||||
local warn = function(name)
|
||||
ya.notify {
|
||||
title = "Deprecated API",
|
||||
content = string.format(
|
||||
[[The `%s` global variable has been removed in Yazi v0.3, please remove it from your `init.lua`.
|
||||
local function warn(name)
|
||||
ya.notify {
|
||||
title = "Deprecated API",
|
||||
content = string.format(
|
||||
[[The `%s` global variable has been removed in Yazi v0.3, please remove it from your `init.lua`.
|
||||
|
||||
See https://github.com/sxyazi/yazi/pull/1257 for details.]],
|
||||
name
|
||||
),
|
||||
timeout = 20,
|
||||
level = "warn",
|
||||
}
|
||||
name
|
||||
),
|
||||
timeout = 20,
|
||||
level = "warn",
|
||||
}
|
||||
end
|
||||
|
||||
local b1, b2, b3 = false, false, false
|
||||
function __yazi_check_and_warn_deprecated_api()
|
||||
if not b1 then
|
||||
for _ in pairs(Manager) do
|
||||
b1 = true
|
||||
warn("Manager")
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
b = true
|
||||
for _ in pairs(Manager) do
|
||||
warn("Manager")
|
||||
break
|
||||
if not b2 then
|
||||
for _ in pairs(Folder) do
|
||||
b2 = true
|
||||
warn("Folder")
|
||||
break
|
||||
end
|
||||
end
|
||||
for _ in pairs(Folder) do
|
||||
warn("Folder")
|
||||
break
|
||||
end
|
||||
for _ in pairs(File) do
|
||||
warn("File")
|
||||
break
|
||||
|
||||
if not b3 then
|
||||
for _ in pairs(File) do
|
||||
b3 = true
|
||||
warn("File")
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -29,14 +29,17 @@ function Current:render()
|
|||
return self:empty()
|
||||
end
|
||||
|
||||
local items = {}
|
||||
local entities, linemodes = {}, {}
|
||||
for _, f in ipairs(files) do
|
||||
items[#items + 1] = ui.ListItem(Entity:render(f)):style(Entity:style(f))
|
||||
linemodes[#linemodes + 1] = Linemode:new(f):render()
|
||||
|
||||
local entity = Entity:new(f)
|
||||
entities[#entities + 1] = ui.ListItem(entity:render()):style(entity:style())
|
||||
end
|
||||
|
||||
return {
|
||||
ui.List(self._area, items),
|
||||
ui.Paragraph(self._area, Linemode:render(files)):align(ui.Paragraph.RIGHT),
|
||||
ui.List(self._area, entities),
|
||||
ui.Paragraph(self._area, linemodes):align(ui.Paragraph.RIGHT),
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,37 +1,35 @@
|
|||
Entity = {
|
||||
_inc = 1000,
|
||||
_children = {
|
||||
{ "icon", id = 1, order = 1000 },
|
||||
{ "prefix", id = 2, order = 2000 },
|
||||
{ "highlights", id = 3, order = 3000 },
|
||||
{ "found", id = 4, order = 4000 },
|
||||
{ "symlink", id = 5, order = 5000 },
|
||||
},
|
||||
}
|
||||
|
||||
function Entity:style(file)
|
||||
local style = file:style()
|
||||
if not file:is_hovered() then
|
||||
return style
|
||||
elseif file:in_preview() then
|
||||
return style and style:patch(THEME.manager.preview_hovered) or THEME.manager.preview_hovered
|
||||
else
|
||||
return style and style:patch(THEME.manager.hovered) or THEME.manager.hovered
|
||||
end
|
||||
end
|
||||
function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end
|
||||
|
||||
function Entity:icon(file)
|
||||
local icon = file:icon()
|
||||
function Entity:icon()
|
||||
local icon = self._file:icon()
|
||||
if not icon then
|
||||
return ui.Line("")
|
||||
elseif file:is_hovered() then
|
||||
elseif self._file:is_hovered() then
|
||||
return ui.Line(" " .. icon.text .. " ")
|
||||
else
|
||||
return ui.Line(" " .. icon.text .. " "):style(icon.style)
|
||||
end
|
||||
end
|
||||
|
||||
function Entity:prefix(file)
|
||||
local prefix = file:prefix() or ""
|
||||
function Entity:prefix()
|
||||
local prefix = self._file:prefix() or ""
|
||||
return ui.Line(prefix ~= "" and prefix .. "/" or "")
|
||||
end
|
||||
|
||||
function Entity:highlights(file)
|
||||
local name = file.name:gsub("\r", "?", 1)
|
||||
local highlights = file:highlights()
|
||||
function Entity:highlights()
|
||||
local name = self._file.name:gsub("\r", "?", 1)
|
||||
local highlights = self._file:highlights()
|
||||
if not highlights or #highlights == 0 then
|
||||
return ui.Line(name)
|
||||
end
|
||||
|
|
@ -50,12 +48,12 @@ function Entity:highlights(file)
|
|||
return ui.Line(spans)
|
||||
end
|
||||
|
||||
function Entity:found(file)
|
||||
if not file:is_hovered() then
|
||||
function Entity:found()
|
||||
if not self._file:is_hovered() then
|
||||
return ui.Line {}
|
||||
end
|
||||
|
||||
local found = file:found()
|
||||
local found = self._file:found()
|
||||
if not found then
|
||||
return ui.Line {}
|
||||
end
|
||||
|
|
@ -66,32 +64,35 @@ function Entity:found(file)
|
|||
}
|
||||
end
|
||||
|
||||
function Entity:symlink(file)
|
||||
function Entity:symlink()
|
||||
if not MANAGER.show_symlink then
|
||||
return ui.Line {}
|
||||
end
|
||||
|
||||
local to = file.link_to
|
||||
local to = self._file.link_to
|
||||
return ui.Line(to and { ui.Span(" -> " .. tostring(to)):italic() } or {})
|
||||
end
|
||||
|
||||
function Entity:render(file)
|
||||
function Entity:render()
|
||||
local lines = {}
|
||||
for _, child in ipairs(self._children) do
|
||||
lines[#lines + 1] = child[1](self, file)
|
||||
for _, c in ipairs(self._children) do
|
||||
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
|
||||
end
|
||||
return ui.Line(lines)
|
||||
end
|
||||
|
||||
-- Initialize children
|
||||
Entity._children = {
|
||||
{ Entity.icon, id = 1, order = 1000 },
|
||||
{ Entity.prefix, id = 2, order = 2000 },
|
||||
{ Entity.highlights, id = 3, order = 3000 },
|
||||
{ Entity.found, id = 4, order = 4000 },
|
||||
{ Entity.symlink, id = 5, order = 5000 },
|
||||
}
|
||||
function Entity:style()
|
||||
local s = self._file:style()
|
||||
if not self._file:is_hovered() then
|
||||
return s
|
||||
elseif self._file:in_preview() then
|
||||
return s and s:patch(THEME.manager.preview_hovered) or THEME.manager.preview_hovered
|
||||
else
|
||||
return s and s:patch(THEME.manager.hovered) or THEME.manager.hovered
|
||||
end
|
||||
end
|
||||
|
||||
-- Children
|
||||
function Entity:children_add(fn, order)
|
||||
self._inc = self._inc + 1
|
||||
self._children[#self._children + 1] = { fn, id = self._inc, order = order }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,16 @@
|
|||
Header = {
|
||||
LEFT = 0,
|
||||
RIGHT = 1,
|
||||
|
||||
_id = "header",
|
||||
_inc = 1000,
|
||||
_left = {
|
||||
{ "cwd", id = 1, order = 1000 },
|
||||
},
|
||||
_right = {
|
||||
{ "count", id = 1, order = 1000 },
|
||||
{ "tabs", id = 2, order = 2000 },
|
||||
},
|
||||
}
|
||||
|
||||
function Header:new(area, tab)
|
||||
|
|
@ -9,7 +20,12 @@ function Header:new(area, tab)
|
|||
}, { __index = self })
|
||||
end
|
||||
|
||||
function Header:cwd(max)
|
||||
function Header:cwd()
|
||||
local max = self._area.w - self._right_width
|
||||
if max <= 0 then
|
||||
return ui.Span("")
|
||||
end
|
||||
|
||||
local s = ya.readable_path(tostring(self._tab.current.cwd)) .. self:flags()
|
||||
return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd)
|
||||
end
|
||||
|
|
@ -75,8 +91,10 @@ function Header:tabs()
|
|||
end
|
||||
|
||||
function Header:render()
|
||||
local right = ui.Line { self:count(), self:tabs() }
|
||||
local left = ui.Line { self:cwd(math.max(0, self._area.w - right:width())) }
|
||||
local right = self:children_render(self.RIGHT)
|
||||
self._right_width = right:width()
|
||||
|
||||
local left = self:children_render(self.LEFT)
|
||||
return {
|
||||
ui.Paragraph(self._area, { left }),
|
||||
ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT),
|
||||
|
|
@ -89,3 +107,32 @@ function Header:click(event, up) end
|
|||
function Header:scroll(event, step) end
|
||||
|
||||
function Header:touch(event, step) end
|
||||
|
||||
-- Children
|
||||
function Header:children_add(fn, order, side)
|
||||
self._inc = self._inc + 1
|
||||
local children = side == self.RIGHT and self._right or self._left
|
||||
|
||||
children[#children + 1] = { fn, id = self._inc, order = order }
|
||||
table.sort(children, function(a, b) return a.order < b.order end)
|
||||
|
||||
return self._inc
|
||||
end
|
||||
|
||||
function Header:children_remove(id, side)
|
||||
local children = side == self.RIGHT and self._right or self._left
|
||||
for i, child in ipairs(children) do
|
||||
if child.id == id then
|
||||
table.remove(children, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Header:children_render(side)
|
||||
local lines = {}
|
||||
for _, c in ipairs(side == self.RIGHT and self._right or self._left) do
|
||||
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
|
||||
end
|
||||
return ui.Line(lines)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
Linemode = {
|
||||
_inc = 1000,
|
||||
_children = {
|
||||
{ "solo", id = 1, order = 1000 },
|
||||
},
|
||||
}
|
||||
|
||||
function Linemode:solo(file)
|
||||
function Linemode:new(file) return setmetatable({ _file = file }, { __index = self }) end
|
||||
|
||||
function Linemode:solo()
|
||||
local mode = cx.active.conf.linemode
|
||||
if mode == "none" or mode == "solo" then
|
||||
return ui.Line("")
|
||||
|
|
@ -14,18 +19,18 @@ function Linemode:solo(file)
|
|||
|
||||
return ui.Line {
|
||||
ui.Span(" "),
|
||||
self[mode](self, file),
|
||||
self[mode](self),
|
||||
ui.Span(" "),
|
||||
}
|
||||
end
|
||||
|
||||
function Linemode:size(file)
|
||||
local size = file:size()
|
||||
function Linemode:size()
|
||||
local size = self._file:size()
|
||||
return ui.Line(size and ya.readable_size(size) or "")
|
||||
end
|
||||
|
||||
function Linemode:ctime(file)
|
||||
local time = (file.cha.created or 0) // 1
|
||||
function Linemode:ctime()
|
||||
local time = (self._file.cha.created or 0) // 1
|
||||
if time == 0 then
|
||||
return ui.Line("")
|
||||
elseif os.date("%Y", time) == os.date("%Y") then
|
||||
|
|
@ -35,8 +40,8 @@ function Linemode:ctime(file)
|
|||
end
|
||||
end
|
||||
|
||||
function Linemode:mtime(file)
|
||||
local time = (file.cha.modified or 0) // 1
|
||||
function Linemode:mtime()
|
||||
local time = (self._file.cha.modified or 0) // 1
|
||||
if time == 0 then
|
||||
return ui.Line("")
|
||||
elseif os.date("%Y", time) == os.date("%Y") then
|
||||
|
|
@ -46,27 +51,23 @@ function Linemode:mtime(file)
|
|||
end
|
||||
end
|
||||
|
||||
function Linemode:permissions(file) return ui.Line(file.cha:permissions() or "") end
|
||||
function Linemode:permissions() return ui.Line(self._file.cha:permissions() or "") end
|
||||
|
||||
function Linemode:owner(file)
|
||||
local user = file.cha.uid and ya.user_name(file.cha.uid) or file.cha.uid
|
||||
local group = file.cha.gid and ya.group_name(file.cha.gid) or file.cha.gid
|
||||
function Linemode:owner()
|
||||
local user = self._file.cha.uid and ya.user_name(self._file.cha.uid) or self._file.cha.uid
|
||||
local group = self._file.cha.gid and ya.group_name(self._file.cha.gid) or self._file.cha.gid
|
||||
return ui.Line(string.format("%s:%s", user or "-", group or "-"))
|
||||
end
|
||||
|
||||
function Linemode:render(files)
|
||||
function Linemode:render()
|
||||
local lines = {}
|
||||
for _, f in ipairs(files) do
|
||||
lines[#lines + 1] = self:children_render(f)
|
||||
for _, c in ipairs(self._children) do
|
||||
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
|
||||
end
|
||||
return lines
|
||||
return ui.Line(lines)
|
||||
end
|
||||
|
||||
-- Initialize children
|
||||
Linemode._children = {
|
||||
{ Linemode.solo, id = 1, order = 1000 },
|
||||
}
|
||||
|
||||
-- Children
|
||||
function Linemode:children_add(fn, order)
|
||||
self._inc = self._inc + 1
|
||||
self._children[#self._children + 1] = { fn, id = self._inc, order = order }
|
||||
|
|
@ -82,11 +83,3 @@ function Linemode:children_remove(id)
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Linemode:children_render(file)
|
||||
local lines = {}
|
||||
for _, child in ipairs(self._children) do
|
||||
lines[#lines + 1] = child[1](self, file)
|
||||
end
|
||||
return ui.Line(lines)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ function Parent:render()
|
|||
|
||||
local items = {}
|
||||
for _, f in ipairs(self._folder.window) do
|
||||
items[#items + 1] = ui.ListItem(Entity:render(f)):style(Entity:style(f))
|
||||
local entity = Entity:new(f)
|
||||
items[#items + 1] = ui.ListItem(entity:render()):style(entity:style())
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,16 @@ Status = {
|
|||
|
||||
_id = "status",
|
||||
_inc = 1000,
|
||||
_left = {
|
||||
{ "mode", id = 1, order = 1000 },
|
||||
{ "size", id = 2, order = 2000 },
|
||||
{ "name", id = 3, order = 3000 },
|
||||
},
|
||||
_right = {
|
||||
{ "permissions", id = 4, order = 1000 },
|
||||
{ "percentage", id = 5, order = 2000 },
|
||||
{ "position", id = 6, order = 3000 },
|
||||
},
|
||||
}
|
||||
|
||||
function Status:new(area, tab)
|
||||
|
|
@ -138,18 +148,7 @@ function Status:scroll(event, step) end
|
|||
|
||||
function Status:touch(event, step) end
|
||||
|
||||
-- Initialize children
|
||||
Status._left = {
|
||||
{ Status.mode, id = 1, order = 1000 },
|
||||
{ Status.size, id = 2, order = 2000 },
|
||||
{ Status.name, id = 3, order = 3000 },
|
||||
}
|
||||
Status._right = {
|
||||
{ Status.permissions, id = 4, order = 1000 },
|
||||
{ Status.percentage, id = 5, order = 2000 },
|
||||
{ Status.position, id = 6, order = 3000 },
|
||||
}
|
||||
|
||||
-- Children
|
||||
function Status:children_add(fn, order, side)
|
||||
self._inc = self._inc + 1
|
||||
local children = side == self.RIGHT and self._right or self._left
|
||||
|
|
@ -172,8 +171,8 @@ end
|
|||
|
||||
function Status:children_render(side)
|
||||
local lines = {}
|
||||
for _, child in ipairs(side == self.RIGHT and self._right or self._left) do
|
||||
lines[#lines + 1] = child[1](self)
|
||||
for _, c in ipairs(side == self.RIGHT and self._right or self._left) do
|
||||
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
|
||||
end
|
||||
return ui.Line(lines)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ function M:peek()
|
|||
|
||||
local items = {}
|
||||
for _, f in ipairs(folder.window) do
|
||||
items[#items + 1] = ui.ListItem(Entity:render(f)):style(Entity:style(f))
|
||||
local entity = Entity:new(f)
|
||||
items[#items + 1] = ui.ListItem(entity:render()):style(entity:style())
|
||||
end
|
||||
|
||||
ya.preview_widgets(self, {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ pub fn peek_sync(cmd: &Cmd, file: yazi_shared::fs::File, skip: usize) {
|
|||
});
|
||||
|
||||
let cmd: Cmd =
|
||||
Opt { name: cmd.name.to_owned(), sync: true, cb: Some(cb), ..Default::default() }.into();
|
||||
Opt { id: cmd.name.to_owned(), sync: true, cb: Some(cb), ..Default::default() }.into();
|
||||
|
||||
emit!(Call(cmd.with_name("plugin"), Layer::App));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ pub fn seek_sync(cmd: &Cmd, file: yazi_shared::fs::File, units: i16) {
|
|||
});
|
||||
|
||||
let cmd: Cmd =
|
||||
Opt { name: cmd.name.to_owned(), sync: true, cb: Some(cb), ..Default::default() }.into();
|
||||
Opt { id: cmd.name.to_owned(), sync: true, cb: Some(cb), ..Default::default() }.into();
|
||||
|
||||
emit!(Call(cmd.with_name("plugin"), Layer::App));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,21 +52,20 @@ impl Loader {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(&self, name: &str) -> mlua::Result<Table> {
|
||||
pub fn load(&self, id: &str) -> mlua::Result<Table> {
|
||||
let globals = LUA.globals();
|
||||
let loaded: Table = globals.raw_get::<_, Table>("package")?.raw_get("loaded")?;
|
||||
if let Ok(t) = loaded.raw_get::<_, Table>(name) {
|
||||
if let Ok(t) = loaded.raw_get::<_, Table>(id) {
|
||||
return Ok(t);
|
||||
}
|
||||
|
||||
let t: Table = match self.read().get(name) {
|
||||
Some(b) => LUA.load(b.as_ref()).set_name(name).call(())?,
|
||||
None => Err(format!("plugin `{name}` not found").into_lua_err())?,
|
||||
let t: Table = match self.read().get(id) {
|
||||
Some(b) => LUA.load(b.as_ref()).set_name(id).call(())?,
|
||||
None => Err(format!("plugin `{id}` not found").into_lua_err())?,
|
||||
};
|
||||
|
||||
// TODO: rename to `_id`
|
||||
t.raw_set("_name", LUA.create_string(name)?)?;
|
||||
loaded.raw_set(name, t.clone())?;
|
||||
t.raw_set("_id", LUA.create_string(id)?)?;
|
||||
loaded.raw_set(id, t.clone())?;
|
||||
Ok(t)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ pub(super) type OptCallback = Box<dyn FnOnce(&Lua, Table) -> mlua::Result<()> +
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct Opt {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
pub sync: bool,
|
||||
pub args: Vec<Data>,
|
||||
pub cb: Option<OptCallback>,
|
||||
|
|
@ -16,8 +16,8 @@ impl TryFrom<Cmd> for Opt {
|
|||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
|
||||
let Some(name) = c.take_first_str().filter(|s| !s.is_empty()) else {
|
||||
bail!("plugin name cannot be empty");
|
||||
let Some(id) = c.take_first_str().filter(|s| !s.is_empty()) else {
|
||||
bail!("plugin id cannot be empty");
|
||||
};
|
||||
|
||||
let args = if let Some(s) = c.str("args") {
|
||||
|
|
@ -26,14 +26,14 @@ impl TryFrom<Cmd> for Opt {
|
|||
c.take_any::<Vec<Data>>("args").unwrap_or_default()
|
||||
};
|
||||
|
||||
Ok(Self { name, sync: c.bool("sync"), args, cb: c.take_any("callback") })
|
||||
Ok(Self { id, sync: c.bool("sync"), args, cb: c.take_any("callback") })
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Opt> for Cmd {
|
||||
fn from(value: Opt) -> Self {
|
||||
let mut cmd =
|
||||
Cmd::args("", vec![value.name]).with_bool("sync", value.sync).with_any("args", value.args);
|
||||
Cmd::args("", vec![value.id]).with_bool("sync", value.sync).with_any("args", value.args);
|
||||
|
||||
if let Some(cb) = value.cb {
|
||||
cmd = cmd.with_any("callback", cb);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue