Asa@0: --- AceConfigCmd-3.0 handles access to an options table through the "command line" interface via the ChatFrames. Asa@0: -- @class file Asa@0: -- @name AceConfigCmd-3.0 Asa@0: -- @release $Id: AceConfigCmd-3.0.lua 904 2009-12-13 11:56:37Z nevcairiel $ Asa@0: Asa@0: --[[ Asa@0: AceConfigCmd-3.0 Asa@0: Asa@0: Handles commandline optionstable access Asa@0: Asa@0: REQUIRES: AceConsole-3.0 for command registration (loaded on demand) Asa@0: Asa@0: ]] Asa@0: Asa@0: -- TODO: plugin args Asa@0: Asa@0: Asa@0: local MAJOR, MINOR = "AceConfigCmd-3.0", 12 Asa@0: local AceConfigCmd = LibStub:NewLibrary(MAJOR, MINOR) Asa@0: Asa@0: if not AceConfigCmd then return end Asa@0: Asa@0: AceConfigCmd.commands = AceConfigCmd.commands or {} Asa@0: local commands = AceConfigCmd.commands Asa@0: Asa@0: local cfgreg = LibStub("AceConfigRegistry-3.0") Asa@0: local AceConsole -- LoD Asa@0: local AceConsoleName = "AceConsole-3.0" Asa@0: Asa@0: -- Lua APIs Asa@0: local strsub, strsplit, strlower, strmatch, strtrim = string.sub, string.split, string.lower, string.match, string.trim Asa@0: local format, tonumber, tostring = string.format, tonumber, tostring Asa@0: local tsort, tinsert = table.sort, table.insert Asa@0: local select, pairs, next, type = select, pairs, next, type Asa@0: local error, assert = error, assert Asa@0: Asa@0: -- WoW APIs Asa@0: local _G = _G Asa@0: Asa@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Asa@0: -- List them here for Mikk's FindGlobals script Asa@0: -- GLOBALS: LibStub, SELECTED_CHAT_FRAME, DEFAULT_CHAT_FRAME Asa@0: Asa@0: Asa@0: local L = setmetatable({}, { -- TODO: replace with proper locale Asa@0: __index = function(self,k) return k end Asa@0: }) Asa@0: Asa@0: Asa@0: Asa@0: local function print(msg) Asa@0: (SELECTED_CHAT_FRAME or DEFAULT_CHAT_FRAME):AddMessage(msg) Asa@0: end Asa@0: Asa@0: -- constants used by getparam() calls below Asa@0: Asa@0: local handlertypes = {["table"]=true} Asa@0: local handlermsg = "expected a table" Asa@0: Asa@0: local functypes = {["function"]=true, ["string"]=true} Asa@0: local funcmsg = "expected function or member name" Asa@0: Asa@0: Asa@0: -- pickfirstset() - picks the first non-nil value and returns it Asa@0: Asa@0: local function pickfirstset(...) Asa@0: for i=1,select("#",...) do Asa@0: if select(i,...)~=nil then Asa@0: return select(i,...) Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: Asa@0: -- err() - produce real error() regarding malformed options tables etc Asa@0: Asa@0: local function err(info,inputpos,msg ) Asa@0: local cmdstr=" "..strsub(info.input, 1, inputpos-1) Asa@0: error(MAJOR..": /" ..info[0] ..cmdstr ..": "..(msg or "malformed options table"), 2) Asa@0: end Asa@0: Asa@0: Asa@0: -- usererr() - produce chatframe message regarding bad slash syntax etc Asa@0: Asa@0: local function usererr(info,inputpos,msg ) Asa@0: local cmdstr=strsub(info.input, 1, inputpos-1); Asa@0: print("/" ..info[0] .. " "..cmdstr ..": "..(msg or "malformed options table")) Asa@0: end Asa@0: Asa@0: Asa@0: -- callmethod() - call a given named method (e.g. "get", "set") with given arguments Asa@0: Asa@0: local function callmethod(info, inputpos, tab, methodtype, ...) Asa@0: local method = info[methodtype] Asa@0: if not method then Asa@0: err(info, inputpos, "'"..methodtype.."': not set") Asa@0: end Asa@0: Asa@0: info.arg = tab.arg Asa@0: info.option = tab Asa@0: info.type = tab.type Asa@0: Asa@0: if type(method)=="function" then Asa@0: return method(info, ...) Asa@0: elseif type(method)=="string" then Asa@0: if type(info.handler[method])~="function" then Asa@0: err(info, inputpos, "'"..methodtype.."': '"..method.."' is not a member function of "..tostring(info.handler)) Asa@0: end Asa@0: return info.handler[method](info.handler, info, ...) Asa@0: else Asa@0: assert(false) -- type should have already been checked on read Asa@0: end Asa@0: end Asa@0: Asa@0: -- callfunction() - call a given named function (e.g. "name", "desc") with given arguments Asa@0: Asa@0: local function callfunction(info, tab, methodtype, ...) Asa@0: local method = tab[methodtype] Asa@0: Asa@0: info.arg = tab.arg Asa@0: info.option = tab Asa@0: info.type = tab.type Asa@0: Asa@0: if type(method)=="function" then Asa@0: return method(info, ...) Asa@0: else Asa@0: assert(false) -- type should have already been checked on read Asa@0: end Asa@0: end Asa@0: Asa@0: -- do_final() - do the final step (set/execute) along with validation and confirmation Asa@0: Asa@0: local function do_final(info, inputpos, tab, methodtype, ...) Asa@0: if info.validate then Asa@0: local res = callmethod(info,inputpos,tab,"validate",...) Asa@0: if type(res)=="string" then Asa@0: usererr(info, inputpos, "'"..strsub(info.input, inputpos).."' - "..res) Asa@0: return Asa@0: end Asa@0: end Asa@0: -- console ignores .confirm Asa@0: Asa@0: callmethod(info,inputpos,tab,methodtype, ...) Asa@0: end Asa@0: Asa@0: Asa@0: -- getparam() - used by handle() to retreive and store "handler", "get", "set", etc Asa@0: Asa@0: local function getparam(info, inputpos, tab, depth, paramname, types, errormsg) Asa@0: local old,oldat = info[paramname], info[paramname.."_at"] Asa@0: local val=tab[paramname] Asa@0: if val~=nil then Asa@0: if val==false then Asa@0: val=nil Asa@0: elseif not types[type(val)] then Asa@0: err(info, inputpos, "'" .. paramname.. "' - "..errormsg) Asa@0: end Asa@0: info[paramname] = val Asa@0: info[paramname.."_at"] = depth Asa@0: end Asa@0: return old,oldat Asa@0: end Asa@0: Asa@0: Asa@0: -- iterateargs(tab) - custom iterator that iterates both t.args and t.plugins.* Asa@0: local dummytable={} Asa@0: Asa@0: local function iterateargs(tab) Asa@0: if not tab.plugins then Asa@0: return pairs(tab.args) Asa@0: end Asa@0: Asa@0: local argtabkey,argtab=next(tab.plugins) Asa@0: local v Asa@0: Asa@0: return function(_, k) Asa@0: while argtab do Asa@0: k,v = next(argtab, k) Asa@0: if k then return k,v end Asa@0: if argtab==tab.args then Asa@0: argtab=nil Asa@0: else Asa@0: argtabkey,argtab = next(tab.plugins, argtabkey) Asa@0: if not argtabkey then Asa@0: argtab=tab.args Asa@0: end Asa@0: end Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: local function checkhidden(info, inputpos, tab) Asa@0: if tab.cmdHidden~=nil then Asa@0: return tab.cmdHidden Asa@0: end Asa@0: local hidden = tab.hidden Asa@0: if type(hidden) == "function" or type(hidden) == "string" then Asa@0: info.hidden = hidden Asa@0: hidden = callmethod(info, inputpos, tab, 'hidden') Asa@0: info.hidden = nil Asa@0: end Asa@0: return hidden Asa@0: end Asa@0: Asa@0: local function showhelp(info, inputpos, tab, depth, noHead) Asa@0: if not noHead then Asa@0: print("|cff33ff99"..info.appName.."|r: Arguments to |cffffff78/"..info[0].."|r "..strsub(info.input,1,inputpos-1)..":") Asa@0: end Asa@0: Asa@0: local sortTbl = {} -- [1..n]=name Asa@0: local refTbl = {} -- [name]=tableref Asa@0: Asa@0: for k,v in iterateargs(tab) do Asa@0: if not refTbl[k] then -- a plugin overriding something in .args Asa@0: tinsert(sortTbl, k) Asa@0: refTbl[k] = v Asa@0: end Asa@0: end Asa@0: Asa@0: tsort(sortTbl, function(one, two) Asa@0: local o1 = refTbl[one].order or 100 Asa@0: local o2 = refTbl[two].order or 100 Asa@0: if type(o1) == "function" or type(o1) == "string" then Asa@0: info.order = o1 Asa@0: info[#info+1] = one Asa@0: o1 = callmethod(info, inputpos, refTbl[one], "order") Asa@0: info[#info] = nil Asa@0: info.order = nil Asa@0: end Asa@0: if type(o2) == "function" or type(o1) == "string" then Asa@0: info.order = o2 Asa@0: info[#info+1] = two Asa@0: o2 = callmethod(info, inputpos, refTbl[two], "order") Asa@0: info[#info] = nil Asa@0: info.order = nil Asa@0: end Asa@0: if o1<0 and o2<0 then return o1 4) and not _G["KEY_" .. text] then Asa@0: return false Asa@0: end Asa@0: local s = text Asa@0: if shift then Asa@0: s = "SHIFT-" .. s Asa@0: end Asa@0: if ctrl then Asa@0: s = "CTRL-" .. s Asa@0: end Asa@0: if alt then Asa@0: s = "ALT-" .. s Asa@0: end Asa@0: return s Asa@0: end Asa@0: Asa@0: -- handle() - selfrecursing function that processes input->optiontable Asa@0: -- - depth - starts at 0 Asa@0: -- - retfalse - return false rather than produce error if a match is not found (used by inlined groups) Asa@0: Asa@0: local function handle(info, inputpos, tab, depth, retfalse) Asa@0: Asa@0: if not(type(tab)=="table" and type(tab.type)=="string") then err(info,inputpos) end Asa@0: Asa@0: ------------------------------------------------------------------- Asa@0: -- Grab hold of handler,set,get,func,etc if set (and remember old ones) Asa@0: -- Note that we do NOT validate if method names are correct at this stage, Asa@0: -- the handler may change before they're actually used! Asa@0: Asa@0: local oldhandler,oldhandler_at = getparam(info,inputpos,tab,depth,"handler",handlertypes,handlermsg) Asa@0: local oldset,oldset_at = getparam(info,inputpos,tab,depth,"set",functypes,funcmsg) Asa@0: local oldget,oldget_at = getparam(info,inputpos,tab,depth,"get",functypes,funcmsg) Asa@0: local oldfunc,oldfunc_at = getparam(info,inputpos,tab,depth,"func",functypes,funcmsg) Asa@0: local oldvalidate,oldvalidate_at = getparam(info,inputpos,tab,depth,"validate",functypes,funcmsg) Asa@0: --local oldconfirm,oldconfirm_at = getparam(info,inputpos,tab,depth,"confirm",functypes,funcmsg) Asa@0: Asa@0: ------------------------------------------------------------------- Asa@0: -- Act according to .type of this table Asa@0: Asa@0: if tab.type=="group" then Asa@0: ------------ group -------------------------------------------- Asa@0: Asa@0: if type(tab.args)~="table" then err(info, inputpos) end Asa@0: if tab.plugins and type(tab.plugins)~="table" then err(info,inputpos) end Asa@0: Asa@0: -- grab next arg from input Asa@0: local _,nextpos,arg = (info.input):find(" *([^ ]+) *", inputpos) Asa@0: if not arg then Asa@0: showhelp(info, inputpos, tab, depth) Asa@0: return Asa@0: end Asa@0: nextpos=nextpos+1 Asa@0: Asa@0: -- loop .args and try to find a key with a matching name Asa@0: for k,v in iterateargs(tab) do Asa@0: if not(type(k)=="string" and type(v)=="table" and type(v.type)=="string") then err(info,inputpos, "options table child '"..tostring(k).."' is malformed") end Asa@0: Asa@0: -- is this child an inline group? if so, traverse into it Asa@0: if v.type=="group" and pickfirstset(v.cmdInline, v.inline, false) then Asa@0: info[depth+1] = k Asa@0: if handle(info, inputpos, v, depth+1, true)==false then Asa@0: info[depth+1] = nil Asa@0: -- wasn't found in there, but that's ok, we just keep looking down here Asa@0: else Asa@0: return -- done, name was found in inline group Asa@0: end Asa@0: -- matching name and not a inline group Asa@0: elseif strlower(arg)==strlower(k:gsub(" ", "_")) then Asa@0: info[depth+1] = k Asa@0: return handle(info,nextpos,v,depth+1) Asa@0: end Asa@0: end Asa@0: Asa@0: -- no match Asa@0: if retfalse then Asa@0: -- restore old infotable members and return false to indicate failure Asa@0: info.handler,info.handler_at = oldhandler,oldhandler_at Asa@0: info.set,info.set_at = oldset,oldset_at Asa@0: info.get,info.get_at = oldget,oldget_at Asa@0: info.func,info.func_at = oldfunc,oldfunc_at Asa@0: info.validate,info.validate_at = oldvalidate,oldvalidate_at Asa@0: --info.confirm,info.confirm_at = oldconfirm,oldconfirm_at Asa@0: return false Asa@0: end Asa@0: Asa@0: -- couldn't find the command, display error Asa@0: usererr(info, inputpos, "'"..arg.."' - " .. L["unknown argument"]) Asa@0: return Asa@0: end Asa@0: Asa@0: local str = strsub(info.input,inputpos); Asa@0: Asa@0: if tab.type=="execute" then Asa@0: ------------ execute -------------------------------------------- Asa@0: do_final(info, inputpos, tab, "func") Asa@0: Asa@0: Asa@0: Asa@0: elseif tab.type=="input" then Asa@0: ------------ input -------------------------------------------- Asa@0: Asa@0: local res = true Asa@0: if tab.pattern then Asa@0: if not(type(tab.pattern)=="string") then err(info, inputpos, "'pattern' - expected a string") end Asa@0: if not strmatch(str, tab.pattern) then Asa@0: usererr(info, inputpos, "'"..str.."' - " .. L["invalid input"]) Asa@0: return Asa@0: end Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", str) Asa@0: Asa@0: Asa@0: Asa@0: elseif tab.type=="toggle" then Asa@0: ------------ toggle -------------------------------------------- Asa@0: local b Asa@0: local str = strtrim(strlower(str)) Asa@0: if str=="" then Asa@0: b = callmethod(info, inputpos, tab, "get") Asa@0: Asa@0: if tab.tristate then Asa@0: --cycle in true, nil, false order Asa@0: if b then Asa@0: b = nil Asa@0: elseif b == nil then Asa@0: b = false Asa@0: else Asa@0: b = true Asa@0: end Asa@0: else Asa@0: b = not b Asa@0: end Asa@0: Asa@0: elseif str==L["on"] then Asa@0: b = true Asa@0: elseif str==L["off"] then Asa@0: b = false Asa@0: elseif tab.tristate and str==L["default"] then Asa@0: b = nil Asa@0: else Asa@0: if tab.tristate then Asa@0: usererr(info, inputpos, format(L["'%s' - expected 'on', 'off' or 'default', or no argument to toggle."], str)) Asa@0: else Asa@0: usererr(info, inputpos, format(L["'%s' - expected 'on' or 'off', or no argument to toggle."], str)) Asa@0: end Asa@0: return Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", b) Asa@0: Asa@0: Asa@0: elseif tab.type=="range" then Asa@0: ------------ range -------------------------------------------- Asa@0: local val = tonumber(str) Asa@0: if not val then Asa@0: usererr(info, inputpos, "'"..str.."' - "..L["expected number"]) Asa@0: return Asa@0: end Asa@0: if type(info.step)=="number" then Asa@0: val = val- (val % info.step) Asa@0: end Asa@0: if type(info.min)=="number" and valinfo.max then Asa@0: usererr(info, inputpos, val.." - "..format(L["must be equal to or lower than %s"], tostring(info.max)) ) Asa@0: return Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", val) Asa@0: Asa@0: Asa@0: elseif tab.type=="select" then Asa@0: ------------ select ------------------------------------ Asa@0: local str = strtrim(strlower(str)) Asa@0: Asa@0: local values = tab.values Asa@0: if type(values) == "function" or type(values) == "string" then Asa@0: info.values = values Asa@0: values = callmethod(info, inputpos, tab, "values") Asa@0: info.values = nil Asa@0: end Asa@0: Asa@0: if str == "" then Asa@0: local b = callmethod(info, inputpos, tab, "get") Asa@0: local fmt = "|cffffff78- [%s]|r %s" Asa@0: local fmt_sel = "|cffffff78- [%s]|r %s |cffff0000*|r" Asa@0: print(L["Options for |cffffff78"..info[#info].."|r:"]) Asa@0: for k, v in pairs(values) do Asa@0: if b == k then Asa@0: print(fmt_sel:format(k, v)) Asa@0: else Asa@0: print(fmt:format(k, v)) Asa@0: end Asa@0: end Asa@0: return Asa@0: end Asa@0: Asa@0: local ok Asa@0: for k,v in pairs(values) do Asa@0: if strlower(k)==str then Asa@0: str = k -- overwrite with key (in case of case mismatches) Asa@0: ok = true Asa@0: break Asa@0: end Asa@0: end Asa@0: if not ok then Asa@0: usererr(info, inputpos, "'"..str.."' - "..L["unknown selection"]) Asa@0: return Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", str) Asa@0: Asa@0: elseif tab.type=="multiselect" then Asa@0: ------------ multiselect ------------------------------------------- Asa@0: local str = strtrim(strlower(str)) Asa@0: Asa@0: local values = tab.values Asa@0: if type(values) == "function" or type(values) == "string" then Asa@0: info.values = values Asa@0: values = callmethod(info, inputpos, tab, "values") Asa@0: info.values = nil Asa@0: end Asa@0: Asa@0: if str == "" then Asa@0: local fmt = "|cffffff78- [%s]|r %s" Asa@0: local fmt_sel = "|cffffff78- [%s]|r %s |cffff0000*|r" Asa@0: print(L["Options for |cffffff78"..info[#info].."|r (multiple possible):"]) Asa@0: for k, v in pairs(values) do Asa@0: if callmethod(info, inputpos, tab, "get", k) then Asa@0: print(fmt_sel:format(k, v)) Asa@0: else Asa@0: print(fmt:format(k, v)) Asa@0: end Asa@0: end Asa@0: return Asa@0: end Asa@0: Asa@0: --build a table of the selections, checking that they exist Asa@0: --parse for =on =off =default in the process Asa@0: --table will be key = true for options that should toggle, key = [on|off|default] for options to be set Asa@0: local sels = {} Asa@0: for v in str:gmatch("[^ ]+") do Asa@0: --parse option=on etc Asa@0: local opt, val = v:match('(.+)=(.+)') Asa@0: --get option if toggling Asa@0: if not opt then Asa@0: opt = v Asa@0: end Asa@0: Asa@0: --check that the opt is valid Asa@0: local ok Asa@0: for k,v in pairs(values) do Asa@0: if strlower(k)==opt then Asa@0: opt = k -- overwrite with key (in case of case mismatches) Asa@0: ok = true Asa@0: break Asa@0: end Asa@0: end Asa@0: Asa@0: if not ok then Asa@0: usererr(info, inputpos, "'"..opt.."' - "..L["unknown selection"]) Asa@0: return Asa@0: end Asa@0: Asa@0: --check that if val was supplied it is valid Asa@0: if val then Asa@0: if val == L["on"] or val == L["off"] or (tab.tristate and val == L["default"]) then Asa@0: --val is valid insert it Asa@0: sels[opt] = val Asa@0: else Asa@0: if tab.tristate then Asa@0: usererr(info, inputpos, format(L["'%s' '%s' - expected 'on', 'off' or 'default', or no argument to toggle."], v, val)) Asa@0: else Asa@0: usererr(info, inputpos, format(L["'%s' '%s' - expected 'on' or 'off', or no argument to toggle."], v, val)) Asa@0: end Asa@0: return Asa@0: end Asa@0: else Asa@0: -- no val supplied, toggle Asa@0: sels[opt] = true Asa@0: end Asa@0: end Asa@0: Asa@0: for opt, val in pairs(sels) do Asa@0: local newval Asa@0: Asa@0: if (val == true) then Asa@0: --toggle the option Asa@0: local b = callmethod(info, inputpos, tab, "get", opt) Asa@0: Asa@0: if tab.tristate then Asa@0: --cycle in true, nil, false order Asa@0: if b then Asa@0: b = nil Asa@0: elseif b == nil then Asa@0: b = false Asa@0: else Asa@0: b = true Asa@0: end Asa@0: else Asa@0: b = not b Asa@0: end Asa@0: newval = b Asa@0: else Asa@0: --set the option as specified Asa@0: if val==L["on"] then Asa@0: newval = true Asa@0: elseif val==L["off"] then Asa@0: newval = false Asa@0: elseif val==L["default"] then Asa@0: newval = nil Asa@0: end Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", opt, newval) Asa@0: end Asa@0: Asa@0: Asa@0: elseif tab.type=="color" then Asa@0: ------------ color -------------------------------------------- Asa@0: local str = strtrim(strlower(str)) Asa@0: if str == "" then Asa@0: --TODO: Show current value Asa@0: return Asa@0: end Asa@0: Asa@0: local r, g, b, a Asa@0: Asa@0: if tab.hasAlpha then Asa@0: if str:len() == 8 and str:find("^%x*$") then Asa@0: --parse a hex string Asa@0: r,g,b,a = tonumber(str:sub(1, 2), 16) / 255, tonumber(str:sub(3, 4), 16) / 255, tonumber(str:sub(5, 6), 16) / 255, tonumber(str:sub(7, 8), 16) / 255 Asa@0: else Asa@0: --parse seperate values Asa@0: r,g,b,a = str:match("^([%d%.]+) ([%d%.]+) ([%d%.]+) ([%d%.]+)$") Asa@0: r,g,b,a = tonumber(r), tonumber(g), tonumber(b), tonumber(a) Asa@0: end Asa@0: if not (r and g and b and a) then Asa@0: usererr(info, inputpos, format(L["'%s' - expected 'RRGGBBAA' or 'r g b a'."], str)) Asa@0: return Asa@0: end Asa@0: Asa@0: if r >= 0.0 and r <= 1.0 and g >= 0.0 and g <= 1.0 and b >= 0.0 and b <= 1.0 and a >= 0.0 and a <= 1.0 then Asa@0: --values are valid Asa@0: elseif r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255 and a >= 0 and a <= 255 then Asa@0: --values are valid 0..255, convert to 0..1 Asa@0: r = r / 255 Asa@0: g = g / 255 Asa@0: b = b / 255 Asa@0: a = a / 255 Asa@0: else Asa@0: --values are invalid Asa@0: usererr(info, inputpos, format(L["'%s' - values must all be either in the range 0..1 or 0..255."], str)) Asa@0: end Asa@0: else Asa@0: a = 1.0 Asa@0: if str:len() == 6 and str:find("^%x*$") then Asa@0: --parse a hex string Asa@0: r,g,b = tonumber(str:sub(1, 2), 16) / 255, tonumber(str:sub(3, 4), 16) / 255, tonumber(str:sub(5, 6), 16) / 255 Asa@0: else Asa@0: --parse seperate values Asa@0: r,g,b = str:match("^([%d%.]+) ([%d%.]+) ([%d%.]+)$") Asa@0: r,g,b = tonumber(r), tonumber(g), tonumber(b) Asa@0: end Asa@0: if not (r and g and b) then Asa@0: usererr(info, inputpos, format(L["'%s' - expected 'RRGGBB' or 'r g b'."], str)) Asa@0: return Asa@0: end Asa@0: if r >= 0.0 and r <= 1.0 and g >= 0.0 and g <= 1.0 and b >= 0.0 and b <= 1.0 then Asa@0: --values are valid Asa@0: elseif r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255 then Asa@0: --values are valid 0..255, convert to 0..1 Asa@0: r = r / 255 Asa@0: g = g / 255 Asa@0: b = b / 255 Asa@0: else Asa@0: --values are invalid Asa@0: usererr(info, inputpos, format(L["'%s' - values must all be either in the range 0-1 or 0-255."], str)) Asa@0: end Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", r,g,b,a) Asa@0: Asa@0: elseif tab.type=="keybinding" then Asa@0: ------------ keybinding -------------------------------------------- Asa@0: local str = strtrim(strlower(str)) Asa@0: if str == "" then Asa@0: --TODO: Show current value Asa@0: return Asa@0: end Asa@0: local value = keybindingValidateFunc(str:upper()) Asa@0: if value == false then Asa@0: usererr(info, inputpos, format(L["'%s' - Invalid Keybinding."], str)) Asa@0: return Asa@0: end Asa@0: Asa@0: do_final(info, inputpos, tab, "set", value) Asa@0: Asa@0: elseif tab.type=="description" then Asa@0: ------------ description -------------------- Asa@0: -- ignore description, GUI config only Asa@0: else Asa@0: err(info, inputpos, "unknown options table item type '"..tostring(tab.type).."'") Asa@0: end Asa@0: end Asa@0: Asa@0: --- Handle the chat command. Asa@0: -- This is usually called from a chat command handler to parse the command input as operations on an aceoptions table.\\ Asa@0: -- AceConfigCmd uses this function internally when a slash command is registered with `:CreateChatCommand` Asa@0: -- @param slashcmd The slash command WITHOUT leading slash (only used for error output) Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: -- @param input The commandline input (as given by the WoW handler, i.e. without the command itself) Asa@0: -- @usage Asa@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceConsole-3.0") Asa@0: -- -- Use AceConsole-3.0 to register a Chat Command Asa@0: -- MyAddon:RegisterChatCommand("mychat", "ChatCommand") Asa@0: -- Asa@0: -- -- Show the GUI if no input is supplied, otherwise handle the chat input. Asa@0: -- function MyAddon:ChatCommand(input) Asa@0: -- -- Assuming "MyOptions" is the appName of a valid options table Asa@0: -- if not input or input:trim() == "" then Asa@0: -- LibStub("AceConfigDialog-3.0"):Open("MyOptions") Asa@0: -- else Asa@0: -- LibStub("AceConfigCmd-3.0").HandleCommand(MyAddon, "mychat", "MyOptions", input) Asa@0: -- end Asa@0: -- end Asa@0: function AceConfigCmd:HandleCommand(slashcmd, appName, input) Asa@0: Asa@0: local optgetter = cfgreg:GetOptionsTable(appName) Asa@0: if not optgetter then Asa@0: error([[Usage: HandleCommand("slashcmd", "appName", "input"): 'appName' - no options table "]]..tostring(appName)..[[" has been registered]], 2) Asa@0: end Asa@0: local options = assert( optgetter("cmd", MAJOR) ) Asa@0: Asa@0: local info = { -- Don't try to recycle this, it gets handed off to callbacks and whatnot Asa@0: [0] = slashcmd, Asa@0: appName = appName, Asa@0: options = options, Asa@0: input = input, Asa@0: self = self, Asa@0: handler = self, Asa@0: uiType = "cmd", Asa@0: uiName = MAJOR, Asa@0: } Asa@0: Asa@0: handle(info, 1, options, 0) -- (info, inputpos, table, depth) Asa@0: end Asa@0: Asa@0: --- Utility function to create a slash command handler. Asa@0: -- Also registers tab completion with AceTab Asa@0: -- @param slashcmd The slash command WITHOUT leading slash (only used for error output) Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: function AceConfigCmd:CreateChatCommand(slashcmd, appName) Asa@0: if not AceConsole then Asa@0: AceConsole = LibStub(AceConsoleName) Asa@0: end Asa@0: if AceConsole.RegisterChatCommand(self, slashcmd, function(input) Asa@0: AceConfigCmd.HandleCommand(self, slashcmd, appName, input) -- upgradable Asa@0: end, Asa@0: true) then -- succesfully registered so lets get the command -> app table in Asa@0: commands[slashcmd] = appName Asa@0: end Asa@0: end Asa@0: Asa@0: --- Utility function that returns the options table that belongs to a slashcommand. Asa@0: -- Designed to be used for the AceTab interface. Asa@0: -- @param slashcmd The slash command WITHOUT leading slash (only used for error output) Asa@0: -- @return The options table associated with the slash command (or nil if the slash command was not registered) Asa@0: function AceConfigCmd:GetChatCommandOptions(slashcmd) Asa@0: return commands[slashcmd] Asa@0: end