-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlib.lua
241 lines (207 loc) · 5.35 KB
/
lib.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
local socket = require 'socket'
function os.capture(cmd)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
return s
end
function io.fileExists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
function mtime()
return socket.gettime()
end
function string:split(delimiter)
local text = self
local list = {}
local pos = 1
if delimiter == nil or string.find("", delimiter, 1) then -- this would result in endless loops
error("delimiter matches empty string!")
end
while 1 do
local first = text:find(delimiter, pos)
if first then -- found?
table.insert(list, text:sub(pos, first-1))
pos = first+1
else
table.insert(list, text:sub(pos))
break
end
end
return list
end
function string:pad(pad_length, pad_string, pad_type)
local output = self
if not pad_string then pad_string = ' ' end
if not pad_type then pad_type = 'STR_PAD_RIGHT' end
if pad_type == 'STR_PAD_BOTH' then
local j = 0
while string.len(output) < pad_length do
output = j % 2 == 0 and output .. pad_string or pad_string .. output
j = j + 1
end
else
while string.len(output) < pad_length do
output = pad_type == 'STR_PAD_LEFT' and pad_string .. output or output .. pad_string
end
end
return output
end
function string:titlecase()
local str, result = self, ''
for word in string.gmatch(str, "%S+") do
local first = string.sub(word,1,1)
result = (result .. string.upper(first) ..
string.lower(string.sub(word,2)) .. ' ')
end
return result
end
function table.clone(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[table.clone(orig_key)] = table.clone(orig_value)
end
setmetatable(copy, table.clone(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function table.map(table, f)
local result = {}
for key, value in pairs(table) do
result[key] = f(value);
end
return result
end
function table.group(table, by, cols)
local result = {}
for _, value in pairs(table) do
key = by(value);
if key ~= nil then
if result[key] == nil then
result[key] = {key = key}
--for col, f in cols do
--result[key][col] = nil
--end
end
for col, f in pairs(cols) do
result[key][col] = f(result[key][col], value)
end
end
end
return result
end
function table.values(table)
local result = {}
for _, value in pairs(table) do
result[#result+1] = value
end
return result
end
function table.filter(table, f)
local result = {}
for key, value in pairs(table) do
if f(value, key) then
result[key] = value
end
end
return result
end
local units = {'B', 'KiB', 'MiB', 'GiB', 'TiB'}
function humanReadableBytes(bytes, current)
if current == nil then current = 'B' end
for i,unit in pairs(units) do
if current == unit then
if bytes > 99 then
bytes = round(bytes / 1024, 1)
current = units[i+1]
else
return bytes .. unit
end
end
end
return bytes .. 'PiB'
end
function round(num, numDecimalPlaces)
if numDecimalPlaces and numDecimalPlaces>0 then
local mult = 10^numDecimalPlaces
return math.floor(num * mult + 0.5) / mult
end
return math.floor(num + 0.5)
end
function interp(s, tab)
return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
function getCoreHwmon()
local output = os.capture('ls -l /sys/class/hwmon|grep coretemp')
if output == '' then
return 0
end
return tonumber(output:match(' hwmon%d '):sub(-2, -1));
end
function getCurrentNetwork()
networks = os.capture('ip route|egrep \'^(0.0.0.0|default)\'|awk \'{print $5}\''):split("\n")
return networks[1]
end
function isWifi()
local p = getCurrentNetwork():find('wl')
return p ~= nil and p == 1
end
local lastSpeedTest = {
download = 3000,
upload = 1000,
at = 0
}
local function speedtest()
if lastSpeedTest.at > os.time() - 3600 then
return lastSpeedTest;
end
local output = os.capture('speedtest'):split("\n")
lastSpeedTest.at = os.time()
lastSpeedTest.download = tonumber(output[1]:gsub(".%d+ KiB/s", ''), 10)
lastSpeedTest.upload = tonumber(output[2]:gsub(".%d+ KiB/s", ''), 10)
print('speedtest at ', lastSpeedTest.at, lastSpeedTest.download, lastSpeedTest.upload)
return lastSpeedTest
end
function getDownloadSpeed()
return speedtest().download
end
function getUploadSpeed()
return speedtest().upload
end
function hasBattery(bat)
bat = bat or 'BAT0'
return io.fileExists('/sys/class/power_supply/' .. bat)
end
function getCpuCount()
return tonumber(os.capture('nproc'))
end
function tprint (tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent+1)
elseif type(v) == 'boolean' then
print(formatting .. tostring(v))
elseif type(v) == 'function' then
print(formatting .. '<function>')
else
print(formatting .. v)
end
end
end