Quantcast

Slash cmd improved. Can search for fields py name pattern

Petr Grabovoy [06-02-16 - 01:08]
Slash cmd improved. Can search for fields py name pattern
Filename
README.md
ViragDevTool.lua
ViragDevTool.toc
ViragDevTool.xml
ViragDevToolMappings.lua
diff --git a/README.md b/README.md
index 94f9121..43cb916 100644
--- a/README.md
+++ b/README.md
@@ -47,7 +47,29 @@ Output:

 | Id(Row in list)   | Type          | Data Name  | Data Value  |
 | ----------------- | ------------- | ---------- | -----------------------|
-| 1                 | table         | \_G (number of objects in the table) | value   |
+
+### /CMD
+
+* **/vdt - toggle ui**
+* **/vdd <name>** - add variable to the list. will search in _G
+
+Can use . to select object in some other object
+
+For example:
+/vdt ViragDevTool.myvar will add myvar to the list if it exists
+
+* **/vdt find [someString] [optional parent]**
+
+will try to find every variable that has searched someString
+
+For example:
+/vdt find Frame will list all the objects in _G that have "Frame" in their name
+/vdt find Frame ViragDevTool will list  vars with Frames in their name but only for ViragDevTool
+
+* **/vdt startswith [someString] [optional parent]**
+
+Same as for "/vdt find" but will look only prefix of variable names
+find searches for \*someString\* and startswith searches for someString\*

 ### Other functionality
 * **Clicking on table name** will expand and show its children
diff --git a/ViragDevTool.lua b/ViragDevTool.lua
index a7e95a1..328210c 100644
--- a/ViragDevTool.lua
+++ b/ViragDevTool.lua
@@ -1,24 +1,47 @@
 -- create global instance
 ViragDevTool = {
+    --static constant useed for metatable name
     METATABLE_NAME = "$metatable",
-    tArgs = {}, -- stores arguments for cunction calls

+    --this 2 keyword are for cmd operations
+    -- you can use /vdt find somestr parentname(can be in format _G.Frame.Button)
+    -- for examle "/vdt find Virag" will find every variable in _G that has *Virag* pattern
+    -- "/vdt find Data ViragDevTool" will find every variable that has *Data* in their name in _G.ViragDevTool object if it exists
+    -- same for "startswith"
+    FIND_CMD_KEYWORD = "find",
+    STARTS_WITH_CMD_KEYWORD = "startswith",
+
+    -- stores arguments for fcunction calls --todo implement
+    tArgs = {},
+
+    -- mapping table is used to store searches and diferent values that are frequently used
+    -- for example we may need some api or some variable so we can add it here
+    mapping = {},
+
+    -- this variable will be used only on first load so it is just default init with empty values.
+    -- will be replaced with ViragDevTool_Settings at 2-nd start
     DEFAULT_SETTINGS = {
-        history = {}, -- stores history of recent calls to ViragDevTool_AddData
-        favourites = {} -- stores  saved vars for fust call
+        -- stores history of recent calls to /vdt
+        history = {},
+        favourites = {} --todo implement
     }
 }

 -- just remove global reference so it is easy to read with my ide
 local ViragDevTool = ViragDevTool
-local pairs, tostring, type, print, string, getmetatable, table, pcall = pairs, tostring, type, print, string, getmetatable, table, pcall
-local HybridScrollFrame_CreateButtons, HybridScrollFrame_GetOffset, HybridScrollFrame_Update = HybridScrollFrame_CreateButtons, HybridScrollFrame_GetOffset, HybridScrollFrame_Update
+
+local pairs, tostring, type, print, string, getmetatable, table, pcall =
+pairs, tostring, type, print, string, getmetatable, table, pcall
+
+local HybridScrollFrame_CreateButtons, HybridScrollFrame_GetOffset, HybridScrollFrame_Update =
+HybridScrollFrame_CreateButtons, HybridScrollFrame_GetOffset, HybridScrollFrame_Update

 -----------------------------------------------------------------------------------------------
 -- ViragDevTool_Colors == ViragDevTool.colors
 -----------------------------------------------------------------------------------------------

