feat: add children_add and children_remove methods to the Header component as well

This commit is contained in:
sxyazi 2024-07-15 14:57:39 +08:00
parent ec33085f67
commit c803241bd7
No known key found for this signature in database
8 changed files with 141 additions and 86 deletions

View file

@ -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"]}

View file

@ -4,37 +4,43 @@ Manager = {}
Folder = {} Folder = {}
File = {} File = {}
local b = false local function warn(name)
function __yazi_check_and_warn_deprecated_api() ya.notify {
if b then title = "Deprecated API",
return content = string.format(
end [[The `%s` global variable has been removed in Yazi v0.3, please remove it from your `init.lua`.
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`.
See https://github.com/sxyazi/yazi/pull/1257 for details.]], See https://github.com/sxyazi/yazi/pull/1257 for details.]],
name name
), ),
timeout = 20, timeout = 20,
level = "warn", 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 end
b = true if not b2 then
for _ in pairs(Manager) do for _ in pairs(Folder) do
warn("Manager") b2 = true
break warn("Folder")
break
end
end end
for _ in pairs(Folder) do
warn("Folder") if not b3 then
break for _ in pairs(File) do
end b3 = true
for _ in pairs(File) do warn("File")
warn("File") break
break end
end end
end end

View file

@ -29,14 +29,17 @@ function Current:render()
return self:empty() return self:empty()
end end
local items = {} local entities, linemodes = {}, {}
for _, f in ipairs(files) do 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 end
return { return {
ui.List(self._area, items), ui.List(self._area, entities),
ui.Paragraph(self._area, Linemode:render(files)):align(ui.Paragraph.RIGHT), ui.Paragraph(self._area, linemodes):align(ui.Paragraph.RIGHT),
} }
end end

View file

@ -2,36 +2,27 @@ Entity = {
_inc = 1000, _inc = 1000,
} }
function Entity:style(file) function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end
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:icon(file) function Entity:icon()
local icon = file:icon() local icon = self._file:icon()
if not icon then if not icon then
return ui.Line("") return ui.Line("")
elseif file:is_hovered() then elseif self._file:is_hovered() then
return ui.Line(" " .. icon.text .. " ") return ui.Line(" " .. icon.text .. " ")
else else
return ui.Line(" " .. icon.text .. " "):style(icon.style) return ui.Line(" " .. icon.text .. " "):style(icon.style)
end end
end end
function Entity:prefix(file) function Entity:prefix()
local prefix = file:prefix() or "" local prefix = self._file:prefix() or ""
return ui.Line(prefix ~= "" and prefix .. "/" or "") return ui.Line(prefix ~= "" and prefix .. "/" or "")
end end
function Entity:highlights(file) function Entity:highlights()
local name = file.name:gsub("\r", "?", 1) local name = self._file.name:gsub("\r", "?", 1)
local highlights = file:highlights() local highlights = self._file:highlights()
if not highlights or #highlights == 0 then if not highlights or #highlights == 0 then
return ui.Line(name) return ui.Line(name)
end end
@ -50,12 +41,12 @@ function Entity:highlights(file)
return ui.Line(spans) return ui.Line(spans)
end end
function Entity:found(file) function Entity:found()
if not file:is_hovered() then if not self._file:is_hovered() then
return ui.Line {} return ui.Line {}
end end
local found = file:found() local found = self._file:found()
if not found then if not found then
return ui.Line {} return ui.Line {}
end end
@ -66,23 +57,34 @@ function Entity:found(file)
} }
end end
function Entity:symlink(file) function Entity:symlink()
if not MANAGER.show_symlink then if not MANAGER.show_symlink then
return ui.Line {} return ui.Line {}
end end
local to = file.link_to local to = self._file.link_to
return ui.Line(to and { ui.Span(" -> " .. tostring(to)):italic() } or {}) return ui.Line(to and { ui.Span(" -> " .. tostring(to)):italic() } or {})
end end
function Entity:render(file) function Entity:render()
local lines = {} local lines = {}
for _, child in ipairs(self._children) do for _, child in ipairs(self._children) do
lines[#lines + 1] = child[1](self, file) lines[#lines + 1] = child[1](self)
end end
return ui.Line(lines) return ui.Line(lines)
end end
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
-- Initialize children -- Initialize children
Entity._children = { Entity._children = {
{ Entity.icon, id = 1, order = 1000 }, { Entity.icon, id = 1, order = 1000 },

View file

@ -1,5 +1,9 @@
Header = { Header = {
LEFT = 0,
RIGHT = 1,
_id = "header", _id = "header",
_inc = 1000,
} }
function Header:new(area, tab) function Header:new(area, tab)
@ -9,7 +13,12 @@ function Header:new(area, tab)
}, { __index = self }) }, { __index = self })
end 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() 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) return ui.Span(ya.truncate(s, { max = max, rtl = true })):style(THEME.manager.cwd)
end end
@ -75,8 +84,10 @@ function Header:tabs()
end end
function Header:render() function Header:render()
local right = ui.Line { self:count(), self:tabs() } local right = self:children_render(self.RIGHT)
local left = ui.Line { self:cwd(math.max(0, self._area.w - right:width())) } self._right_width = right:width()
local left = self:children_render(self.LEFT)
return { return {
ui.Paragraph(self._area, { left }), ui.Paragraph(self._area, { left }),
ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT), ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT),
@ -89,3 +100,40 @@ function Header:click(event, up) end
function Header:scroll(event, step) end function Header:scroll(event, step) end
function Header:touch(event, step) end function Header:touch(event, step) end
-- Initialize children
Header._left = {
{ Header.cwd, id = 1, order = 1000 },
}
Header._right = {
{ Header.count, id = 1, order = 1000 },
{ Header.tabs, id = 2, order = 2000 },
}
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 _, child in ipairs(side == self.RIGHT and self._right or self._left) do
lines[#lines + 1] = child[1](self)
end
return ui.Line(lines)
end

View file

@ -2,7 +2,9 @@ Linemode = {
_inc = 1000, _inc = 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 local mode = cx.active.conf.linemode
if mode == "none" or mode == "solo" then if mode == "none" or mode == "solo" then
return ui.Line("") return ui.Line("")
@ -14,18 +16,18 @@ function Linemode:solo(file)
return ui.Line { return ui.Line {
ui.Span(" "), ui.Span(" "),
self[mode](self, file), self[mode](self),
ui.Span(" "), ui.Span(" "),
} }
end end
function Linemode:size(file) function Linemode:size()
local size = file:size() local size = self._file:size()
return ui.Line(size and ya.readable_size(size) or "") return ui.Line(size and ya.readable_size(size) or "")
end end
function Linemode:ctime(file) function Linemode:ctime()
local time = (file.cha.created or 0) // 1 local time = (self._file.cha.created or 0) // 1
if time == 0 then if time == 0 then
return ui.Line("") return ui.Line("")
elseif os.date("%Y", time) == os.date("%Y") then elseif os.date("%Y", time) == os.date("%Y") then
@ -35,8 +37,8 @@ function Linemode:ctime(file)
end end
end end
function Linemode:mtime(file) function Linemode:mtime()
local time = (file.cha.modified or 0) // 1 local time = (self._file.cha.modified or 0) // 1
if time == 0 then if time == 0 then
return ui.Line("") return ui.Line("")
elseif os.date("%Y", time) == os.date("%Y") then elseif os.date("%Y", time) == os.date("%Y") then
@ -46,20 +48,20 @@ function Linemode:mtime(file)
end end
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) function Linemode:owner()
local user = file.cha.uid and ya.user_name(file.cha.uid) or file.cha.uid local user = self._file.cha.uid and ya.user_name(self._file.cha.uid) or self._file.cha.uid
local group = file.cha.gid and ya.group_name(file.cha.gid) or file.cha.gid 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 "-")) return ui.Line(string.format("%s:%s", user or "-", group or "-"))
end end
function Linemode:render(files) function Linemode:render()
local lines = {} local lines = {}
for _, f in ipairs(files) do for _, child in ipairs(self._children) do
lines[#lines + 1] = self:children_render(f) lines[#lines + 1] = child[1](self)
end end
return lines return ui.Line(lines)
end end
-- Initialize children -- Initialize children
@ -82,11 +84,3 @@ function Linemode:children_remove(id)
end end
end 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

View file

@ -17,7 +17,8 @@ function Parent:render()
local items = {} local items = {}
for _, f in ipairs(self._folder.window) do 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 end
return { return {

View file

@ -20,7 +20,8 @@ function M:peek()
local items = {} local items = {}
for _, f in ipairs(folder.window) do 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 end
ya.preview_widgets(self, { ya.preview_widgets(self, {