-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaclie.lua
More file actions
220 lines (192 loc) · 5.49 KB
/
Copy pathluaclie.lua
File metadata and controls
220 lines (192 loc) · 5.49 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
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
require("luaclie.luaclie_c")
luaclie = {}
luaclie.COLOR = {}
luaclie.COLOR.DEFAULT = "\27[0m"
luaclie.COLOR.GRAY = "\27[2m"
luaclie.COLOR.GRAY0 = "\27[30;1m"
luaclie.COLOR.GRAY1 = "\27[90;2m"
luaclie.COLOR.GRAY2 = "\27[30;3m"
luaclie.COLOR.RED = "\27[31;1m"
luaclie.COLOR.RED0 = "\27[91;2m"
luaclie.COLOR.RED1 = "\27[31;3m"
luaclie.COLOR.RED2 = "\27[31;2m"
luaclie.COLOR.GREEN = "\27[32;1m"
luaclie.COLOR.GREEN0 = "\27[92;2m"
luaclie.COLOR.GREEN1 = "\27[32;3m"
luaclie.COLOR.GREEN2 = "\27[32;2m"
luaclie.COLOR.YELLOW = "\27[33;1m"
luaclie.COLOR.YELLOW0 = "\27[93;2m"
luaclie.COLOR.YELLOW1 = "\27[33;3m"
luaclie.COLOR.YELLOW2 = "\27[33;2m"
luaclie.COLOR.BLUE = "\27[34;1m"
luaclie.COLOR.BLUE0 = "\27[94;2m"
luaclie.COLOR.BLUE1 = "\27[34;3m"
luaclie.COLOR.BLUE2 = "\27[34;2m"
luaclie.COLOR.MAGENTA = "\27[35;1m"
luaclie.COLOR.MAGENTA0 = "\27[95;2m"
luaclie.COLOR.MAGENTA1 = "\27[35;3m"
luaclie.COLOR.MAGENTA2 = "\27[35;2m"
luaclie.COLOR.CYAN = "\27[36;1m"
luaclie.COLOR.CYAN0 = "\27[96;2m"
luaclie.COLOR.CYAN1 = "\27[36;3m"
luaclie.COLOR.CYAN2 = "\27[36;2m"
luaclie.COLOR.BOLD = "\27[1m"
luaclie.COLOR.BG = {}
luaclie.COLOR.BG.GRAY = "\27[47m"
luaclie.COLOR.BG.RED = "\27[41m"
luaclie.COLOR.BG.GREEN = "\27[42m"
luaclie.COLOR.BG.YELLOW = "\27[43m"
luaclie.COLOR.BG.BLUE = "\27[44m"
luaclie.COLOR.BG.MAGENTA = "\27[45m"
luaclie.COLOR.BG.CYAN = "\27[46m"
--D: Table Miner
--P: table_path : Table path string
--R: keys : Table with all keys on table
luaclie.tableminer = function (table_path)
local GetTableTypes = function (input_table)
local key_types = {}
for key, value in pairs(input_table) do
key_types[key] = type(value)
end
return key_types
end
local AddTypeSuffixToKeyNames = function (key_types)
local keys = {}
for name, type in pairs(key_types) do
local key_name = name
if type == "function" then
key_name = key_name .. "("
elseif type == "table" then
key_name = key_name .. "."
end
table.insert(keys, key_name)
end
return keys
end
table_path = table_path and table_path or ""
local current_table = _ENV
for table_name in string.gmatch(table_path, "[^%.^:]+[%.:]") do
if type(current_table) ~= "table" then
return {}
end
current_table = current_table[string.match(table_name, "[^%.^:]+")]
end
if type(current_table) ~= "table" then
return {}
end
local key_types = {}
local metatable = getmetatable(current_table)
if metatable and type(metatable.__index) == "table" then
key_types = GetTableTypes(metatable.__index)
end
for key, type in pairs(GetTableTypes(current_table)) do
key_types[key] = type
end
return AddTypeSuffixToKeyNames(key_types)
end
--D: Function Reference
--P: text : Input text with command line
luaclie.funcref = function (text)
local current_table = _ENV
local table_name
local last_func_name = nil
local comment_table = {}
local func_info
local maxlen = 0
local currlen
print()
-- Get function name with table path
for func_name in string.gmatch(text, "[%a%d%.:_]+%(") do
last_func_name = func_name
end
if last_func_name == nil then
print("no function found in \"" .. text .. "\"")
return
end
-- Travel thought tables until function
for func_name in string.gmatch(last_func_name, "[^%.^:^%(]+[%.:%(]") do
if current_table == nil then
print("table \"" .. table_name .. "\" is a nil value")
return
end
table_name = string.match(func_name, "[^%.^:^%(]+")
current_table = current_table[table_name]
end
-- Error: no table
if current_table == nil then
print("function \"" .. table_name .. "\" is a nil value")
return
end
func_info = debug.getinfo(current_table)
-- Error: defined in user interface
if func_info["short_src"] == "stdin" then
print("function defined in user interface, it has no description")
return
elseif func_info["short_src"] == "[C]" then
print("function defined in C code, it has no description")
return
end
local file = io.open(func_info["short_src"])
-- Error: no such file
if file == nil then
print("error: no such file: " .. func_info["short_src"])
return
end
-- Search comments above function
local counter = func_info["linedefined"]-1;
for line in file:lines() do
if string.match(line, "^[%s\t]*%-%-") ~= nil then
table.insert(comment_table, string.match(line, "^[%s\t]*(%-%-.*)"))
else
comment_table = {}
end
counter = counter-1
if counter == 0 then
break
end
end
-- Get maximum string length from comment
for _, line in pairs(comment_table) do
currlen = string.len(line)
if currlen > maxlen then
maxlen = currlen
end
end
if maxlen >= 80 then
maxlen = 80
end
-- Print separator
luaclie.printf("\27[30;4;1m")
for i = 1, maxlen + 18 do
luaclie.printf("-")
end
luaclie.printf("\27[0m\n")
-- Print help text
print("\27[33;1mFunction:\27[0m ")
print(" " .. last_func_name)
print("\27[33;1mReference:\27[0m")
for _, line in pairs(comment_table) do
print(" " .. line)
end
-- Print separator
luaclie.printf("\27[30;4;1m")
for i = 1, maxlen + 18 do
luaclie.printf("-")
end
luaclie.printf("\27[0m\n")
end
--D: C-like printf
luaclie.printf = function (string, ...)
io.write(string.format(string, ...))
end
--D: printf with color
luaclie.printc = function (color, string, ...)
luaclie.printf(color)
luaclie.printf(string, ...)
luaclie.printf(luaclie.COLOR.DEFAULT)
end
--D: Clear screen
luaclie.cls = function ()
os.execute("clear")
end
return luaclie