-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhilbifetch.lua
executable file
·241 lines (198 loc) · 5.83 KB
/
hilbifetch.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env hilbish
local lunacolors = require 'lunacolors'
local hilbifetch = {
order = {
{'title', showName = false},
{'infosep', showName = false},
'os',
'kernel',
'uptime',
'terminal',
'shell',
{'padding', showName = false},
{'colors', showName = false}
}
}
local infotable = {}
local displayName = {}
-- ASCII art
hilbifetch.ascii =
[[ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣐⣶⣶⣀⡀⣤⠄⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣠⣴⣖⢶⣶⣦⣀⢰⣿⣿⣿⣿⠏⠀⠀⣐⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣸⣿⣿⣿⣯⣻⣿⣿⢹⣿⣿⣿⡟⠠⠨⠪⠂⣰⣦⣄⠀⠀⠀
⠀⠀⠀⢠⢯⣶⣮⢿⣿⣷⡽⣿⡘⣿⡿⣿⠁⡑⠡⠃⣰⣿⣿⣿⣦⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠑⠛⢿⣿⣿⣇⣿⠇⠁⡐⠀⢀⣬⣿⣿⣿⣿⣿⡄⠀
⠀⠀⠀⠀⠀⠀⣠⣴⣶⣴⣽⢿⣯⠸⡀⡐⠀⠐⢱⣙⣮⣭⣿⣿⠿⠷⠀
⠀⠀⠀⠀⣠⣻⣿⠿⣿⣿⣟⣩⡑⠀⢠⠁⠀⠮⠿⠶⠶⠦⣌⡁⠀⠀⠀
⠀⠀⠀⢈⣿⣿⣿⣿⣷⣿⣿⢟⣍⣤⣨⡴⣄⢎⢍⡖⡿⠽⣿⣟⣷⣄⠀
⠀⠀⠀⠸⣿⣿⣿⣿⢿⣯⣮⣟⣾⣿⠋⠘⠉⠑⣳⡻⣦⣯⣽⣿⣿⣿⡀
⠀⠀⠀⠀⠘⢿⣷⣽⣿⡿⡎⠛⠋⠁⠀⠀⠀⠀⢳⢿⣽⣷⣿⣝⢿⡿⠀
⠀⠀⠀⠀⠀⠀⠉⠙⠻⠋⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣽⣾⡿⠟⠁⠀]]
hilbifetch.sep = ' > '
function hilbifetch.addInfo(name, cb)
infotable[name] = cb
end
function hilbifetch.getInfo(name)
return infotable[name]()
end
function hilbifetch.setDisplay(name, display)
displayName[name] = display
end
function hilbifetch.getDisplay(name)
return displayName[name]
end
hilbifetch.addInfo('title', function()
return string.format('%s@%s', hilbish.user, hilbish.host)
end)
hilbifetch.addInfo('infosep', function()
local title = hilbifetch.getInfo 'title'
return string.rep('~', title:len() + 1)
end)
hilbifetch.addInfo('colors', function()
local r = '\27[49m'
local result = {'', ''}
for i = 0, 7 do
result[1] = result[1] .. '\27[4' .. i .. 'm '
end
result[1] = result[1] .. r
for i = 0, 7 do
result[2] = result[2] .. '\27[10' .. i .. 'm '
end
result[2] = result[2] .. r
return result
end)
hilbifetch.addInfo('os', function()
return hilbish.os.name
end)
hilbifetch.addInfo('kernel', function()
local f = io.open '/proc/version'
local content = f:read '*a'
f:close()
local kernel = content:split(' ')[3]:split('-')[1]
return kernel
end)
hilbifetch.addInfo('uptime', function()
local _, upout = hilbish.run('uptime -p', false)
-- (temporary) workaround for the `up` removal; an empty string doesnt work ??
local uptime = upout:gsub('\n', ''):gsub('up ', '\0')
return uptime
end)
hilbifetch.addInfo('shell', function()
local shellbin = os.getenv 'SHELL'
local _, out = hilbish.run(shellbin .. ' --version', false)
return out:gsub('\n', '')
end)
hilbifetch.addInfo('memory', function()
local f = io.open('/proc/meminfo', 'rb')
local meminfo = f:read '*a'
local memTotal, usedMem
if meminfo then
memTotal = meminfo:sub(select(1, meminfo:find 'MemTotal:' ), -1)
memTotal = memTotal:sub(1, select(2, memTotal:find '\n'))
:gsub('MemTotal:%s+', '')
:gsub(' kB\n', '')
p = io.popen 'free | grep Mem'
usedMem = p:read '*a'
usedMem = usedMem:gsub('[%a%p]+%s+%d+%s+', '')
local i, j = usedMem:find '%d+'
usedMem = usedMem:sub(i, j)
else
memTotal = 0
usedMem = 0
end
f:close()
return string.format('%.0f/%.0fMiB', tonumber(usedMem) / 1024, tonumber(memTotal) / 1024)
end)
hilbifetch.addInfo('padding', function()
local amount
for i, v in ipairs(hilbifetch.order) do
local entry = type(v) == 'table' and v[1] or v
if entry == 'padding' then
amount = #hilbifetch.ascii:split('\n') - i - 1
end
end
return string.rep(' ', amount < 0 and 0 or amount):split(' ')
end)
hilbifetch.addInfo('terminal', function()
return os.getenv 'TERM'
end)
function longest(arr)
local longestint = 0
local longestidx = 1
for i = 1, #arr do
if utf8.len(arr[i]) > longestint then
longestint, longestidx = utf8.len(arr[i]), i
end
end
return longestidx, longestint
end
function hilbifetch.echo()
local asciiArr = string.split(hilbifetch.ascii, '\n')
for i, _ in ipairs(asciiArr) do
asciiArr[i] = asciiArr[i]:gsub('^%s*(.-)$', '%1')
end
local _, len = longest(asciiArr)
local done = false
local idx = 0
local infos = {}
for i, _ in ipairs(hilbifetch.order) do
local infPiece = hilbifetch.order[i]
local infoKey = infPiece
if type(infPiece) == 'table' then
infoKey = infPiece[1]
else
infPiece = {}
end
local inf = hilbifetch.getInfo(infoKey)
local display = hilbifetch.getDisplay(infoKey)
local infName = display or infoKey
local infFormat = ''
if infPiece.showName ~= false then
local color = infPiece.nameColor or 'bold yellow'
color = color:gsub('(%s-)(%w+)', '{%2}')
infFormat = lunacolors.format(color .. infName) .. hilbifetch.sep
end
local infColor = infPiece.color or ''
infColor = infColor:gsub('(%s-)(%w+)', '{%2}')
if type(inf) == 'table' then
for j, v in ipairs(inf) do
v = lunacolors.format(infColor .. v)
if j == 1 then
infFormat = infFormat .. v
else
infFormat = v
end
table.insert(infos, infFormat)
end
else
table.insert(infos, infFormat .. lunacolors.format(infColor .. inf))
end
end
while not done do
local infoPart = ''
idx = idx + 1
local asciiPart = asciiArr[idx]
local spacecount = len + 3
if asciiPart then
infoPart = asciiPart
spacecount = len - utf8.len(asciiPart) + 3
end
local info = infos[idx]
if info then
infoPart = infoPart .. string.rep(' ', spacecount) .. info
end
infoPart = infoPart .. '\n'
if infoPart == '\n' then
done = true
end
io.write(infoPart)
io.flush()
end
end
if not pcall(debug.getlocal, 4, 1) then
local env = setmetatable({hilbifetch = hilbifetch}, {__index = _G})
local f, err = loadfile(hilbish.userDir.config .. '/hilbifetch/init.lua', 't', env)
assert(f, err)
f()
hilbifetch.echo()
end