---todo refactore this
+-- todo refactore this class
+
 local ViragDevTool_Colors = {
     white = "|cFFFFFFFF",
     gray = "|cFFBEB9B5",
@@ -219,27 +242,60 @@ end
 function ViragDevTool:AddDataFromString(msg, bAddToHistory)
     if msg == "" then
         msg = "_G"
+
+    elseif #msg < 3 then
+        self:print(msg .. " - too short, need str of size 2+")
+        return
     end

-    local vars = string.split(msg, ".") or {}
+    local msgs = self.split(msg, " ")

-    local var = _G
-    for _, name in pairs(vars) do
-        if var then
-            var = var[name]
+    local resultTable
+
+    if #msgs > 1 then
+        -- got search and not normal _g[msg]
+        -- search can have find and prefix
+        local firstArg = msgs[1]
+        local secondArg = msgs[2]
+        local thirdArg = msgs[3]
+
+        local parent = _G
+
+        if thirdArg then parent = self:FromStrToObject(thirdArg) end
+
+        if string.lower(firstArg) == self.FIND_CMD_KEYWORD then
+            resultTable = self:FindIn(parent, secondArg, string.match)
+        elseif string.lower(firstArg) == self.STARTS_WITH_CMD_KEYWORD then
+            resultTable = self:FindIn(parent, secondArg, self.starts)
         end

+    else
+        resultTable = self:FromStrToObject(msg)
+        if not resultTable then
+            self:print("_G." .. msg .. " == nil, so can't add")
+        end
     end
-    if var  then
-    if bAddToHistory then
-        ViragDevTool:AddToHistory(msg)
+
+    if resultTable then
+        if bAddToHistory then
+            ViragDevTool:AddToHistory(msg)
+        end
+
+        ViragDevTool_AddData(resultTable, msg)
     end
+end

-    ViragDevTool_AddData(var, msg)
-    else
-        self:print("_G." .. msg .. " == nil, so can't add")
+function ViragDevTool:FromStrToObject(str)
+    local vars = self.split(str, ".") or {}
+
+    local var = _G
+    for _, name in pairs(vars) do
+        if var then
+            var = var[name]
+        end
     end

+    return var
 end


@@ -395,7 +451,8 @@ function ViragDevTool:UIUpdateMainTableButton(node, info, id)

         local function tablelength(T)
             local count = 0
-            for _ in pairs(T) do count = count + 1 end
+            for _ in pairs(T) do count = count + 1
+            end
             return count
         end

@@ -422,8 +479,11 @@ function ViragDevTool:UIUpdateMainTableButton(node, info, id)
     self:SetMainTableButtonScript(valueButton, info)
 end

+-----------------------------------------------------------------------------------------------
+-- Main table row button clicks setup
+-----------------------------------------------------------------------------------------------
 function ViragDevTool:SetMainTableButtonScript(button, info)
-    local valueType =  type(info.value)
+    local valueType = type(info.value)
     if valueType == "table" then
         button:SetScript("OnMouseUp", function(this, button, down)
             if info.expanded then
@@ -441,19 +501,6 @@ function ViragDevTool:SetMainTableButtonScript(button, info)
     end
 end

-
-function ViragDevTool:GetObjectTypeFromWoWAPI(value)
-    if value.GetObjectType and value.IsForbidden then
-        local ok, forbidden = pcall(value.IsForbidden, value)
-        if ok and not forbidden then
-            local ok, result = pcall(value.GetObjectType, value)
-            if ok then
-                return result
-            end
-        end
-    end
-end
-
 function ViragDevTool:TryCallFunction(info)
     -- info.value is just our function to call
     local parent, ok
@@ -509,7 +556,8 @@ function ViragDevTool:ProcessCallFunctionData(ok, info, parent, args, results)
     -- for example 1, 2, nil, 4 should return only this 4 values nothing more, nothing less.
     local found = false
     for i = 10, 1, -1 do
-        if results[i] ~= nil then found = true end
+        if results[i] ~= nil then found = true
+        end

         if found or i == 1 then -- if found some return or if return is nil
         nodes[i] = list:NewNode(results[i], string.format("  return: %d", i), padding)
@@ -540,7 +588,7 @@ function ViragDevTool:AddToHistory(strValue)
         local hist = self.settings.history
         table.insert(hist, 1, strValue)
         while #hist > 20 do -- can have only 10 values in history
-            table.remove(hist, 20)
+        table.remove(hist, 20)
         end
         self:UpdateSideBarUI()
     end
@@ -580,8 +628,12 @@ function ViragDevTool:OnLoad(mainFrame)

     -- register slash cmd
     SLASH_VIRAGDEVTOOLS1 = '/vdt';
-    function SlashCmdList.VIRAGDEVTOOLS(msg, editbox) -- 4.
-        self:AddDataFromString(msg, true)
+    function SlashCmdList.VIRAGDEVTOOLS(msg, editbox)
+        if msg == "" or msg == nil then
+            self:ToggleUI()
+        else
+            self:AddDataFromString(msg, true)
+        end
     end
 end

@@ -608,22 +660,33 @@ end
 -----------------------------------------------------------------------------------------------
 -- UTILS
 -----------------------------------------------------------------------------------------------
-
 function ViragDevTool:print(strText)
     print(self.colors.darkred .. "[Virag's DT]: " .. self.colors.white .. strText)
 end

 function ViragDevTool:shallowcopyargs(orig)
     local copy = {}
-    for k, v in pairs(orig) do copy[k] = orig[v] end
+    for k, v in pairs(orig) do copy[k] = orig[v]
+    end
     return copy
 end

-function string:split(sep)
+function ViragDevTool:split(sep)
     local sep, fields = sep or ".", {}
     local pattern = string.format("([^%s]+)", sep)
-    self:gsub(pattern, function(c) fields[#fields + 1] = c end)
+    self:gsub(pattern, function(c) fields[#fields + 1] = c
+    end)
     return fields
 end

-
+function ViragDevTool:GetObjectTypeFromWoWAPI(value)
+    if value.GetObjectType and value.IsForbidden then
+        local ok, forbidden = pcall(value.IsForbidden, value)
+        if ok and not forbidden then
+            local ok, result = pcall(value.GetObjectType, value)
+            if ok then
+                return result
+            end
+        end
+    end
+end
\ No newline at end of file
diff --git a/ViragDevTool.toc b/ViragDevTool.toc
index 38c6119..735b4fc 100644
--- a/ViragDevTool.toc
+++ b/ViragDevTool.toc
@@ -4,4 +4,5 @@
 ## Version: 0.1
 ## SavedVariables: ViragDevTool_Settings
 ViragDevTool.lua
+ViragDevToolMappings.lua
 ViragDevTool.xml
diff --git a/ViragDevTool.xml b/ViragDevTool.xml
index 742dc0a..1fd2361 100644
--- a/ViragDevTool.xml
+++ b/ViragDevTool.xml
@@ -264,7 +264,6 @@
                         <Scripts>
                             <OnClick>
                                 ViragDevTool_AddData(_G, "_G")
-                                ViragDevTool_AddData(ViragDevTool, "ViragDevTool")

                             </OnClick>
                         </Scripts>
@@ -272,17 +271,29 @@
                             <Anchor point="LEFT" relativeTo="$parentClearButton" relativePoint="RIGHT"/>
                         </Anchors>
                     </Button>
-                    <Button text="Unit" name="$parentAddPlayerInfoButton" inherits="ViragDevToolTopButton">
+                    <Button text="Mapping" name="$parentAddPlayerInfoButton" inherits="ViragDevToolTopButton">
                         <Scripts>
                             <OnClick>
-                                local name, realm = UnitFullName("player")
-                                ViragDevTool_AddData(UnitGUID("player"), name .. "-" .. realm)
+                                ViragDevTool_AddData(ViragDevTool.mapping, "mapping")
                             </OnClick>
                         </Scripts>
                         <Anchors>
                             <Anchor point="LEFT" relativeTo="$parentAddGlobalButton" relativePoint="RIGHT"/>
                         </Anchors>
                     </Button>
+                    <Button text="x" inherits="ViragDevToolTopButton" virtual="true">
+                        <Size>
+                            <AbsDimension x="25" y="25"/>
+                        </Size>
+                        <Anchors>
+                            <Anchor point="RIGHT" />
+                        </Anchors>
+                        <Scripts>
+                            <OnClick>
+                                ViragDevTool:ToggleUI()
+                            </OnClick>
+                        </Scripts>
+                    </Button>
                 </Frames>
             </Frame>
         </Frames>
diff --git a/ViragDevToolMappings.lua b/ViragDevToolMappings.lua
new file mode 100644
index 0000000..70b4617
--- /dev/null
+++ b/ViragDevToolMappings.lua
@@ -0,0 +1,38 @@
+local ViragDevTool = ViragDevTool
+
+--- this is just example demo how you can use this file to explore api.
+-- lets suppose we want to look into default api
+-- then we can add all variables manualy to some table and add tis table with ViragDevTool_AddData
+-- but we could create this table dinamicaly if we know prefix name
+function ViragDevTool:AddToMapping(strName, containsSearch)
+    local fn = containsSearch and string.match or self.starts
+    self.mapping[strName] = self:FindIn(_G, strName, fn)
+end
+
+function ViragDevTool:FindIn(parent, strName, fn)
+    local resultTable = {}
+
+    for k, v in pairs(parent) do
+        if fn(k, strName) then
+            resultTable[k] = v
+        end
+    end
+
+    return resultTable
+end
+
+function ViragDevTool.starts(String, Start)
+    return string.sub(String, 1, string.len(Start)) == Start
+end
+
+function ViragDevTool.ends(String, End)
+    return End == '' or string.sub(String, -string.len(End)) == End
+end
+
+
+--here or in any other place you can change mappings
+--ViragDevTool:AddToMapping("LFD")
+--ViragDevTool:AddToMapping("LFR")
+--ViragDevTool:AddToMapping("LFG")
+--ViragDevTool:AddToMapping("Virag")
+