Skip to content

Commit f4c35b5

Browse files
committed
feat: truncate long items in the file list (#2754)
1 parent cea2628 commit f4c35b5

File tree

7 files changed

+46
-20
lines changed

7 files changed

+46
-20
lines changed

yazi-plugin/preset/components/current.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ function Current:redraw()
3232
return self:empty()
3333
end
3434

35-
local entities, linemodes = {}, {}
35+
local left, right = {}, {}
3636
for _, f in ipairs(files) do
37-
entities[#entities + 1] = Entity:new(f):redraw()
38-
linemodes[#linemodes + 1] = Linemode:new(f):redraw()
37+
left[#left + 1] = Entity:new(f):redraw()
38+
right[#right + 1] = Linemode:new(f):redraw()
39+
left[#left]:truncate { max = math.max(0, self._area.w - right[#right]:width()) }
3940
end
4041

4142
return {
42-
ui.List(entities):area(self._area),
43-
ui.Text(linemodes):area(self._area):align(ui.Text.RIGHT),
43+
ui.List(left):area(self._area),
44+
ui.Text(right):area(self._area):align(ui.Text.RIGHT),
4445
}
4546
end
4647

yazi-plugin/preset/components/parent.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ function Parent:redraw()
1717
return {}
1818
end
1919

20-
local entities = {}
20+
local items = {}
2121
for _, f in ipairs(self._folder.window) do
22-
entities[#entities + 1] = Entity:new(f):redraw()
22+
items[#items + 1] = Entity:new(f):redraw():truncate { max = self._area.w }
2323
end
2424

2525
return {
26-
ui.List(entities):area(self._area),
26+
ui.List(items):area(self._area),
2727
}
2828
end
2929

yazi-plugin/preset/plugins/folder.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ function M:peek(job)
1919
return ya.preview_widget(job, ui.Line(s):area(job.area):align(ui.Line.CENTER))
2020
end
2121

22-
local entities = {}
22+
local items = {}
2323
for _, f in ipairs(folder.window) do
24-
entities[#entities + 1] = Entity:new(f):redraw()
24+
items[#items + 1] = Entity:new(f):redraw():truncate { max = job.area.w }
2525
end
2626

2727
ya.preview_widget(job, {
28-
ui.List(entities):area(job.area),
28+
ui.List(items):area(job.area),
2929
table.unpack(Marker:new(job.area, folder):redraw()),
3030
})
3131
end

yazi-plugin/preset/plugins/magick.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function M:preload(job)
3131
end
3232

3333
-- stylua: ignore
34-
local status, err = cmd:args {
34+
local status, err = cmd:arg {
3535
tostring(job.file.url), "-auto-orient", "-strip",
3636
"-sample", string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
3737
"-quality", rt.preview.image_quality,
@@ -48,7 +48,7 @@ end
4848
function M:spot(job) require("file"):spot(job) end
4949

5050
function M.with_limit()
51-
local cmd = Command("magick"):args { "-limit", "thread", 1 }
51+
local cmd = Command("magick"):arg { "-limit", "thread", 1 }
5252
if rt.tasks.image_alloc > 0 then
5353
cmd:arg { "-limit", "memory", rt.tasks.image_alloc, "-limit", "disk", "1MiB" }
5454
end

yazi-plugin/preset/plugins/svg.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function M:preload(job)
2626
end
2727

2828
-- stylua: ignore
29-
local cmd = Command("resvg"):args {
29+
local cmd = Command("resvg"):arg {
3030
"-w", rt.preview.max_width, "-h", rt.preview.max_height,
3131
"--image-rendering", "optimizeSpeed",
3232
tostring(job.file.url), tostring(cache)

yazi-plugin/preset/plugins/video.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ function M:preload(job)
6060
})
6161

6262
if percent ~= 0 then
63-
cmd:args { "-ss", math.floor(meta.format.duration * percent / 100) }
63+
cmd:arg { "-ss", math.floor(meta.format.duration * percent / 100) }
6464
end
65-
cmd:args { "-i", tostring(job.file.url) }
65+
cmd:arg { "-i", tostring(job.file.url) }
6666
if percent == 0 then
67-
cmd:args { "-map", "disp:attached_pic" }
67+
cmd:arg { "-map", "disp:attached_pic" }
6868
end
6969

7070
-- stylua: ignore
@@ -126,9 +126,9 @@ function M:spot_base(job)
126126
end
127127

128128
function M.list_meta(url, entries)
129-
local cmd = Command("ffprobe"):args { "-v", "quiet" }
129+
local cmd = Command("ffprobe"):arg { "-v", "quiet" }
130130
if not entries:find("attached_pic", 1, true) then
131-
cmd = cmd:args { "-select_streams", "v" }
131+
cmd:arg { "-select_streams", "v" }
132132
end
133133

134134
local output, err = cmd:arg({ "-show_entries", entries, "-of", "json=c=1", tostring(url) }):output()

yazi-plugin/src/elements/line.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::mem;
1+
use std::{borrow::Cow, mem};
22

33
use ansi_to_tui::IntoText;
44
use mlua::{AnyUserData, ExternalError, ExternalResult, IntoLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value};
@@ -131,5 +131,30 @@ impl UserData for Line {
131131
methods.add_method("visible", |_, me, ()| {
132132
Ok(me.inner.iter().flat_map(|s| s.content.chars()).any(|c| c.width().unwrap_or(0) > 0))
133133
});
134+
methods.add_function_mut("truncate", |_, (ud, t): (AnyUserData, Table)| {
135+
let mut me = ud.borrow_mut::<Self>()?;
136+
let max = t.raw_get("max")?;
137+
138+
let mut width = 0;
139+
'outer: for (x, span) in me.inner.iter_mut().enumerate() {
140+
for (y, c) in span.content.char_indices() {
141+
width += c.width().unwrap_or(0);
142+
if width < max {
143+
continue;
144+
} else if width == max && span.content[y..].chars().nth(1).is_none() {
145+
continue;
146+
}
147+
148+
match &mut span.content {
149+
Cow::Borrowed(s) => span.content = Cow::Borrowed(&s[..y]),
150+
Cow::Owned(s) => s.truncate(y),
151+
}
152+
me.inner.spans.truncate(x + 1);
153+
me.inner.spans.push(ratatui::text::Span::raw("…"));
154+
break 'outer;
155+
}
156+
}
157+
Ok(ud)
158+
});
134159
}
135160
}

0 commit comments

Comments
 (0)