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 16, 2024
1 parent ec33085 commit ed6ae00
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 134 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
69 changes: 35 additions & 34 deletions yazi-plugin/preset/components/entity.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 }
Expand Down
53 changes: 50 additions & 3 deletions yazi-plugin/preset/components/header.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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
Loading

0 comments on commit ed6ae00

Please sign in to comment.