Skip to content

Commit ed6ae00

Browse files
committed
feat: add children_add and children_remove methods to the Header component as well (#1297)
1 parent ec33085 commit ed6ae00

File tree

14 files changed

+184
-134
lines changed

14 files changed

+184
-134
lines changed

cspell.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +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":[]}
1+
{"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"]}

yazi-fm/src/app/commands/plugin.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ impl App {
1717
};
1818

1919
if !opt.sync {
20-
return self.cx.tasks.plugin_micro(opt.name, opt.args);
20+
return self.cx.tasks.plugin_micro(opt.id, opt.args);
2121
}
2222

23-
if LOADER.read().contains_key(&opt.name) {
23+
if LOADER.read().contains_key(&opt.id) {
2424
return self.plugin_do(opt);
2525
}
2626

2727
tokio::spawn(async move {
28-
if LOADER.ensure(&opt.name).await.is_ok() {
28+
if LOADER.ensure(&opt.id).await.is_ok() {
2929
Self::_plugin_do(opt);
3030
}
3131
});
@@ -44,12 +44,12 @@ impl App {
4444
};
4545

4646
match LUA.named_registry_value::<RtRef>("rt") {
47-
Ok(mut r) => r.swap(&opt.name),
47+
Ok(mut r) => r.swap(&opt.id),
4848
Err(e) => return warn!("{e}"),
4949
}
5050

5151
defer! { LUA.named_registry_value::<RtRef>("rt").map(|mut r| r.reset()).ok(); };
52-
let plugin = match LOADER.load(&opt.name) {
52+
let plugin = match LOADER.load(&opt.id) {
5353
Ok(plugin) => plugin,
5454
Err(e) => return warn!("{e}"),
5555
};

yazi-plugin/preset/compat.lua

+33-27
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,43 @@ Manager = {}
44
Folder = {}
55
File = {}
66

7-
local b = false
8-
function __yazi_check_and_warn_deprecated_api()
9-
if b then
10-
return
11-
end
12-
13-
local warn = function(name)
14-
ya.notify {
15-
title = "Deprecated API",
16-
content = string.format(
17-
[[The `%s` global variable has been removed in Yazi v0.3, please remove it from your `init.lua`.
7+
local function warn(name)
8+
ya.notify {
9+
title = "Deprecated API",
10+
content = string.format(
11+
[[The `%s` global variable has been removed in Yazi v0.3, please remove it from your `init.lua`.
1812
1913
See https://github.com/sxyazi/yazi/pull/1257 for details.]],
20-
name
21-
),
22-
timeout = 20,
23-
level = "warn",
24-
}
25-
end
14+
name
15+
),
16+
timeout = 20,
17+
level = "warn",
18+
}
19+
end
2620

27-
b = true
28-
for _ in pairs(Manager) do
29-
warn("Manager")
30-
break
21+
local b1, b2, b3 = false, false, false
22+
function __yazi_check_and_warn_deprecated_api()
23+
if not b1 then
24+
for _ in pairs(Manager) do
25+
b1 = true
26+
warn("Manager")
27+
break
28+
end
3129
end
32-
for _ in pairs(Folder) do
33-
warn("Folder")
34-
break
30+
31+
if not b2 then
32+
for _ in pairs(Folder) do
33+
b2 = true
34+
warn("Folder")
35+
break
36+
end
3537
end
36-
for _ in pairs(File) do
37-
warn("File")
38-
break
38+
39+
if not b3 then
40+
for _ in pairs(File) do
41+
b3 = true
42+
warn("File")
43+
break
44+
end
3945
end
4046
end

yazi-plugin/preset/components/current.lua

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ function Current:render()
2929
return self:empty()
3030
end
3131

32-
local items = {}
32+
local entities, linemodes = {}, {}
3333
for _, f in ipairs(files) do
34-
items[#items + 1] = ui.ListItem(Entity:render(f)):style(Entity:style(f))
34+
linemodes[#linemodes + 1] = Linemode:new(f):render()
35+
36+
local entity = Entity:new(f)
37+
entities[#entities + 1] = ui.ListItem(entity:render()):style(entity:style())
3538
end
3639

3740
return {
38-
ui.List(self._area, items),
39-
ui.Paragraph(self._area, Linemode:render(files)):align(ui.Paragraph.RIGHT),
41+
ui.List(self._area, entities),
42+
ui.Paragraph(self._area, linemodes):align(ui.Paragraph.RIGHT),
4043
}
4144
end
4245

yazi-plugin/preset/components/entity.lua

+35-34
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
Entity = {
22
_inc = 1000,
3+
_children = {
4+
{ "icon", id = 1, order = 1000 },
5+
{ "prefix", id = 2, order = 2000 },
6+
{ "highlights", id = 3, order = 3000 },
7+
{ "found", id = 4, order = 4000 },
8+
{ "symlink", id = 5, order = 5000 },
9+
},
310
}
411

5-
function Entity:style(file)
6-
local style = file:style()
7-
if not file:is_hovered() then
8-
return style
9-
elseif file:in_preview() then
10-
return style and style:patch(THEME.manager.preview_hovered) or THEME.manager.preview_hovered
11-
else
12-
return style and style:patch(THEME.manager.hovered) or THEME.manager.hovered
13-
end
14-
end
12+
function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end
1513

16-
function Entity:icon(file)
17-
local icon = file:icon()
14+
function Entity:icon()
15+
local icon = self._file:icon()
1816
if not icon then
1917
return ui.Line("")
20-
elseif file:is_hovered() then
18+
elseif self._file:is_hovered() then
2119
return ui.Line(" " .. icon.text .. " ")
2220
else
2321
return ui.Line(" " .. icon.text .. " "):style(icon.style)
2422
end
2523
end
2624

27-
function Entity:prefix(file)
28-
local prefix = file:prefix() or ""
25+
function Entity:prefix()
26+
local prefix = self._file:prefix() or ""
2927
return ui.Line(prefix ~= "" and prefix .. "/" or "")
3028
end
3129

32-
function Entity:highlights(file)
33-
local name = file.name:gsub("\r", "?", 1)
34-
local highlights = file:highlights()
30+
function Entity:highlights()
31+
local name = self._file.name:gsub("\r", "?", 1)
32+
local highlights = self._file:highlights()
3533
if not highlights or #highlights == 0 then
3634
return ui.Line(name)
3735
end
@@ -50,12 +48,12 @@ function Entity:highlights(file)
5048
return ui.Line(spans)
5149
end
5250

53-
function Entity:found(file)
54-
if not file:is_hovered() then
51+
function Entity:found()
52+
if not self._file:is_hovered() then
5553
return ui.Line {}
5654
end
5755

58-
local found = file:found()
56+
local found = self._file:found()
5957
if not found then
6058
return ui.Line {}
6159
end
@@ -66,32 +64,35 @@ function Entity:found(file)
6664
}
6765
end
6866

69-
function Entity:symlink(file)
67+
function Entity:symlink()
7068
if not MANAGER.show_symlink then
7169
return ui.Line {}
7270
end
7371

74-
local to = file.link_to
72+
local to = self._file.link_to
7573
return ui.Line(to and { ui.Span(" -> " .. tostring(to)):italic() } or {})
7674
end
7775

78-
function Entity:render(file)
76+
function Entity:render()
7977
local lines = {}
80-
for _, child in ipairs(self._children) do
81-
lines[#lines + 1] = child[1](self, file)
78+
for _, c in ipairs(self._children) do
79+
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
8280
end
8381
return ui.Line(lines)
8482
end
8583

86-
-- Initialize children
87-
Entity._children = {
88-
{ Entity.icon, id = 1, order = 1000 },
89-
{ Entity.prefix, id = 2, order = 2000 },
90-
{ Entity.highlights, id = 3, order = 3000 },
91-
{ Entity.found, id = 4, order = 4000 },
92-
{ Entity.symlink, id = 5, order = 5000 },
93-
}
84+
function Entity:style()
85+
local s = self._file:style()
86+
if not self._file:is_hovered() then
87+
return s
88+
elseif self._file:in_preview() then
89+
return s and s:patch(THEME.manager.preview_hovered) or THEME.manager.preview_hovered
90+
else
91+
return s and s:patch(THEME.manager.hovered) or THEME.manager.hovered
92+
end
93+
end
9494

95+
-- Children
9596
function Entity:children_add(fn, order)
9697
self._inc = self._inc + 1
9798
self._children[#self._children + 1] = { fn, id = self._inc, order = order }

yazi-plugin/preset/components/header.lua

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
Header = {
2+
LEFT = 0,
3+
RIGHT = 1,
4+
25
_id = "header",
6+
_inc = 1000,
7+
_left = {
8+
{ "cwd", id = 1, order = 1000 },
9+
},
10+
_right = {
11+
{ "count", id = 1, order = 1000 },
12+
{ "tabs", id = 2, order = 2000 },
13+
},
314
}
415

516
function Header:new(area, tab)
@@ -9,7 +20,12 @@ function Header:new(area, tab)
920
}, { __index = self })
1021
end
1122

12-
function Header:cwd(max)
23+
function Header:cwd()
24+
local max = self._area.w - self._right_width
25+
if max <= 0 then
26+
return ui.Span("")
27+
end
28+
1329
local s = ya.readable_path(tostring(self._tab.current.cwd)) .. self:flags()
1430
return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd)
1531
end
@@ -75,8 +91,10 @@ function Header:tabs()
7591
end
7692

7793
function Header:render()
78-
local right = ui.Line { self:count(), self:tabs() }
79-
local left = ui.Line { self:cwd(math.max(0, self._area.w - right:width())) }
94+
local right = self:children_render(self.RIGHT)
95+
self._right_width = right:width()
96+
97+
local left = self:children_render(self.LEFT)
8098
return {
8199
ui.Paragraph(self._area, { left }),
82100
ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT),
@@ -89,3 +107,32 @@ function Header:click(event, up) end
89107
function Header:scroll(event, step) end
90108

91109
function Header:touch(event, step) end
110+
111+
-- Children
112+
function Header:children_add(fn, order, side)
113+
self._inc = self._inc + 1
114+
local children = side == self.RIGHT and self._right or self._left
115+
116+
children[#children + 1] = { fn, id = self._inc, order = order }
117+
table.sort(children, function(a, b) return a.order < b.order end)
118+
119+
return self._inc
120+
end
121+
122+
function Header:children_remove(id, side)
123+
local children = side == self.RIGHT and self._right or self._left
124+
for i, child in ipairs(children) do
125+
if child.id == id then
126+
table.remove(children, i)
127+
break
128+
end
129+
end
130+
end
131+
132+
function Header:children_render(side)
133+
local lines = {}
134+
for _, c in ipairs(side == self.RIGHT and self._right or self._left) do
135+
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
136+
end
137+
return ui.Line(lines)
138+
end

0 commit comments

Comments
 (0)