feat: ensure that Entity and Linemode have consistent space padding (#1580)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
Solitude 2024-09-01 05:54:04 +03:00 committed by sxyazi
parent 907ac09e34
commit fdd574997b
No known key found for this signature in database
4 changed files with 27 additions and 18 deletions

View file

@ -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

View file

@ -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()

View file

@ -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::<Self>()?.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::<Self>()?;
@ -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))
});
}
}

View file

@ -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))
});
}
}