Advertisement
draugath

table_viewer.lua

May 11th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.90 KB | None | 0 0
  1. -- Originally written by slime (c) 2007
  2. -- Heavily modified by draugath (c) 2010.
  3.  
  4. test.table_viewer     = {}
  5. test.table_viewer.ui  = {}
  6.  
  7. local tv              = test.table_viewer
  8. tv.failed             = false
  9. tv.history_size       = gkini.ReadInt('DevKit', 'tableviewer.history_size', 10)
  10. tv.history            = unspickle(gkini.ReadString('DevKit', 'tableviewer.history', '{}'))
  11. tv.treetable          = {}
  12. tv.table_ref          = {}
  13. tv.breadcrumbs        = {}
  14. tv.breadcrumbs.list   = {}
  15.  
  16. local ui              = tv.ui
  17. ui.dialog             = {}
  18.  
  19. local function build_table_name(tbl)
  20.     tbl = tbl or {}
  21.     local table_ref = tv.table_ref
  22.     local tree = ui.tree
  23.     local curid = tonumber(tree.curid)
  24.  
  25.     local function strip_desc(id)
  26.         local name, str = tree['NAME'..id]
  27.         if (type(table_ref[id]) == 'table') then
  28.             return (name:gsub('%(.*%)%s', ''))
  29.         elseif (ui.readiup.value == 'ON' and type(table_ref[id]) == 'userdata') then
  30.             str = name:gsub('%(.*%)%s', '') or name:gsub(':.*', '')
  31.             if (str == name) then return nil else return str end
  32.         else
  33.             return (name:gsub(':.*', ''))
  34.         end
  35.     end
  36.    
  37.     while (curid > 0) do
  38.         table.insert(tbl, 1, strip_desc(curid))
  39.         if (tbl[1] == nil) then return end
  40.         curid = tonumber(tree['PARENT'..curid])
  41.     end
  42.     for i, v in ipairs(test.splittable(tree['NAME0'])) do
  43.         table.insert(tbl, i, v)
  44.     end
  45.  
  46.     return test.jointable(tbl)
  47. end
  48.  
  49. function tv.breadcrumbs:New(name, path)
  50.     local button = iup.stationbutton{title=name, path=path}
  51.  
  52.     function button:action()
  53.         ui.textbox.value = self.path
  54.         ui.search:action()
  55.     end
  56.     return button
  57. end
  58.  
  59. function tv.ConvertTable(tbl_string)
  60.     local string_good, tbl = test.exists(tbl_string)
  61.     if (not string_good or not type(tbl)) then tv.failed = true; return false end
  62.     tv.treetable = {}
  63.     tv.table_ref = {}
  64.  
  65.     local function iup_recurse(in_control, insert_table)
  66.         local controls = {}
  67.         local cur_elem = in_control
  68.         if (cur_elem == ui.breadcrumbbox_inner_contents) then return end
  69.         repeat
  70.             table.insert(controls, cur_elem)
  71.             cur_elem = iup.GetBrother(cur_elem)
  72.         until (not cur_elem)
  73.  
  74.         for _,v in ipairs(controls) do
  75.             local iupType = iup.GetType(v)
  76.             table.insert(tv.table_ref, v)
  77.             if (test.IsIupContainer(v) or (iupType == 'list' and v['control'] == 'YES')) then
  78.                 table.insert(insert_table, {branchname=iupType})
  79.                 cur_elem = iup.GetNextChild(v)
  80.                 if (iupType == 'list') then test.print('list.nextchild='..tostring(pcall(iup.GetType, cur_elem))) end
  81.                 if (cur_elem) then iup_recurse(cur_elem, insert_table[#insert_table]) end
  82.             else
  83.                 table.insert(insert_table, iupType)
  84.             end
  85.         end
  86.     end
  87.  
  88.     local function recurse(intable, insert_table)
  89.         if (intable == tv.treetable or intable == tv.table_ref) then return end
  90.         local keys = {}
  91.         if (ui.readiup.value == 'ON' and
  92.             #tv.treetable == 0 and
  93.             type(intable) == 'userdata') then
  94.  
  95.             iup_recurse(intable, insert_table)
  96.  
  97.         elseif (type(intable) == 'table') then
  98.  
  99.             for k in pairs(intable) do table.insert(keys, k) end
  100.             table.sort(keys, function(a,b) return gkmisc.strnatcasecmp(a,b) < 0 end)
  101.  
  102.             for _,v in ipairs(keys) do
  103.                 if (ui[type(intable[v])].value == 'ON') then
  104.                     table.insert(tv.table_ref, intable[v])
  105.                     if type(intable[v]) == "table" then
  106.                         if (type(v) == 'number') then
  107.                             table.insert(insert_table, {branchname='(table) ['..tostring(v)..']'})
  108.                         else
  109.                             table.insert(insert_table, {branchname='(table) '..tostring(v)})
  110.                         end
  111.                         recurse(intable[v], insert_table[#insert_table])
  112.  
  113.                     elseif (ui.readiup.value == 'ON' and
  114.                             type(intable[v]) == 'userdata' and
  115.                             test.IsIupControl(intable[v])) then
  116.                         table.insert(insert_table, {branchname="(iup."..iup.GetType(intable[v])..") "..tostring(v)})
  117.                         iup_recurse(intable[v], insert_table[#insert_table])
  118.  
  119.                     else
  120.                         local val = intable[v]
  121.                         local str
  122.                         if type(val) == "string" then val = string.format("%q", val) else val = tostring(val) end
  123.                         if (type(v) == 'number') then
  124.                             str = '['..tostring(v)..']: '..val
  125.                         else
  126.                             str = tostring(v)..": "..val
  127.                         end
  128.                         table.insert(insert_table, str)
  129.                     end
  130.                 end
  131.             end
  132.  
  133.         else
  134.  
  135.             local val = intable
  136.             if type(val) == "string" then val = string.format("%q", val) else val = tostring(val) end
  137.             local str = val
  138.             table.insert(insert_table, str)
  139.  
  140.         end
  141.     end
  142.  
  143.     recurse(tbl, tv.treetable)
  144.     tv.treetable.branchname = tbl_string
  145.     return true
  146. end
  147.  
  148. function tv.breadcrumbs.create(tbl_string)
  149.    
  150.     local ibox = ui.breadcrumbbox_inner_contents
  151.     if (tv.breadcrumbs.list[1] == nil) then
  152.         tv.breadcrumbs.list = test.splittable(ui.textbox.value)
  153.     end
  154.     local bc = tv.breadcrumbs.list
  155.    
  156.  
  157.     iup.Detach(ibox)
  158.     iup.Destroy(ibox)
  159.     ibox = iup.hbox{gap = 5,}
  160.    
  161.     for i = 1, (#bc - 1) do
  162.        
  163.         iup.Append(ibox, tv.breadcrumbs:New(bc[i], test.jointable(bc, 1, i)))
  164.     end
  165.  
  166.     iup.Append(ui.breadcrumbbox_inner, ibox)
  167.     ui.breadcrumbbox_inner_contents = ibox
  168.     ui.breadcrumbbox_inner:map()
  169.     iup.Refresh(ui.breadcrumbbox_inner)
  170. end
  171.  
  172. -- Top line controls --------
  173. ui.entry, ui.textbox, ui.dropdown = test.list2{expand='HORIZONTAL', list = tv.history}
  174. ui.readiup = iup.stationtoggle{title = 'Read IUP', value = 'OFF', action = function(self)
  175.         ui.search.action()
  176.     end
  177. }
  178. ui.search = iup.stationbutton{title='Go', size=120, action=function(self, fromOpen)
  179.         local tbl
  180.     --  if (tv.failed) then
  181.     --      tbl = ui.tree["NAME0"]
  182.     --  else
  183.             tbl = ui.textbox.value
  184.     --  end
  185.         if tbl == '' then return end
  186.         if (fromOpen == nil) then tv.breadcrumbs.list = {} end
  187.         if (test.disable_iup_read[tbl]) then ui.readiup.value = 'OFF' end
  188.         if (tv.ConvertTable(tbl)) then
  189.             ui.tree.value = 'ROOT'
  190.             ui.tree.delnode = 'CHILDREN'
  191.             iup.TreeSetValue(ui.tree, tv.treetable)
  192.             ui.tree.redraw = 'YES'
  193.             tv.breadcrumbs.create(tbl)
  194.             tv.failed = false
  195.             local inHistory = false
  196.             for _, v in ipairs(tv.history) do if (v == tbl) then inHistory = true; break end end
  197.             if (not inHistory) then
  198.                 table.insert(tv.history, 1, tbl)
  199.                 local size = tv.history_size
  200.                 if (#tv.history == (size + 1)) then tv.history[size + 1] = nil end
  201.                 do
  202.                     local h = {}
  203.                     for i = 1, size do h[i] = tv.history[i] end
  204.                     gkini.WriteString('DevKit', 'tableviewer.history', spickle(h))
  205.                 end
  206.             end
  207.             for i, v in ipairs(tv.history) do ui.dropdown[i] = v end
  208.         end
  209.     end
  210. }
  211. -- --------------------------
  212. -- Filter Box definition ----
  213. ui.filterbox = iup.hbox{gap = 10, iup.label{title = 'Filters:'}}
  214. for _, v in ipairs({'boolean', 'number', 'string', 'function', 'userdata', 'thread', 'table'}) do
  215.     ui[v] = iup.stationtoggle{title = v, value = 'on', action = function(self)
  216.         ui.search.action()
  217.     end}
  218.     iup.Append(ui.filterbox, ui[v])
  219. end
  220. -- --------------------------
  221. -- Breadcrumb Box definition
  222. ui.open = iup.stationbutton{title = 'Open', active = 'NO', action = function(self)
  223.         tv.breadcrumbs.list = {}
  224.         local bc = tv.breadcrumbs.list
  225.         local str = build_table_name()
  226.  
  227.         if (test.dont_read[str]) then return end
  228.         ui.textbox.value = str
  229.         ui.search:action(true)
  230.     end
  231. }
  232.  
  233. ui.breadcrumbbox_inner_contents = iup.hbox{gap = 5,}
  234. ui.breadcrumbbox_inner = iup.hbox{ui.breadcrumbbox_inner_contents}
  235. -- --------------------------
  236. -- Other controls
  237. ui.selected = iup.label{title = '', expand = 'HORIZONTAL'}
  238. ui.multiline = iup.stationsubmultiline{size = '350', expand = 'yes', readonly = 'yes'}
  239. ui.tree = iup.stationsubsubtree{expand='YES', addexpanded='NO', renamenode_cb=function(self, id, name)
  240.         if self['KIND'..id] == 'BRANCH' then
  241.             local state = self['STATE'..id]
  242.             if state == 'EXPANDED' then
  243.                 self:setstate(id, 'COLLAPSED')
  244.             else
  245.                 self:setstate(id, 'EXPANDED')
  246.             end
  247.         end
  248.     end, selection_cb = function(self, id, status)
  249.        
  250.         if (ui.textbox.value ~= nil and ui.textbox.value == '') then return iup.IGNORE end
  251.         local tbl, txt
  252.  
  253.         if (id == 0) then
  254.             ui.open.active = 'NO'
  255.         else
  256.             ui.open.active = 'YES'
  257.             tbl = tv.table_ref[id]
  258.         end
  259.         ui.tree.curid = id -- type(id) == 'number', but it is getting converted to 'string' on assignment
  260.  
  261.         if (ui.readiup.value == 'ON' and test.IsIupControl(tbl)) then
  262.             txt = 'Name='..test.nz(iup.GetName(tbl)).."\n"..string.gsub(iup.GetAttributes(tbl),",", "\n")
  263.            
  264.         else
  265.             txt = tostring(tbl)
  266.         end
  267.         ui.multiline.value = txt
  268.         ui.selected.title = build_table_name()
  269.     end
  270. }
  271.  
  272. ui.options = iup.stationbutton{title='Options', size = 100, action = function(self)
  273.         local opt = tv.options
  274.         opt.build_dialog()
  275.         opt.ui.dialog:popup(iup.CENTER, iup.CENTER)
  276.         opt.ui.dialog:destroy()
  277.     end}
  278.  
  279.  
  280. ui.close = iup.stationbutton{title='Close', size = 100, action = function(self)
  281.         HideDialog(ui.dialog)
  282.     end
  283. }
  284. -- -----------------------------
  285.  
  286. function tv.build_dialog()
  287.     ui.main = iup.pdarootframe{
  288.         iup.pdarootframebg{
  289.             iup.vbox{
  290.                 iup.hbox{
  291.                     iup.label{title='Enter A Table:'},
  292.                     ui.entry,
  293.                     ui.readiup,
  294.                     ui.search,
  295.                     gap=5, alignment='ACENTER',
  296.                 },
  297.                 ui.filterbox,
  298.                 iup.hbox{
  299.                     ui.breadcrumbbox_inner,
  300.                     iup.fill{},
  301.                     ui.open,
  302.                 },
  303.                 iup.hbox{iup.label{title = 'Currently selected: '}, ui.selected},
  304.                 iup.hbox{
  305.                     ui.tree,
  306.                     ui.multiline,
  307.                     gap=5,
  308.                 },
  309.                 iup.hbox{iup.fill{expand = 'YES'}, ui.options, ui.close, gap = 5},
  310.                 gap=5,
  311.             },
  312.         },
  313.         size="TWOTHIRDxTWOTHIRD", expand="NO",
  314.     }
  315.  
  316.     ui.dialog = iup.dialog{
  317.         iup.vbox{
  318.             iup.fill{},
  319.             iup.hbox{
  320.                 iup.fill{},
  321.                 ui.main,
  322.                 iup.fill{},
  323.             },
  324.             iup.fill{},
  325.         },
  326.         defaultesc=ui.close,
  327.         defaultenter=ui.search,
  328.         bgcolor="0 0 0 128 *",
  329.         -- fullscreen="YES",
  330.         title = 'Table Viewer [v'..test.version..']',
  331.         border="NO",
  332.         resize="NO",
  333.         menubox="YES",
  334.         topmost="YES",
  335.     }
  336.  
  337.     function ui.dialog:Show(str)
  338.         local autoset = false
  339.         if (str ~= nil and type(str) == 'string') then ui.textbox.value = str; autoset = true end
  340.         ShowDialog(self, iup.CENTER, iup.CENTER)
  341.         if (autoset) then ui.search:action() end
  342.     end
  343.     ui.dialog:map()
  344. end
  345.  
  346. dofile('table_viewer_options.lua')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement