@@ -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
722function Tasks :reflow () return { self } end
823
924function 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
22114end
0 commit comments