News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Exploring tables
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
RE: Exploring tables

A little bit of code cleanup
Also, multiline strings now display on multiple lines in the browser. This is particularly visible with bind_lua_addtovtable()

Code:
-- everything is local so it won't show up in the Global environment
local w,outpoot,title,backbutt,current_table
local my_depth=0
local my_level={}
local my_title={}
local my_line={}

-- text colors
local col_tab="@b"
local col_func="@C1"
local col_str="@C4"
local col_num="@C13"
local col_usr="@C8@B17"

local function lookittable()
  outpoot:clear()
  local my_value,my_string
  local tmptab={} -- temporary table for alphabetizing the list of my_string's
  for k,v in pairs(current_table) do
    if type(v)=="string" then
      my_value=col_str..tostring(k).."\t"..string.gsub(tostring(v),"\n.*","")
      table.insert(tmptab,my_value)
      my_string=string.gsub(tostring(v),".-\n(.*)","%1")
      my_value=0
      for line in string.gmatch(my_string,".-\n") do
        my_value=1+my_value
        -- for multiline strings, pad numbers with zeros for proper sorting.
        table.insert(tmptab,col_str..tostring(k)..string.format('%03d',my_value).."\t"..line)
      end
    else
      my_value=tostring(k).."\t"..tostring(v)
      if     type(v)=="table" then my_string=col_tab..tostring(k)
      elseif type(v)=="function" then my_string=col_func..my_value
      elseif type(v)=="number" then my_string=col_num..my_value
      elseif type(v)=="userdata" then my_string=col_usr..my_value
      else my_string=tostring(k)
      end
      table.insert(tmptab,my_string)
    end --if
  end
  table.sort(tmptab) -- alphabetize
  for i,v in ipairs(tmptab) do outpoot:add(v) end -- add the sorted list to the browser
  title:label(my_title[my_depth])
  if my_depth==0 then backbutt:deactivate() else backbutt:activate() end
  outpoot:middleline(my_line[my_depth])
end

local function move_forward(k,v)
  my_depth=my_depth+1 -- move up a level
  my_level[my_depth]=current_table
  my_title[my_depth]=tostring(k).." ("..type(v)..")"
  my_line[my_depth]=1
  lookittable()
end

local function move_back()
if my_depth > 0 then
  current_table=my_level[my_depth-1]
  my_depth=my_depth-1
  lookittable()
end
end

local function which_is_it()
  local this_text=outpoot:text(outpoot:value())
  if outpoot:value() > 0 and type(current_table) == "table" then
    my_line[my_depth]=outpoot:value()
    -- Compare the browser text with each key in current_table until a match is found
    -- This could probably be streamlined
    for k,v in pairs(current_table) do
      -- it's a table
      if this_text == col_tab..tostring(k) then
        my_level[my_depth]=current_table --set a table to return to with "back"
        current_table=v
    move_forward(k,v)
        break
      end
      -- it's userdata
      if string.find(this_text,"^"..col_usr..tostring(k)) then
        my_level[my_depth]=current_table
        current_table=getmetatable(v)
    move_forward(k,v)
        break
      end
      -- it's a function
      if string.find(this_text,"^"..col_func..tostring(k))  then
        my_level[my_depth]=current_table
        -- is there a way to get more useful info?
        current_table=debug.getinfo(v,"nSlufL")
    move_forward(k,v)
        break
      end
    end
  end
end

local function mlexplore()
local ww=400
local wh=360

local win=fltk:Fl_Double_Window(ww,wh,"view murga tables")

outpoot=fltk:Fl_Select_Browser(10,40,ww-20,wh-50)
if Fl_Browser.column_widths then outpoot:column_widths({(ww-20)/2,0}) end
outpoot:selection_color(0)
outpoot:callback(which_is_it)

title=fltk:Fl_Box(90,10,ww-100,30)
title:box(fltk.FL_THIN_UP_BOX)

backbutt=fltk:Fl_Button(10,10,80,30,"@<- back")
backbutt:callback(move_back)

win:resizable(outpoot)
current_table=_G
my_title[my_depth]="Global Environment (table: _G)"
my_line[my_depth]=1
lookittable()
win:show()
Fl:run()
end

mlexplore()

04-24-2008 06:07 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
Exploring tables - mikshaw - 09-07-2007, 06:53 AM
RE: Exploring tables - chrimo - 09-07-2007, 07:28 AM
RE: Exploring tables - mikshaw - 09-07-2007, 01:18 PM
RE: Exploring tables - mikshaw - 09-08-2007, 09:22 AM
RE: Exploring tables - mikshaw - 09-12-2007, 09:40 AM
RE: Exploring tables - mikshaw - 01-22-2008, 05:23 AM
RE: Exploring tables - mikshaw - 03-24-2008, 11:36 AM
RE: Exploring tables - mikshaw - 04-24-2008 06:07 AM
RE: Exploring tables - petermac - 08-05-2008, 10:05 AM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: