Skip to content

Commit 4eec20b

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

File tree

13 files changed

+160
-106
lines changed

13 files changed

+160
-106
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

+27-25
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,27 @@ Entity = {
22
_inc = 1000,
33
}
44

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
5+
function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end
156

16-
function Entity:icon(file)
17-
local icon = file:icon()
7+
function Entity:icon()
8+
local icon = self._file:icon()
189
if not icon then
1910
return ui.Line("")
20-
elseif file:is_hovered() then
11+
elseif self._file:is_hovered() then
2112
return ui.Line(" " .. icon.text .. " ")
2213
else
2314
return ui.Line(" " .. icon.text .. " "):style(icon.style)
2415
end
2516
end
2617

27-
function Entity:prefix(file)
28-
local prefix = file:prefix() or ""
18+
function Entity:prefix()
19+
local prefix = self._file:prefix() or ""
2920
return ui.Line(prefix ~= "" and prefix .. "/" or "")
3021
end
3122

32-
function Entity:highlights(file)
33-
local name = file.name:gsub("\r", "?", 1)
34-
local highlights = file:highlights()
23+
function Entity:highlights()
24+
local name = self._file.name:gsub("\r", "?", 1)
25+
local highlights = self._file:highlights()
3526
if not highlights or #highlights == 0 then
3627
return ui.Line(name)
3728
end
@@ -50,12 +41,12 @@ function Entity:highlights(file)
5041
return ui.Line(spans)
5142
end
5243

53-
function Entity:found(file)
54-
if not file:is_hovered() then
44+
function Entity:found()
45+
if not self._file:is_hovered() then
5546
return ui.Line {}
5647
end
5748

58-
local found = file:found()
49+
local found = self._file:found()
5950
if not found then
6051
return ui.Line {}
6152
end
@@ -66,23 +57,34 @@ function Entity:found(file)
6657
}
6758
end
6859

69-
function Entity:symlink(file)
60+
function Entity:symlink()
7061
if not MANAGER.show_symlink then
7162
return ui.Line {}
7263
end
7364

74-
local to = file.link_to
65+
local to = self._file.link_to
7566
return ui.Line(to and { ui.Span(" -> " .. tostring(to)):italic() } or {})
7667
end
7768

78-
function Entity:render(file)
69+
function Entity:render()
7970
local lines = {}
8071
for _, child in ipairs(self._children) do
81-
lines[#lines + 1] = child[1](self, file)
72+
lines[#lines + 1] = child[1](self)
8273
end
8374
return ui.Line(lines)
8475
end
8576

77+
function Entity:style()
78+
local s = self._file:style()
79+
if not self._file:is_hovered() then
80+
return s
81+
elseif self._file:in_preview() then
82+
return s and s:patch(THEME.manager.preview_hovered) or THEME.manager.preview_hovered
83+
else
84+
return s and s:patch(THEME.manager.hovered) or THEME.manager.hovered
85+
end
86+
end
87+
8688
-- Initialize children
8789
Entity._children = {
8890
{ Entity.icon, id = 1, order = 1000 },

yazi-plugin/preset/components/header.lua

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Header = {
2+
LEFT = 0,
3+
RIGHT = 1,
4+
25
_id = "header",
6+
_inc = 1000,
37
}
48

59
function Header:new(area, tab)
@@ -9,7 +13,12 @@ function Header:new(area, tab)
913
}, { __index = self })
1014
end
1115

12-
function Header:cwd(max)
16+
function Header:cwd()
17+
local max = self._area.w - self._right_width
18+
if max <= 0 then
19+
return ui.Span("")
20+
end
21+
1322
local s = ya.readable_path(tostring(self._tab.current.cwd)) .. self:flags()
1423
return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd)
1524
end
@@ -75,8 +84,10 @@ function Header:tabs()
7584
end
7685

7786
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())) }
87+
local right = self:children_render(self.RIGHT)
88+
self._right_width = right:width()
89+
90+
local left = self:children_render(self.LEFT)
8091
return {
8192
ui.Paragraph(self._area, { left }),
8293
ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT),
@@ -89,3 +100,40 @@ function Header:click(event, up) end
89100
function Header:scroll(event, step) end
90101

