-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathstatus.lua
184 lines (155 loc) · 4.2 KB
/
status.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Status = {
LEFT = 0,
RIGHT = 1,
_id = "status",
_inc = 1000,
_left = {
{ "mode", id = 1, order = 1000 },
{ "size", id = 2, order = 2000 },
{ "name", id = 3, order = 3000 },
},
_right = {
{ "perm", id = 4, order = 1000 },
{ "percent", id = 5, order = 2000 },
{ "position", id = 6, order = 3000 },
},
}
function Status:new(area, tab)
return setmetatable({
_area = area,
_tab = tab,
_current = tab.current,
}, { __index = self })
end
function Status:style()
local m = THEME.mode
if self._tab.mode.is_select then
return { main = m.select_main, alt = m.select_alt }
elseif self._tab.mode.is_unset then
return { main = m.unset_main, alt = m.unset_alt }
else
return { main = m.normal_main, alt = m.normal_alt }
end
end
function Status:mode()
local mode = tostring(self._tab.mode):sub(1, 3):upper()
local style = self:style()
return ui.Line {
ui.Span(THEME.status.separator_open):fg(style.main.bg),
ui.Span(" " .. mode .. " "):style(style.main),
ui.Span(THEME.status.separator_close):fg(style.main.bg):bg(style.alt.bg),
}
end
function Status:size()
local h = self._current.hovered
if not h then
return ""
end
local style = self:style()
return ui.Line {
ui.Span(" " .. ya.readable_size(h:size() or h.cha.len) .. " "):style(style.alt),
ui.Span(THEME.status.separator_close):fg(style.alt.bg),
}
end
function Status:name()
local h = self._current.hovered
if not h then
return ""
end
return " " .. h.name:gsub("\r", "?", 1)
end
function Status:perm()
local h = self._current.hovered
if not h then
return ""
end
local perm = h.cha:perm()
if not perm then
return ""
end
local spans = {}
for i = 1, #perm do
local c = perm:sub(i, i)
local style = THEME.status.perm_type
if c == "-" or c == "?" then
style = THEME.status.perm_sep
elseif c == "r" then
style = THEME.status.perm_read
elseif c == "w" then
style = THEME.status.perm_write
elseif c == "x" or c == "s" or c == "S" or c == "t" or c == "T" then
style = THEME.status.perm_exec
end
spans[i] = ui.Span(c):style(style)
end
return ui.Line(spans)
end
function Status:percent()
local percent = 0
local cursor = self._current.cursor
local length = #self._current.files
if cursor ~= 0 and length ~= 0 then
percent = math.floor((cursor + 1) * 100 / length)
end
if percent == 0 then
percent = " Top "
elseif percent == 100 then
percent = " Bot "
else
percent = string.format(" %2d%% ", percent)
end
local style = self:style()
return ui.Line {
ui.Span(" " .. THEME.status.separator_open):fg(style.alt.bg),
ui.Span(percent):style(style.alt),
}
end
function Status:position()
local cursor = self._current.cursor
local length = #self._current.files
local style = self:style()
return ui.Line {
ui.Span(THEME.status.separator_open):fg(style.main.bg):bg(style.alt.bg),
ui.Span(string.format(" %2d/%-2d ", math.min(cursor + 1, length), length)):style(style.main),
ui.Span(THEME.status.separator_close):fg(style.main.bg),
}
end
function Status:reflow() return { self } end
function Status:redraw()
local left = self:children_redraw(self.LEFT)
local right = self:children_redraw(self.RIGHT)
local right_width = right:width()
return {
ui.Text(left):area(self._area),
ui.Text(right):area(self._area):align(ui.Text.RIGHT),
table.unpack(ya.redraw_with(Progress:new(self._area, right_width))),
}
end
-- Mouse events
function Status:click(event, up) end
function Status:scroll(event, step) end
function Status:touch(event, step) end
-- Children
function Status:children_add(fn, order, side)
self._inc = self._inc + 1
local children = side == self.RIGHT and self._right or self._left
children[#children + 1] = { fn, id = self._inc, order = order }
table.sort(children, function(a, b) return a.order < b.order end)
return self._inc
end
function Status:children_remove(id, side)
local children = side == self.RIGHT and self._right or self._left
for i, child in ipairs(children) do
if child.id == id then
table.remove(children, i)
break
end
end
end
function Status:children_redraw(side)
local lines = {}
for _, c in ipairs(side == self.RIGHT and self._right or self._left) do
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
end
return ui.Line(lines)
end