-
Notifications
You must be signed in to change notification settings - Fork 5
/
drafto_LuaUtils.lua
161 lines (134 loc) · 4.41 KB
/
drafto_LuaUtils.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
-----------------------------------------------------------------------------------------------------------------------
--- LuaUtils
-- Basic utilities for working with Lua
-- @author draftomatic
-----------------------------------------------------------------------------------------------------------------------
local LuaUtils = {}
--- Converts a table to a string (shallow)
function LuaUtils:PrintTable(tbl)
local str = "{"
for k,v in pairs(tbl) do
str = str .. k .. "=" .. tostring(v) .. ", "
end
str = str .. "}"
return str
end
function LuaUtils:GetTableSize(tbl)
local count = 0
for _ in pairs(tbl) do count = count + 1 end
return count
end
function LuaUtils:Pack(...)
return { ... }, select("#", ...)
end
function LuaUtils:FormatTime(nTime)
if type(nTime) ~= "number" then return nTime end
local nDays, nHours, nMinutes, nSeconds = math.floor(nTime / 86400), math.floor((nTime % 86400) / 3600), math.floor((nTime % 3600) / 60), nTime % 60;
if nDays ~= 0 then
return ("%dd %dh %dm %ds"):format(nDays, nHours, nMinutes, nSeconds)
elseif nHours ~= 0 then
return ("%dh %dm %ds"):format(nHours, nMinutes, nSeconds)
elseif nMinutes ~= 0 then
return ("0h %dm %ds"):format(nMinutes, nSeconds)
else
return ("0m %ds"):format(nSeconds)
end
end
--- Basic string StartsWith utility
function LuaUtils:StartsWith(str, start)
return string.sub(str, 1, string.len(start)) == start
end
function LuaUtils:EndsWith(str,endStr)
return endStr == '' or string.sub(str, -string.len(endStr)) == endStr
end
function LuaUtils:EqualsIgnoreCase(str1, str2)
return str1:lower() == str2:lower()
end
--- Basic string Trim utility
function LuaUtils:Trim(s)
return s:match'^%s*(.*%S)' or ''
end
function LuaUtils:TableContainsString(t, str)
for _,v in pairs(t) do
if v == str then return true end
end
return false
end
--- Wraps a text in color format tags with the given color (aarrggbb hex string)
function LuaUtils:markupTextColor(text, color)
--return "<T TextColor=\"" .. color .. "\">" .. text .. "</T>"
return string.format("<T TextColor=\"%s\">%s</T>", color, text)
end
-- Wraps a text in color format tags with the given color (aarrggbb hex string)
function LuaUtils:MarkupText(text, color, font)
--return "<T TextColor=\"" .. color .. "\">" .. text .. "</T>"
return string.format("<T TextColor=\"%s\" Font=\"%s\">%s</T>", color, font, tostring(text))
end
function LuaUtils:MarkupBGColor(text, color)
--return "<T TextColor=\"" .. color .. "\">" .. text .. "</T>"
return string.format("<T BGColor=\"%s\">%s</T>", color, tostring(text))
end
--- Escapes HTML characters in a string
function LuaUtils:EscapeHTML(str)
local subst = {
["&"] = "&";
['"'] = """;
["'"] = "'";
["<"] = "<";
[">"] = ">";
--["\n"] = " ";
}
return tostring(str):gsub("[&\"'<>\n]", subst)
end
--- Unescapes HTML characters in a string
function LuaUtils:UnescapeHTML(str)
str = tostring(str)
str = str:gsub(""", '"')
str = str:gsub("'", "'")
str = str:gsub("<", "<")
str = str:gsub(">", ">")
str = str:gsub(" ", "\n") -- CRLF (Windows Only!)
str = str:gsub("&", "&")
return str
end
-- Register Library
Apollo.RegisterPackage(LuaUtils, "Drafto:Lib:LuaUtils-1.2", 1, {})
-----------------------------------------------------------------------------------------------------------------------
--- Standard Queue Implementation
-- http://www.lua.org/pil/11.4.html
-----------------------------------------------------------------------------------------------------------------------
local Queue = {}
function Queue.new()
return {first = 0, last = -1}
end
function Queue.PushLeft(queue, value)
local first = queue.first - 1
queue.first = first
queue[first] = value
end
function Queue.PushRight(queue, value)
local last = queue.last + 1
queue.last = last
queue[last] = value
end
function Queue.PopLeft(queue)
local first = queue.first
if first > queue.last then error("queue is empty") end
local value = queue[first]
queue[first] = nil -- to allow garbage collection
queue.first = first + 1
return value
end
function Queue.PopRight(queue)
local last = queue.last
if queue.first > last then error("queue is empty") end
local value = queue[last]
queue[last] = nil -- to allow garbage collection
queue.last = last - 1
return value
end
function Queue.Size(queue)
return queue.last - queue.first + 1
end
-- Register Library
Apollo.RegisterPackage(Queue, "Drafto:Lib:Queue-1.2", 1, {})