Global Environment Display

  • FeynmanLogomaker
    1st Oct 2013 Member 0 Permalink

    The code: 

     

    function globaldisp()
    local f = io.open('GlobalEnv.txt', 'w')
    for i, v in pairs(_G) do
    f:write('Item: ', tostring(i), '\t\tValue: ', tostring(v), '\n')
    if type(v) == 'table' and v ~= _G then
    for i2, v2 in pairs(v) do
    f:write('\tItem: ', tostring(i2), '\t\tValue: ', tostring(v2), '\n')
    if type(v2) == 'table' and v2 ~= _G then
    for i3, v3 in pairs(v2) do
    f:write('\t\tItem: ', tostring(i3), '\t\tValue: ', tostring(v3), '\n')
    if type(v3) == 'table' and v3 ~= _G then
    for i4, v4 in pairs(v3) do
    f:write('\t\t\tItem: ', tostring(i4), '\t\tValue: ', tostring(v4), '\n')
    if type(v4) == 'table' then
    for i5, v5 in pairs(v4) do
    f:write('\t\t\t\tItem: ', tostring(i4), '\t\tValue: ', tostring(v4), '\n')
    end
    end
    end
    end
    end
    end
    end
    end
    end
    f:close()
    end

     

    Of course, you can also use this, but I may or may not have copied most of it from a book I have on Lua programming:

    local function LclRenderStr(Obj, TruncLen)
    local TpStr = type(Obj)
    if TpStr == "string" then
    Obj = string.gsub(Obj, "[^%w%p ]", function(Ch)
    return "\\" .. string.format("%03d", string.byte(Ch)) end )
    if TruncLen and TruncLen > 0 and string.len(Obj) > TruncLen + 3 then
    -- This could misleadingly truncate numeric escape value
    Obj = string.sub(Obj, 1, TruncLen) .. "..."
    end
    Obj = '"' .. Obj .. '"'
    elseif TpStr == "boolean" then
    Obj = "boolean: " .. tostring(Obj)
    else
    Obj = tostring(Obj)
    end
    return Obj
    end
    local function LclShave(Str)
    local Count
    Str, Count = string.gsub(Str, '^%[%"(.+)%"%]$', '%1')
    if Count == 1 then
    Str = string.gsub(Str, '%"%]%[%"', '.')
    end
    return Str
    end
    local function LclRender(Tbl, Val, KeyStr, TruncLen, Lvl, Visited, KeyPathStr)
    local VtpStr, ValStr
    VtpStr = type(Val)
    if Visited[Val] then
    ValStr = "same as " .. Visited[Val]
    else
    ValStr = LclRenderStr(Val, TruncLen)
    if VtpStr == "function" then
    local Env = getfenv(Val)
    Env = Visited[Env] or Env
    ValStr = string.gsub(ValStr, "(function:%s*.*)$", "%1 (env " ..
    string.gsub(tostring(Env), "table: ", "") .. ")")
    elseif VtpStr == "table" then
    ValStr = ValStr .. string.format(" (n = %d)", #Val)
    end
    end
    KeyPathStr = KeyPathStr .. "[" .. KeyStr .. "]"
    table.insert(Tbl, { Lvl, string.format('[%s] %s',
    KeyStr, ValStr) })
    if VtpStr == "table" and not Visited[Val] then
    Visited[Val] = LclShave(KeyPathStr)
    local SrtTbl = {}
    for K, V in pairs(Val) do
    table.insert(SrtTbl, { LclRenderStr(K, TruncLen), V, K, type(K) })
    end
    local function LclCmp(A, B)
    local Cmp
    local Ta, Tb = A[4], B[4]
    if Ta == "number" then
    if Tb == "number" then
    Cmp = A[3] < B[3]
    else
    Cmp = true
    end
    else
    if Tb == "number" then
    Cmp = false
    else
    Cmp = A[1] < B[1]
    end
    end
    return Cmp
    end
    table.sort(SrtTbl, LclCmp)
    for J, Rec in ipairs(SrtTbl) do
    LclRender(Tbl, Rec[2], Rec[1], TruncLen, Lvl + 1, Visited, KeyPathStr)
    end
    end
    end
    function ObjectDescribe(Tbl, Val, Key, TruncLen)
    LclRender(Tbl, Val, LclRenderStr(Key, TruncLen), TruncLen or 0, 1, {}, "")
    end
    function ObjectShow(Val, Key, TruncLen)
    local Tbl = {}
    ObjectDescribe(Tbl, Val, Key, TruncLen)
    for J, Rec in ipairs(Tbl) do
    io.write(string.rep(" ", Rec[1] - 1), Rec[2], "\n")
    end
    end
    function _GShow()
    io.output('GlobalEnv.txt')
    ObjectShow(_G, nil, math.huge)
    end
     

    You'll have to figure out what the output looks like on your own, since Pastebin won't let me post a file greater than 500 kb. Yeah, the Lua/TPT global environment is HUGE. For me, the output spanned about 23,588 lines!

    Edited 3 times by FeynmanLogomaker. Last: 2nd Oct 2013
  • timpfeifer
    1st Oct 2013 Member 0 Permalink

    what are the uses and what key I must press?

  • mniip
    1st Oct 2013 Developer 0 Permalink
    @FeynmanLogomaker (View Post)
    you're doing it wrong
    EDIT: _G._G=_G, it recurses
    Edited once by mniip. Last: 1st Oct 2013
  • FeynmanLogomaker
    1st Oct 2013 Member 0 Permalink

    @timpfeifer (View Post)

     Run the script, and then type 'globaldisp()' (no quotes) in the console. It should generate a file called GlobalEnv.txt in the same folder as your TPT executable.

    @mniip (View Post)

     Thanks, I'll fix have fixed that.

    Edited once by FeynmanLogomaker. Last: 1st Oct 2013
  • timpfeifer
    30th Nov 2013 Member 0 Permalink

    but what you can do with it

  • Schneumer
    30th Nov 2013 Member 0 Permalink

    More necro. God. Timpfeiter, STOP.

  • xetalim
    30th Nov 2013 Member 0 Permalink

    @Schneumer (View Post)

     ALL HAIL THE MIGHTY MODERATOR SCHNEUMER
    THE MODERATOR WHO REPLIES TO EVERY NECRO!

    AS ONLY MODERATORS ARE MODERATORS, AND USERS ARE NOT!

    Edited once by xetalim. Last: 30th Nov 2013
  • Schneumer
    30th Nov 2013 Member 0 Permalink

    You  do too, I just so happen to be active now. Either way, just informing the mods. :l

  • xetalim
    30th Nov 2013 Member 2 Permalink

    @Schneumer (View Post)

    That is not informing the mods.
    Mods dont have to be informed, they can see it.

  • FeynmanLogomaker
    1st Dec 2013 Member 0 Permalink

    Well, thank you all for bringing this to the light again...