forked from endymonium/keystrokelauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.lua
More file actions
120 lines (106 loc) · 2.98 KB
/
Helper.lua
File metadata and controls
120 lines (106 loc) · 2.98 KB
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
local L = LibStub("AceLocale-3.0"):GetLocale("KeystrokeLauncherResurrected")
-- format table as printable string
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
-- check if given slash command exists in _G
function slash_cmd_exists(slash_cmd)
for _, v in pairs(_G) do
if slash_cmd == v then
return true
end
end
end
function item_link_to_string(itemLink)
local itemString, itemName = itemLink:match("|H(.*)|h%[(.*)%]|h")
local itemId = itemString:match(":([^:]+)")
return itemString, itemName, itemId
end
function get_or_create_macro(name, per_char)
local macroId = GetMacroIndexByName(name)
if macroId == 0 then
macroId = CreateMacro(name, "INV_MISC_QUESTIONMARK", "", per_char);
end
return macroId
end
function is_nil_or_empty(val)
if val == nil or val == '' then
return true
else
return false
end
end
function table.length(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- ENUM from https://gist.github.com/szensk/7986347f09742337b36e
-- this enum is really arkward to handle, planing to replace it with something else
local enummt = {
__index = function(table, key)
if rawget(table.enums, key) then
return key
end
end
}
function Enumm(t)
local e = { enums = t }
return setmetatable(e, enummt)
end
function enumm_to_table(enumm)
local rv = {}
for _,v in pairs(enumm) do
for k1,_ in pairs(v) do
rv[k1] = L['CONFIG_INDEX_TYPES_'..k1]
end
end
return rv
end
-- https://gist.github.com/haggen/2fd643ea9a261fea2094
local charset = {} do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(charset, string.char(c)) end
for c = 65, 90 do table.insert(charset, string.char(c)) end
for c = 97, 122 do table.insert(charset, string.char(c)) end
end
function randomString(length)
if not length or length <= 0 then return '' end
return randomString(length - 1) .. charset[math.random(1, #charset)]
end
-- http://snipplr.com/view/13092/strlpad--pad-string-to-the-left/
string.lpad = function(str, len, char)
str = tostring(str)
if char == nil then char = ' ' end
return str .. string.rep(char, len - #str)
end
-- http://lua-users.org/wiki/CopyTable
function shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end