vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | |
2 | -- Class creation |
||
3 | AceChatCmd = AceCoreClass:new() |
||
4 | -- Compatibility reference, deprecated use |
||
5 | AceChatCmdClass = AceChatCmd |
||
6 | |||
7 | -- Object constructor |
||
8 | function AceChatCmd:new(commands,options) |
||
9 | return ace:new(self,{_cmdList=commands,options=options}) |
||
10 | end |
||
11 | |||
12 | |||
13 | --[[--------------------------------------------------------------------------------- |
||
14 | Reporting Methods |
||
15 | ------------------------------------------------------------------------------------]] |
||
16 | |||
17 | function AceChatCmd:result(...) |
||
18 | ace:print(self.app.disabled and ACE_ADDON_STANDBY.." " or "", |
||
19 | format(ACE_CMD_RESULT, self.app.name, ace.concat(arg)) |
||
20 | ) |
||
21 | end |
||
22 | |||
23 | function AceChatCmd:msg(...) self:result(format(unpack(arg))) end |
||
24 | function AceChatCmd:error(...) self:result(ACE_CMD_ERROR.." ", format(unpack(arg))) end |
||
25 | function AceChatCmd:status(text, val, map) |
||
26 | if( map ) then val = map[val or 0] or val end |
||
27 | self:result(text, " ", ACE_TEXT_NOW_SET_TO, " ", |
||
28 | format(ACE_DISPLAY_OPTION, val or ACE_CMD_REPORT_NO_VAL) |
||
29 | ) |
||
30 | end |
||
31 | |||
32 | function AceChatCmd:report(hdr, def) |
||
33 | if( not def ) then def = hdr; hdr = nil end |
||
34 | ace:print(hdr or format(ACE_CMD_RESULT, |
||
35 | self.app.name.." "..ACE_CMD_REPORT_STATUS, |
||
36 | self.app.disabled and ACE_ADDON_STANDBY or "" |
||
37 | ) |
||
38 | ) |
||
39 | for _, ref in ipairs(def) do |
||
40 | local val |
||
41 | if( ref.map ) then val = ref.map[ref.val or 0] |
||
42 | else val = ref.val |
||
43 | end |
||
44 | ace:print( |
||
45 | (ref.indent |
||
46 | and (strrep(" ", strlen(ACE_CMD_REPORT_LINE_PRE)).. |
||
47 | strrep(ACE_CMD_REPORT_LINE_INDENT, ref.indent)) |
||
48 | or ACE_CMD_REPORT_LINE_PRE |
||
49 | ), |
||
50 | format(ACE_CMD_REPORT_LINE, ref.text, val or ACE_CMD_REPORT_NO_VAL) |
||
51 | ) |
||
52 | end |
||
53 | end |
||
54 | |||
55 | |||
56 | --[[--------------------------------------------------------------------------------- |
||
57 | Command Handling Methods |
||
58 | ------------------------------------------------------------------------------------]] |
||
59 | |||
60 | function AceChatCmd:FindCommand(cmd) |
||
61 | cmd = strlower(cmd) |
||
62 | for index in SlashCmdList do |
||
63 | local i, cmdString = 0 |
||
64 | repeat |
||
65 | i = i + 1 |
||
66 | cmdString = getglobal("SLASH_"..index..i) |
||
67 | if( strlower(cmdString or "") == cmd ) then return TRUE end |
||
68 | until( not cmdString ) |
||
69 | end |
||
70 | end |
||
71 | |||
72 | function AceChatCmd:Register(handler) |
||
73 | if( not self._cmdList ) then |
||
74 | error("Attempt to register command handler without defining commands.", 2) |
||
75 | end |
||
76 | if( self.registered ) then return end |
||
77 | |||
78 | local slashID = strupper(self.app.name).."_CMD" |
||
79 | |||
80 | SlashCmdList[slashID] = function(msg) self:ProcessCommand(msg) end |
||
81 | local i = 0 |
||
82 | for index, cmd in self._cmdList do |
||
83 | if( not self:FindCommand(cmd) ) then |
||
84 | if( not self.commands ) then self.commands = {} end |
||
85 | i = i + 1 |
||
86 | tinsert(self.commands, cmd) |
||
87 | setglobal("SLASH_"..slashID..i, cmd) |
||
88 | end |
||
89 | end |
||
90 | |||
91 | self.handler = handler or self |
||
92 | |||
93 | if( not self.options ) then self.options = {} end |
||
94 | |||
95 | if( self.handler.Report ) then |
||
96 | tinsert(self.options, 1, { |
||
97 | option = ACE_CMD_OPT_REPORT, |
||
98 | desc = ACE_CMD_OPT_REPORT_DESC, |
||
99 | method = "Report" |
||
100 | } |
||
101 | ) |
||
102 | end |
||
103 | |||
104 | if( self.app.Enable ) then |
||
105 | tinsert(self.options, 1, { |
||
106 | option = ACE_CMD_OPT_STANDBY, |
||
107 | desc = format(ACE_CMD_OPT_STANDBY_DESC, self.app.name), |
||
108 | method = "ToggleStandBy" |
||
109 | } |
||
110 | ) |
||
111 | end |
||
112 | |||
113 | tinsert(self.options, 1, {option = ACE_CMD_OPT_HELP, desc = ACE_CMD_OPT_HELP_DESC}) |
||
114 | |||
115 | -- Create a closure for printing option lines, since it's done so much |
||
116 | self.printOpt = function(l,t) ace:print(format(ACE_CMD_USAGE_OPT_DESC,l,t)) end |
||
117 | self.registered = TRUE |
||
118 | end |
||
119 | |||
120 | function AceChatCmd:ProcessCommand(msg) |
||
121 | msg = ace.trim(msg or "") |
||
122 | |||
123 | if( strfind(strlower(msg), ACE_CMD_OPT_HELP) == 1 ) then |
||
124 | self:DisplayAddonInfo(self.options) |
||
125 | return |
||
126 | elseif( msg ~= "" ) then |
||
127 | return self:ProcessOptions(self.options, msg, self.handler, self.handler.CommandHandler) |
||
128 | elseif( self.handler.CommandHandler ) then |
||
129 | self.handler.CommandHandler(self.handler, msg) |
||
130 | return |
||
131 | end |
||
132 | |||
133 | self:DisplayUsage() |
||
134 | end |
||
135 | |||
136 | function AceChatCmd:ProcessOptions(list, msg, handler, method, opttext) |
||
137 | local _, _, cmd = strfind(strlower(msg), "(%S+)") |
||
138 | local args = gsub(msg, cmd.."%s*", "") |
||
139 | |||
140 | local i = ace.TableFindByKeyValue(list, "option", cmd, TRUE) |
||
141 | if( i ) then |
||
142 | local opt = list[i] |
||
143 | if( opt.args and (args ~= "") and |
||
144 | self:ProcessOptions(opt.args, |
||
145 | args, |
||
146 | opt.handler or handler, |
||
147 | opt.method or method, |
||
148 | opttext and opttext.." "..opt.option or opt.option |
||
149 | ) |
||
150 | ) then |
||
151 | return TRUE |
||
152 | elseif( opt.method and ((args ~= "") or (not opt.input)) and |
||
153 | (strlower(args) ~= ACE_CMD_OPT_HELP) |
||
154 | ) then |
||
155 | handler = opt.handler or handler |
||
156 | handler[opt.method](handler, args) |
||
157 | return TRUE |
||
158 | elseif( opt.input and (args == "") or (opt.args and (not method)) or |
||
159 | (strlower(args) == ACE_CMD_OPT_HELP) |
||
160 | ) then |
||
161 | self:OptionHeader(opt, opttext) |
||
162 | self:DisplayArgUsage(opt) |
||
163 | return TRUE |
||
164 | end |
||
165 | elseif( ((cmd or "") ~= "") and (cmd ~= ACE_CMD_OPT_HELP) and (not method) ) then |
||
166 | self:result(format(ACE_CMD_OPT_INVALID, cmd)) |
||
167 | return TRUE |
||
168 | end |
||
169 | end |
||
170 | |||
171 | |||
172 | --[[--------------------------------------------------------------------------------- |
||
173 | Usage Display Methods |
||
174 | ------------------------------------------------------------------------------------]] |
||
175 | |||
176 | function AceChatCmd:DisplayAddonInfo() |
||
177 | self:CommandHeader() |
||
178 | if( self.commands ) then |
||
179 | self.printOpt(ACE_TEXT_COMMANDS, ace.concat(self.commands, " | ")) |
||
180 | end |
||
181 | if( self.app.author ) then self.printOpt(ACE_TEXT_AUTHOR, self.app.author) end |
||
182 | if( self.app.releaseDate ) then self.printOpt(ACE_TEXT_RELEASED, self.app.releaseDate) end |
||
183 | if( self.app.email ) then self.printOpt(ACE_TEXT_EMAIL, self.app.email) end |
||
184 | if( self.app.website ) then self.printOpt(ACE_TEXT_WEBSITE, self.app.website) end |
||
185 | end |
||
186 | |||
187 | function AceChatCmd:OptionHeader(option, opttext) |
||
188 | local args |
||
189 | if( option.args ) then |
||
190 | args = self:ConcatOptions(option.args, ACE_CMD_USAGE_OPT_SEP) |
||
191 | end |
||
192 | if( not args ) then |
||
193 | args = "- "..(option.desc or ACE_CMD_USAGE_NOINFO) |
||
194 | end |
||
195 | ace:print(format(ACE_CMD_USAGE_OPTION, |
||
196 | self.commands[1], |
||
197 | opttext and opttext.." "..option.option or option.option, |
||
198 | args |
||
199 | ) |
||
200 | ) |
||
201 | end |
||
202 | |||
203 | function AceChatCmd:DisplayArgUsage(option) |
||
204 | if( not option.args ) then return end |
||
205 | for _, arg in ipairs(option.args) do |
||
206 | self.printOpt(arg.option, arg.desc) |
||
207 | end |
||
208 | end |
||
209 | |||
210 | function AceChatCmd:DisplayUsage() |
||
211 | self:CommandHeader() |
||
212 | |||
213 | if( getn(self.options) < 1 ) then return end |
||
214 | |||
215 | ace:print(format(ACE_CMD_USAGE_HEADER, |
||
216 | self.commands[1], |
||
217 | self:ConcatOptions(self.options, ACE_CMD_USAGE_OPT_SEP) |
||
218 | ) |
||
219 | ) |
||
220 | |||
221 | -- Have to print the lines one at a time or risk it getting too large for the |
||
222 | -- print buffer. |
||
223 | for _, opt in ipairs(self.options) do |
||
224 | self.printOpt(opt.option, opt.desc) |
||
225 | end |
||
226 | end |
||
227 | |||
228 | function AceChatCmd:CommandHeader() |
||
229 | ace:print(self.app.disabled and ACE_ADDON_STANDBY.." " or "", |
||
230 | format(ACE_CMD_USAGE_ADDON_DESC, |
||
231 | self.app.name, |
||
232 | self.app.version, |
||
233 | self.app.description |
||
234 | ) |
||
235 | ) |
||
236 | end |
||
237 | |||
238 | function AceChatCmd:ConcatOptions(options, sep) |
||
239 | local str = "" |
||
240 | for _, opt in options do |
||
241 | if( (str ~= "") and sep ) then str = str..sep end |
||
242 | str = str..opt.option |
||
243 | end |
||
244 | return str |
||
245 | end |