View Single Post
05/12/14, 12:47 PM   #1
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
Utility command to list/filter globals

Hey, I made some useful command that can help people with writing their addons. I thought I will share it with you. First the code, then examples:
Lua Code:
  1. local function pairs_safe_grep(table)
  2.     return function(arg,lastval)
  3.         local val = nil
  4.         repeat
  5.             repeat
  6.                 ok,lastval,v = pcall(next,arg.t,lastval)
  7.                 if ok == false then
  8.                     lastval = string.match(lastval, "private function '([%w_]+)' from" )
  9.                     if lastval == nil then
  10.                         break
  11.                     end
  12.                 end
  13.             until ok == true
  14.            
  15.             if lastval == nil then return nil end
  16.             val = rawget(arg.t, lastval)
  17.             local isend = false
  18.             if arg.k == nil or ( arg.k and string.match(lastval, arg.k) ) then
  19.                 isend = true
  20.             elseif arg.k then
  21.                 isend = false
  22.             end
  23.             if isend and ( arg.v == nil or ( arg.v and string.match(tostring(val), arg.v) ) ) then
  24.                 isend = true
  25.             elseif isend and arg.v then
  26.                 isend = false
  27.             end
  28.         until isend
  29.         return lastval, val
  30.     end,table,nil
  31. end
  32.  
  33. local function harven_dumpg(raw_args)
  34.     local args = {}
  35.     for arg in string.gmatch(raw_args, "|([:;'<,>/%[%]\\=_~`!@#&*%(%)%c%^%$%(%)%%%.%[%]%*%+%-%?%s%w]+)|") do
  36.         table.insert(args,arg)
  37.     end
  38.    
  39.     local i = 0
  40.     if args[1] == "nil" then
  41.         args[1] = nil
  42.     end
  43.     for k,v in pairs_safe_grep({t=_G,k=args[1],v=args[2]}) do
  44.         d(k.." = "..tostring(v))
  45.         i = i + 1
  46.     end
  47.     d("count: "..i)
  48. end
  49.  
  50. SLASH_COMMANDS["/dumpg"] = harven_dumpg
It will iterate over _G table with my custom iterator function pairs_safe_grep. It can dump all global variables or search based on lua patterns. You can specify a pattern for keys and a pattern for values. It will skip all private functions.

Usage:
Code:
/dumpg |key_pattern|value_pattern|
value_pattern is optional and so is key_pattern. But when you want to skip key_pattern and use only value_pattern then you must write nil in key_pattern.

Examples:
Code:
/dumpg |||
will dump all global variables

Code:
/dumpg |^EVENT||
will dump all available event types with their codes.

Code:
/dumpg |^EVENT_.*UPDATE$||
will dump all event that start with EVENT_ and end with UPDATE

Code:
/dumpg |nil|function|
will dump all functions

Code:
/dumpg |ZoFont|userdata|
will dump names of all fonts

Code:
/dumpg |nil|3|
will dump all variables with 3 in their value

Code:
/dumpg |nil|^3$|
will dump all variables with value == 3

Code:
/dumpg |^LOOT_TYPE||
will dump all loot types

Code:
/dumpg |^CT_|^5$|
will search for control type with value 5 (which appears to be CT_ROOT_WINDOW),

and so one.

I often use this command to find a name of interface element. For example I wanted to find the name of Split Stack window. So I opened the window, moved my mouse over it and executed command
Code:
/control
which is another useful command:
Lua Code:
  1. SLASH_COMMANDS["/control"] = function(...)
  2.     d(WINDOW_MANAGER:GetMouseOverControl())
  3. end
It says:
Code:
userdata: 3BE780C0
in my case (it will change after reloadui)

Then i just execute:
Code:
/dumpg |nil|3BE780C0|
and got:
Code:
ZO_StackSplitModalUnderlay = userdata: 3BE780C0
And that is all i needed, the window will be ZO_StackSplit. You can check if it really exists by typing:
Code:
/dumpg |^ZO_StackSplit$||

Last edited by Harven : 05/12/14 at 12:51 PM.
  Reply With Quote