diff --git a/yazi-plugin/preset/components/entity.lua b/yazi-plugin/preset/components/entity.lua index a0a6dfc6..0cf2e951 100644 --- a/yazi-plugin/preset/components/entity.lua +++ b/yazi-plugin/preset/components/entity.lua @@ -1,24 +1,27 @@ 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 }, + { "space", id = 1, order = 1000 }, + { "icon", id = 2, order = 2000 }, + { "prefix", id = 3, order = 3000 }, + { "highlights", id = 4, order = 4000 }, + { "found", id = 5, order = 5000 }, + { "symlink", id = 6, order = 6000 }, }, } function Entity:new(file) return setmetatable({ _file = file }, { __index = self }) end +function Entity:space() return ui.Line(" ") end + function Entity:icon() local icon = self._file:icon() if not icon then return ui.Line("") elseif self._file:is_hovered() then - return ui.Line(" " .. icon.text .. " ") + return ui.Line(icon.text .. " ") else - return ui.Line(" " .. icon.text .. " "):style(icon.style) + return ui.Line(icon.text .. " "):style(icon.style) end end diff --git a/yazi-plugin/preset/components/linemode.lua b/yazi-plugin/preset/components/linemode.lua index 2a02a8c2..ecdf6c9b 100644 --- a/yazi-plugin/preset/components/linemode.lua +++ b/yazi-plugin/preset/components/linemode.lua @@ -2,26 +2,24 @@ Linemode = { _inc = 1000, _children = { { "solo", id = 1, order = 1000 }, + { "space", id = 2, order = 2000 }, }, } function Linemode:new(file) return setmetatable({ _file = file }, { __index = self }) end +function Linemode:space() return ui.Line(" ") end + function Linemode:solo() local mode = cx.active.conf.linemode if mode == "none" or mode == "solo" then return ui.Line("") + elseif not self[mode] then + return ui.Line(" " .. mode) + else + local line = self[mode](self) + return line:visible() and ui.Line { ui.Span(" "), line } or line end - - if not self[mode] then - return ui.Line(" " .. mode .. " ") - end - - return ui.Line { - ui.Span(" "), - self[mode](self), - ui.Span(" "), - } end function Linemode:size() diff --git a/yazi-plugin/src/elements/line.rs b/yazi-plugin/src/elements/line.rs index 16a76562..cfec1502 100644 --- a/yazi-plugin/src/elements/line.rs +++ b/yazi-plugin/src/elements/line.rs @@ -2,6 +2,7 @@ use std::mem; use ansi_to_tui::IntoText; use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, Table, UserData, UserDataMethods, Value}; +use unicode_width::UnicodeWidthChar; use super::{Span, Style}; @@ -84,7 +85,7 @@ impl UserData for Line { fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { crate::impl_style_shorthands!(methods, 0.style); - methods.add_function("width", |_, ud: AnyUserData| Ok(ud.borrow_mut::()?.0.width())); + methods.add_method("width", |_, me, ()| Ok(me.0.width())); methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| { { let mut me = ud.borrow_mut::()?; @@ -105,5 +106,8 @@ impl UserData for Line { }); Ok(ud) }); + methods.add_method("visible", |_, me, ()| { + Ok(me.0.iter().flat_map(|s| s.content.chars()).any(|c| c.width().unwrap_or(0) > 0)) + }); } } diff --git a/yazi-plugin/src/elements/span.rs b/yazi-plugin/src/elements/span.rs index 9464d0ba..e1bb24ba 100644 --- a/yazi-plugin/src/elements/span.rs +++ b/yazi-plugin/src/elements/span.rs @@ -1,4 +1,5 @@ use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, UserDataMethods, Value}; +use unicode_width::UnicodeWidthChar; use super::Style; @@ -29,5 +30,8 @@ impl UserData for Span { }; Ok(ud) }); + methods.add_method("visible", |_, me, ()| { + Ok(me.0.content.chars().any(|c| c.width().unwrap_or(0) > 0)) + }); } }