91102
function Header:touch(event, step) end
103+
104+
-- Initialize children
105+
Header._left = {
106+
{ Header.cwd, id = 1, order = 1000 },
107+
}
108+
Header._right = {
109+
{ Header.count, id = 1, order = 1000 },
110+
{ Header.tabs, id = 2, order = 2000 },
111+
}
112+
113+
function Header:children_add(fn, order, side)
114+
self._inc = self._inc + 1
115+
local children = side == self.RIGHT and self._right or self._left
116+
117+
children[#children + 1] = { fn, id = self._inc, order = order }
118+
table.sort(children, function(a, b) return a.order < b.order end)
119+
120+
return self._inc
121+
end
122+
123+
function Header:children_remove(id, side)
124+
local children = side == self.RIGHT and self._right or self._left
125+
for i, child in ipairs(children) do
126+
if child.id == id then
127+
table.remove(children, i)
128+
break
129+
end
130+
end
131+
end
132+
133+
function Header:children_render(side)
134+
local lines = {}
135+
for _, child in ipairs(side == self.RIGHT and self._right or self._left) do
136+
lines[#lines + 1] = child[1](self)
137+
end
138+
return ui.Line(lines)
139+
end

yazi-plugin/preset/components/linemode.lua

+18-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ Linemode = {
22
_inc = 1000,
33
}
44

5-
function Linemode:solo(file)
5+
function Linemode:new(file) return setmetatable({ _file = file }, { __index = self }) end
6+
7+
function Linemode:solo()
68
local mode = cx.active.conf.linemode
79
if mode == "none" or mode == "solo" then
810
return ui.Line("")
@@ -14,18 +16,18 @@ function Linemode:solo(file)
1416

1517
return ui.Line {
1618
ui.Span(" "),
17-
self[mode](self, file),
19+
self[mode](self),
1820
ui.Span(" "),
1921
}
2022
end
2123

22-
function Linemode:size(file)
23-
local size = file:size()
24+
function Linemode:size()
25+
local size = self._file:size()
2426
return ui.Line(size and ya.readable_size(size) or "")
2527
end
2628

27-
function Linemode:ctime(file)
28-
local time = (file.cha.created or 0) // 1
29+
function Linemode:ctime()
30+
local time = (self._file.cha.created or 0) // 1
2931
if time == 0 then
3032
return ui.Line("")
3133
elseif os.date("%Y", time) == os.date("%Y") then
@@ -35,8 +37,8 @@ function Linemode:ctime(file)
3537
end
3638
end
3739

38-
function Linemode:mtime(file)
39-
local time = (file.cha.modified or 0) // 1
40+
function Linemode:mtime()
41+
local time = (self._file.cha.modified or 0) // 1
4042
if time == 0 then
4143
return ui.Line("")
4244
elseif os.date("%Y", time) == os.date("%Y") then
@@ -46,20 +48,20 @@ function Linemode:mtime(file)
4648
end
4749
end
4850

49-
function Linemode:permissions(file) return ui.Line(file.cha:permissions() or "") end
51+
function Linemode:permissions() return ui.Line(self._file.cha:permissions() or "") end
5052

51-
function Linemode:owner(file)
52-
local user = file.cha.uid and ya.user_name(file.cha.uid) or file.cha.uid
53-
local group = file.cha.gid and ya.group_name(file.cha.gid) or file.cha.gid
53+
function Linemode:owner()
54+
local user = self._file.cha.uid and ya.user_name(self._file.cha.uid) or self._file.cha.uid
55+
local group = self._file.cha.gid and ya.group_name(self._file.cha.gid) or self._file.cha.gid
5456
return ui.Line(string.format("%s:%s", user or "-", group or "-"))
5557
end
5658

57-
function Linemode:render(files)
59+
function Linemode:render()
5860
local lines = {}
59-
for _, f in ipairs(files) do
60-
lines[#lines + 1] = self:children_render(f)
61+
for _, child in ipairs(self._children) do
62+
lines[#lines + 1] = child[1](self)
6163
end
62-
return lines
64+
return ui.Line(lines)
6365
end
6466

6567
-- Initialize children
@@ -82,11 +84,3 @@ function Linemode:children_remove(id)
8284
end
8385
end
8486
end
85-
86-
function Linemode:children_render(file)
87-
local lines = {}
88-
for _, child in ipairs(self._children) do
89-
lines[#lines + 1] = child[1](self, file)
90-
end
91-
return ui.Line(lines)
92-
end

0 commit comments

Comments
 (0)