Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ inspect-fenv
- returns a list of getfenv variables with PAGE_UP / PAGE_DOWN enabled by opening `edit` and removing the temp file after you exit `edit`.
- test
- Usage: pastebin run <id> [expand_key [expand_key [...] ]
- e.g: pastebin run X66qGPN6 _G _ENV
- ]]
- function __INSPECT_FENV__()
- end
- local function dump(node, depth_start)
- -- to make output beautiful
- local function tab(amt)
- local str = ""
- for i=1,amt do
- str = str .. "\t"
- end
- return str
- end
- local cache, stack, output = {},{},{}
- local depth = depth_start
- local output_str = "{\n"
- while true do
- local size = 0
- for k,v in pairs(node) do
- size = size + 1
- end
- local cur_index = 1
- for k,v in pairs(node) do
- if (cache[node] == nil) or (cur_index >= cache[node]) then
- if (string.find(output_str,"}",output_str:len())) then
- output_str = output_str .. ",\n"
- elseif not (string.find(output_str,"\n",output_str:len())) then
- output_str = output_str .. "\n"
- end
- -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
- table.insert(output,output_str)
- output_str = ""
- local key = "["..tostring(k).."]"
- if (type(v) == "number" or type(v) == "boolean") then
- output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
- elseif (type(v) == "table") then
- output_str = output_str .. tab(depth) .. key .. " = {\n"
- table.insert(stack,node)
- table.insert(stack,v)
- cache[node] = cur_index+1
- break
- else
- output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)..""
- end
- if (cur_index == size) then
- output_str = output_str .. "\n" .. tab(depth-1) .. "}"
- else
- output_str = output_str .. ","
- end
- else
- -- close the table
- if (cur_index == size) then
- if depth < depth_start then depth = depth_start end
- output_str = output_str .. "\n" .. tab(depth-1) .. "}"
- end
- end
- cur_index = cur_index + 1
- end
- if (size == 0) then
- output_str = output_str .. "\n" .. tab(depth-1) .. "}"
- end
- if (#stack > 0) then
- node = stack[#stack]
- stack[#stack] = nil
- depth = cache[node] == nil and depth + 1 or depth - 1
- if depth < depth_start then depth = depth_start end
- else
- break
- end
- end
- -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
- table.insert(output,output_str)
- output_str = table.concat(output)
- return output_str
- end
- local function flush(file)
- local h = fs.open(file, "w")
- h.write("")
- h.close()
- return ""
- end
- local function append(file, text)
- local h = fs.open(file, "a")
- h.write(text)
- h.close()
- end
- local function display(log)
- shell.run("edit " .. log)
- end
- local function inspect_env(callback)
- for depth=-100,100 do
- _, env = pcall(getfenv, depth)
- if _ then
- callback(env, depth)
- end
- end
- end
- local function cli(...)
- local logfile = '/inspect-tmp-' .. os.time() .. '.log'
- local args = {...}
- function args:has(key)
- for _, value in pairs(self) do if value == key then return true end end
- return false
- end
- local function log(message)
- append(logfile, tostring(message))
- end
- flush(logfile)
- inspect_env(function(env, depth)
- function env:has(marker)
- for x, y in pairs(self) do
- if x == marker then return true end
- end
- return false
- end
- if env:has("__INSPECT_FENV__") then
- log(("[depth: %s] \n"):format(depth))
- else
- log(("depth: %s \n"):format(depth))
- end
- for key, value in pairs(env) do
- if env:has("__INSPECT_FENV__") then log("*") end
- if args:has(key) then log(">") end
- log(("%s %s %s \n"):format(string.rep(" ", depth), tostring(key), tostring(value)))
- if args:has(key) then
- log(("%s %s \n"):format(string.rep(" ", depth), tostring(dump(value, depth))))
- end
- end
- end)
- display(logfile)
- fs.delete(logfile)
- end
- cli(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement