fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosBugfixes + Dark Mode skina8034e9

v2.1.1a
5.2 KiB250 linesraw
1local floor = math.floor
2local random = math.random
3
4local byte = string.byte
5local char = string.char
6local find = string.find
7local format = string.format
8local gmatch = string.gmatch
9local gsub = string.gsub
10local match = string.match
11local reverse = string.reverse
12local sub = string.sub
13local upper = string.upper
14
15local concat = table.concat
16local insert = table.insert
17
18function string.getFilePath(path)
19	return match(path, "^(.*[/\\])[^/\\]-$") or ""
20end
21
22function string.getFileExtension(path)
23	return match(path, "%.([^%.]+)$") or ""
24end
25
26function string.stripFileExtension(path)
27	local i = match(path, ".+()%.[^%.]+$")
28	if i then return sub(path, 1, i-1) end
29	return path
30end
31
32do
33	-- With special chars: 33-126
34	-- without: 48-57, 65-90, 97-122 (this is going to be a bit messy without continue)
35	local num = 0 --recycling these up here since they get called a lot
36	local function numroll()
37		num = random(48, 122)
38		if (num > 57 and num < 65) or (num > 90 and num < 97) then
39			return numroll()
40		else
41			return num
42		end
43	end
44
45	local tab = {}
46	function string.random(len, specialchars)
47		tab = {}
48		if specialchars then
49			for i=1, len do
50				insert(tab, char(random(33, 126)))
51			end
52			return concat(tab)
53		else
54			for i=1, len do
55				insert(tab, char(numroll()))
56			end
57			return concat(tab)
58		end
59	end
60end
61
62function string.toTable(str)
63	local tbl = {}
64	
65	for i=1, #str do
66		tbl[i] = sub(str, i, i)
67	end
68	
69	return tbl
70end
71
72function string.explode(str, sep, pat)
73	if sep == "" then return string.toTable(str) end
74	
75	local ret = {}
76	local i, pos = 1, 1
77	
78	if not pat then sep = gsub(sep, "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1") end
79	
80	for startpos, endpos in gmatch(str, "()"..sep.."()") do
81		ret[i] = sub(str, pos, startpos - 1)
82		i = i + 1
83		pos = endpos
84	end
85	
86	ret[i] = sub(str, pos)
87	return ret
88end
89
90-- remove trailing and leading whitespace from string.
91function string.trim(s)
92	return gsub(s, "^%s*(.-)%s*$", "%1")
93end
94
95local trim = string.trim
96
97-- remove leading whitespace from string.
98function string.ltrim(s)
99	return gsub(s, "^%s*", "")
100end
101
102-- remove trailing whitespace from string.
103function string.rtrim(s)
104	local n = #s
105	while n > 0 and find(s, "^%s", n) do n = n - 1 end
106	return sub(s, 1, n)
107end
108
109function string.parseArgs(line)
110	local cmd, val = match(line, "(%S-)%s-=%s+(.+)")
111	if cmd and val then
112		return {trim(cmd), trim(val)}
113	end
114	local quote = sub(line, 1,1) ~= '"'
115	local ret = {}
116	for chunk in gmatch(line, '[^"]+') do
117		quote = not quote
118		if quote then
119			insert(ret,chunk)
120		else
121			for chunk in gmatch(chunk, "%S+") do -- changed %w to %S to allow all characters except space
122				insert(ret, chunk)
123			end
124		end
125	end
126	return ret
127end
128
129local pattern_escape_replacements = {
130	["("] = "%(",
131	[")"] = "%)",
132	["."] = "%.",
133	["%"] = "%%",
134	["+"] = "%+",
135	["-"] = "%-",
136	["*"] = "%*",
137	["?"] = "%?",
138	["["] = "%[",
139	["]"] = "%]",
140	["^"] = "%^",
141	["$"] = "%$",
142	["\0"] = "%z"
143}
144
145function string.escape(str)
146	return gsub(str, ".", pattern_escape_replacements)
147end
148
149function string.toSize(size)
150	size = tonumber(size) or 0
151
152	if size <= 0 then return "0" end
153	if size < 1024 then return format("%.2f Bytes", size) end
154	if size < 1024 * 1024 then return format("%.2f KB", size / 1024) end
155	if size < 1024 * 1024 * 1024 then return format("%.2f MB", size / (1024 * 1024)) end
156
157	return format("%.2f GB", size / (1024 * 1024 * 1024))
158end
159
160function string.upperFirst(str)
161	return gsub(str, "^%l", upper)
162end
163
164function string.addCommas(str)
165	str = tostring(str)
166	return reverse(gsub(gsub(reverse(str), "(...)", "%1,"), ",$", ""))
167end
168
169function string.niceNumber(num)
170	return string.addCommas(format("%0.0f", num))
171end
172
173function string.singularOrMuliple(str, num)
174	local fmt = format("%i %s", num, str)
175	return num == 1 and fmt or (fmt .. "s")
176end
177
178function string.AOrAn(s)
179	return match(s, "^h?[AaEeIiOoUu]") and "an" or "a"
180end
181
182function string.secondsToHuman(sec, accuracy)
183
184	local accuracy = accuracy or 2
185
186	local years = sec/31536000
187	local yearsRemainder = sec%31536000
188	local days = yearsRemainder/86400
189	local daysRemainder = sec%86400
190	local hours = daysRemainder/3600
191	local hourRemainder = (sec - 86400)%3600
192	local min = hourRemainder/60
193	local sec = sec%60
194	
195	years = floor(years)
196	days = floor(days)
197	hours = floor(hours)
198	min = floor(min)
199	sec = floor(sec)
200	
201	local results = {}
202	
203	if years >= 1 then
204		insert(results, string.singularOrMuliple("year", years))
205	end
206	if days >= 1 then
207		insert(results, string.singularOrMuliple("day", days))
208	end
209	if hours >= 1 then
210		insert(results, string.singularOrMuliple("hour", hours))
211	end
212	if min >= 1 then
213		insert(results, string.singularOrMuliple("minute", min))
214	end
215	if sec >= 1 then
216		insert(results, string.singularOrMuliple("second", sec))
217	end
218	
219	local result = {}
220	for i=1,accuracy do
221		result[ i ] = results[ i ]
222	end
223	
224	return concat(result, ", ")
225end
226
227function string.escapeURL(url)
228	return gsub(url, "([^A-Za-z0-9_])", function(c)
229		return format("%%%02x", byte(c))
230	end)
231end
232
233function string.enescapeURL(url)
234	return gsub(url, "%%(%x%x)", function(hex)
235		return char(tonumber(hex, 16))
236	end)
237end
238
239function string.fromhex(str)
240	return (str:gsub('..', function (cc)
241		return string.char(tonumber(cc, 16))
242	end))
243end
244
245function string.tohex(str)
246	if not str or str == "" then return "00" end
247	return str:gsub('.', function (c)
248		return string.format('%02X', string.byte(c))
249	end)
250end