Skip to content

Commit

Permalink
feat: add children_add and children_remove methods to the `Header…
Browse files Browse the repository at this point in the history
…` component as well (#1297)
  • Loading branch information
sxyazi committed Jul 15, 2024
1 parent ec33085 commit 4eec20b
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 106 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -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"]}
10 changes: 5 additions & 5 deletions yazi-fm/src/app/commands/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand All @@ -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}"),
};
Expand Down
60 changes: 33 additions & 27 deletions yazi-plugin/preset/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
end
name
),
timeout = 20,
level = "warn",
}
end

b = true
for _ in pairs(Manager) do
warn("Manager")
break
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
for _ in pairs(Folder) do
warn("Folder")
break

if not b2 then
for _ in pairs(Folder) do
b2 = true
warn("Folder")
break
end
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
11 changes: 7 additions & 4 deletions yazi-plugin/preset/components/current.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 27 additions & 25 deletions yazi-plugin/preset/components/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,27 @@ Entity = {
_inc = 1000,
}

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
Expand All @@ -50,12 +41,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
Expand All @@ -66,23 +57,34 @@ 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)
lines[#lines + 1] = child[1](self)
end
return ui.Line(lines)
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
Entity._children = {
{ Entity.icon, id = 1, order = 1000 },
Expand Down
54 changes: 51 additions & 3 deletions yazi-plugin/preset/components/header.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Header = {
LEFT = 0,
RIGHT = 1,

_id = "header",
_inc = 1000,
}

function Header:new(area, tab)
Expand All @@ -9,7 +13,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
Expand Down Expand Up @@ -75,8 +84,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),
Expand All @@ -89,3 +100,40 @@ function Header:click(event, up) end
function Header:scroll(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
42 changes: 18 additions & 24 deletions yazi-plugin/preset/components/linemode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ Linemode = {
_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
if mode == "none" or mode == "solo" then
return ui.Line("")
Expand All @@ -14,18 +16,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
Expand All @@ -35,8 +37,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
Expand All @@ -46,20 +48,20 @@ 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 _, child in ipairs(self._children) do
lines[#lines + 1] = child[1](self)
end
return lines
return ui.Line(lines)
end

-- Initialize children
Expand All @@ -82,11 +84,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
Loading

0 comments on commit 4eec20b

Please sign in to comment.