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