Skip to content

Commit b6ae5d3

Browse files
authored
feat: truncate long paths in archive preview file list (#2778)
1 parent 70e459a commit b6ae5d3

File tree

22 files changed

+258
-88
lines changed

22 files changed

+258
-88
lines changed

yazi-plugin/preset/components/entity.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ end
7979
function Entity:redraw()
8080
local lines = {}
8181
for _, c in ipairs(self._children) do
82-
local line = ui.Line((type(c[1]) == "string" and self[c[1]] or c[1])(self))
83-
c.width, lines[#lines + 1] = line:width(), line
82+
local line = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
83+
c.width, lines[#lines + 1] = ui.width(line), line
8484
end
8585
return ui.Line(lines):style(self:style())
8686
end
@@ -101,12 +101,11 @@ function Entity:ellipsis(max)
101101
for _, child in ipairs(self._children) do
102102
adv = adv + child.width
103103
if adv >= max then
104-
return not f.cha.is_dir and f.url.ext and "…." .. f.url.ext or ""
104+
return not f.cha.is_dir and f.url.ext and "…." .. f.url.ext or nil
105105
elseif child.id == 4 then
106106
break
107107
end
108108
end
109-
return ""
110109
end
111110

112111
-- Children

yazi-plugin/preset/plugins/archive.lua

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local M = {}
22

33
function M:peek(job)
44
local limit = job.area.h
5-
local paths, sizes = {}, {}
65

76
local files, bound, code = self.list_files({ "-p", tostring(job.file.url) }, job.skip, limit)
87
if code ~= 0 then
@@ -13,31 +12,40 @@ function M:peek(job)
1312
)
1413
end
1514

15+
local left, right = {}, {}
1616
for _, f in ipairs(files) do
1717
local icon = File({
1818
url = Url(f.path),
1919
cha = Cha { kind = f.attr:sub(1, 1) == "D" and 1 or 0 },
2020
}):icon()
2121

22-
if icon then
23-
paths[#paths + 1] = ui.Line { ui.Span(" " .. icon.text .. " "):style(icon.style), f.path }
22+
if f.size > 0 then
23+
right[#right + 1] = string.format(" %s ", ya.readable_size(f.size))
2424
else
25-
paths[#paths + 1] = f.path
25+
right[#right + 1] = " "
2626
end
2727

28-
if f.size > 0 then
29-
sizes[#sizes + 1] = string.format(" %s ", ya.readable_size(f.size))
28+
if icon then
29+
left[#left + 1] = ui.Span(" " .. icon.text .. " "):style(icon.style)
3030
else
31-
sizes[#sizes + 1] = ""
31+
left[#left + 1] = " "
3232
end
33+
34+
left[#left] = ui.Line {
35+
left[#left],
36+
ya.truncate(f.path, {
37+
rtl = true,
38+
max = math.max(0, job.area.w - ui.width(left[#left]) - ui.width(right[#right])),
39+
}),
40+
}
3341
end
3442

3543
if job.skip > 0 and bound < job.skip + limit then
3644
ya.emit("peek", { math.max(0, bound - limit), only_if = job.file.url, upper_bound = true })
3745
else
3846
ya.preview_widget(job, {
39-
ui.Text(paths):area(job.area),
40-
ui.Text(sizes):area(job.area):align(ui.Text.RIGHT),
47+
ui.Text(left):area(job.area),
48+
ui.Text(right):area(job.area):align(ui.Text.RIGHT),
4149
})
4250
end
4351
end

yazi-plugin/src/elements/bar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mlua::{AnyUserData, Lua, MetaMethod, Table, UserData};
1+
use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, UserData, Value};
22
use ratatui::widgets::Borders;
33

44
use super::Area;
@@ -13,7 +13,7 @@ pub struct Bar {
1313
}
1414

1515
impl Bar {
16-
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
16+
pub fn compose(lua: &Lua) -> mlua::Result<Value> {
1717
let new = lua.create_function(|_, (_, direction): (Table, u8)| {
1818
Ok(Self { direction: Borders::from_bits_truncate(direction), ..Default::default() })
1919
})?;
@@ -29,7 +29,7 @@ impl Bar {
2929
])?;
3030

3131
bar.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?));
32-
Ok(bar)
32+
bar.into_lua(lua)
3333
}
3434

3535
pub(super) fn render(

yazi-plugin/src/elements/border.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mlua::{AnyUserData, Lua, MetaMethod, Table, UserData, Value};
1+
use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, UserData, Value};
22
use ratatui::widgets::{Borders, Widget};
33

44
use super::Area;
@@ -24,7 +24,7 @@ pub struct Border {
2424
}
2525

2626
impl Border {
27-
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
27+
pub fn compose(lua: &Lua) -> mlua::Result<Value> {
2828
let new = lua.create_function(|_, (_, position): (Table, u8)| {
2929
Ok(Border {
3030
position: ratatui::widgets::Borders::from_bits_truncate(position),
@@ -50,7 +50,7 @@ impl Border {
5050
])?;
5151

5252
border.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?));
53-
Ok(border)
53+
border.into_lua(lua)
5454
}
5555

5656
pub(super) fn render(

yazi-plugin/src/elements/clear.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::atomic::{AtomicBool, Ordering};
22

3-
use mlua::{Lua, MetaMethod, Table, UserData};
3+
use mlua::{IntoLua, Lua, MetaMethod, Table, UserData, Value};
44
use yazi_adapter::ADAPTOR;
55

66
use super::Area;
@@ -13,13 +13,13 @@ pub struct Clear {
1313
}
1414

1515
impl Clear {
16-
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
16+
pub fn compose(lua: &Lua) -> mlua::Result<Value> {
1717
let new = lua.create_function(|_, (_, area): (Table, Area)| Ok(Clear { area }))?;
1818

1919
let clear = lua.create_table()?;
2020
clear.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?));
2121

22-
Ok(clear)
22+
clear.into_lua(lua)
2323
}
2424

2525
pub(super) fn render(

yazi-plugin/src/elements/constraint.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
use mlua::{FromLua, Lua, Table, UserData};
1+
use mlua::{FromLua, IntoLua, Lua, UserData, Value};
22

33
#[derive(Clone, Copy, Default, FromLua)]
44
pub struct Constraint(pub(super) ratatui::layout::Constraint);
55

66
impl Constraint {
7-
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
7+
pub fn compose(lua: &Lua) -> mlua::Result<Value> {
88
use ratatui::layout::Constraint as C;
99

10-
lua.create_table_from([
11-
("Min", lua.create_function(|_, n: u16| Ok(Self(C::Min(n))))?),
12-
("Max", lua.create_function(|_, n: u16| Ok(Self(C::Max(n))))?),
13-
("Length", lua.create_function(|_, n: u16| Ok(Self(C::Length(n))))?),
14-
("Percentage", lua.create_function(|_, n: u16| Ok(Self(C::Percentage(n))))?),
15-
("Ratio", lua.create_function(|_, (a, b): (u32, u32)| Ok(Self(C::Ratio(a, b))))?),
16-
("Fill", lua.create_function(|_, n: u16| Ok(Self(C::Fill(n))))?),
17-
])
10+
lua
11+
.create_table_from([
12+
("Min", lua.create_function(|_, n: u16| Ok(Self(C::Min(n))))?),
13+
("Max", lua.create_function(|_, n: u16| Ok(Self(C::Max(n))))?),
14+
("Length", lua.create_function(|_, n: u16| Ok(Self(C::Length(n))))?),
15+
("Percentage", lua.create_function(|_, n: u16| Ok(Self(C::Percentage(n))))?),
16+
("Ratio", lua.create_function(|_, (a, b): (u32, u32)| Ok(Self(C::Ratio(a, b))))?),
17+
("Fill", lua.create_function(|_, n: u16| Ok(Self(C::Fill(n))))?),
18+
])?
19+
.into_lua(lua)
1820
}
1921
}
2022

yazi-plugin/src/elements/elements.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub fn compose(lua: &Lua) -> mlua::Result<Value> {
2323
b"Style" => super::Style::compose(lua)?,
2424
b"Table" => super::Table::compose(lua)?,
2525
b"Text" => super::Text::compose(lua)?,
26+
27+
b"width" => super::Utils::width(lua)?,
2628
_ => return Ok(Value::Nil),
2729
}
2830
.into_lua(lua)

yazi-plugin/src/elements/gauge.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mlua::{AnyUserData, ExternalError, Lua, MetaMethod, Table, UserData, UserDataMethods, Value};
1+
use mlua::{AnyUserData, ExternalError, IntoLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value};
22
use ratatui::widgets::Widget;
33

44
use super::{Area, Span};
@@ -15,13 +15,13 @@ pub struct Gauge {
1515
}
1616

1717
impl Gauge {
18-
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
18+
pub fn compose(lua: &Lua) -> mlua::Result<Value> {
1919
let new = lua.create_function(|_, _: Table| Ok(Gauge::default()))?;
2020

2121
let gauge = lua.create_table()?;
2222
gauge.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?));
2323

24-
Ok(gauge)
24+
gauge.into_lua(lua)
2525
}
2626

2727
pub(super) fn render(

yazi-plugin/src/elements/layout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mlua::{AnyUserData, Lua, MetaMethod, Table, UserData, UserDataMethods};
1+
use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value};
22

33
use super::{Constraint, Rect};
44

@@ -13,13 +13,13 @@ pub struct Layout {
1313
}
1414

1515
impl Layout {
16-
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
16+
pub fn compose(lua: &Lua) -> mlua::Result<Value> {
1717
let new = lua.create_function(|_, _: Table| Ok(Self::default()))?;
1818

1919
let layout = lua.create_table_from([("HORIZONTAL", HORIZONTAL), ("VERTICAL", VERTICAL)])?;
2020
layout.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?));
2121

22-
Ok(layout)
22+
layout.into_lua(lua)
2323
}
2424
}
2525

0 commit comments

Comments
 (0)