Skip to content

Commit 49910fc

Browse files
cattyman919sxyazi
andauthored
feat: progress of each task (#3121)
Co-authored-by: sxyazi <[email protected]>
1 parent b997e56 commit 49910fc

File tree

5 files changed

+108
-15
lines changed

5 files changed

+108
-15
lines changed

yazi-config/preset/theme-dark.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ icon_command = ""
202202
[tasks]
203203
border = { fg = "blue" }
204204
title = {}
205-
hovered = { fg = "magenta", underline = true }
205+
hovered = { fg = "magenta", bold = true }
206206

207207
# : }}}
208208

yazi-config/preset/theme-light.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ icon_command = ""
202202
[tasks]
203203
border = { fg = "blue" }
204204
title = {}
205-
hovered = { fg = "magenta", underline = true }
205+
hovered = { fg = "magenta", bold = true }
206206

207207
# : }}}
208208

yazi-core/src/tasks/tasks.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ impl Tasks {
5454
}
5555

5656
pub fn limit() -> usize {
57-
(Dimension::available().rows * TASKS_PERCENT / 100).saturating_sub(TASKS_BORDER + TASKS_PADDING)
58-
as usize
57+
((Dimension::available().rows * TASKS_PERCENT / 100)
58+
.saturating_sub(TASKS_BORDER + TASKS_PADDING) as usize)
59+
/ 3
5960
}
6061

6162
pub fn paginate(&self) -> Vec<TaskSnap> {

yazi-plugin/preset/components/tasks.lua

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,113 @@ Tasks = {
22
_id = "tasks",
33
}
44

5-
function Tasks:new(area) return setmetatable({ _area = area }, { __index = self }) end
5+
function Tasks:new(area)
6+
local me = setmetatable({ _area = area }, { __index = self })
7+
me:layout()
8+
return me
9+
end
10+
11+
function Tasks:layout()
12+
self._area = self._area:pad(ui.Pad(1, 1, 1, 3))
13+
self._chunks = ui.Layout()
14+
:direction(ui.Layout.HORIZONTAL)
15+
:constraints({
16+
ui.Constraint.Percentage(60),
17+
ui.Constraint.Percentage(40),
18+
})
19+
:split(self._area)
20+
end
621

722
function Tasks:reflow() return { self } end
823

924
function Tasks:redraw()
10-
local rows = {}
11-
for _, snap in ipairs(cx.tasks.snaps) do
12-
rows[#rows + 1] = ui.Row { snap.name }
25+
local elements = {}
26+
for i, snap in ipairs(cx.tasks.snaps) do
27+
local y = self._area.y + (i - 1) * 3
28+
if y >= self._area.bottom then
29+
break
30+
end
31+
32+
elements[#elements + 1] = ui.Line({ self:icon(snap), snap.name }):area(ui.Rect {
33+
x = self._area.x,
34+
y = y,
35+
w = self._area.w,
36+
h = 1,
37+
})
38+
39+
if i == cx.tasks.cursor + 1 then
40+
elements[#elements] = elements[#elements]:style(th.tasks.hovered)
41+
end
42+
43+
for _, e in ipairs(self:progress_redraw(snap, y + 1)) do
44+
elements[#elements + 1] = e
45+
end
46+
47+
elements[#elements + 1] = ui.Bar(ui.Edge.LEFT)
48+
:area(ui.Rect {
49+
x = math.max(0, self._area.x - 2),
50+
y = y,
51+
w = self._area.w,
52+
h = 2,
53+
})
54+
:symbol("")
55+
56+
if i == cx.tasks.cursor + 1 then
57+
elements[#elements] = elements[#elements]:style(th.tasks.hovered)
58+
end
1359
end
1460

15-
local tbl = ui.Table(rows)
16-
:area(self._area:pad(ui.Pad.x(1)))
17-
:row(cx.tasks.cursor)
18-
:row_style(th.tasks.hovered)
19-
:widths { ui.Constraint.Fill(1) }
61+
return elements
62+
end
63+
64+
function Tasks:icon(snap)
65+
if snap.prog.kind == "FilePaste" then
66+
return ""
67+
elseif snap.prog.kind == "FileDelete" then
68+
return ""
69+
else
70+
return ""
71+
end
72+
end
2073

21-
return { tbl }
74+
function Tasks:progress_redraw(snap, y)
75+
local kind = snap.prog.kind
76+
if kind == "FilePaste" or kind == "FileDelete" then
77+
local label = string.format(
78+
"%3d%% - %s / %s",
79+
math.floor(snap.percent),
80+
ya.readable_size(snap.prog.processed_bytes),
81+
ya.readable_size(snap.prog.total_bytes)
82+
)
83+
84+
local style = th.status.progress_normal
85+
if snap.prog.failed_files > 0 then
86+
style = th.status.progress_error
87+
end
88+
89+
return {
90+
ui.Gauge()
91+
:area(ui.Rect { x = self._chunks[1].x, y = y, w = self._chunks[1].w, h = 1 })
92+
:percent(snap.percent)
93+
:label(ui.Span(label):style(th.status.progress_label))
94+
:gauge_style(style),
95+
96+
ui.Line(string.format("%d/%d", snap.prog.success_files, snap.prog.total_files))
97+
:fg("gray")
98+
:area(ui.Rect { x = self._chunks[2].x, y = y, w = self._chunks[2].w, h = 1 })
99+
:align(ui.Align.RIGHT),
100+
}
101+
else
102+
local text
103+
if snap.running then
104+
text = "Running…"
105+
elseif snap.success then
106+
text = "Completing…"
107+
else
108+
text = "Failed, press Enter to view log…"
109+
end
110+
return {
111+
ui.Line(text):fg("gray"):area(ui.Rect { x = self._chunks[1].x, y = y, w = self._chunks[1].w, h = 1 }),
112+
}
113+
end
22114
end

yazi-shared/src/url/buf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ mod tests {
339339

340340
for (path, expected) in cases {
341341
let path: UrlBuf = path.parse()?;
342-
assert_eq!(path.parent().map(|u| format!("{:?}", u)).as_deref(), expected);
342+
assert_eq!(path.parent().map(|u| format!("{u:?}")).as_deref(), expected);
343343
}
344344

345345
Ok(())

0 commit comments

Comments
 (0)