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