Log In  


A quick port of my pico-8 variable inspector, I find useful for debugging.
It has a few more colours for readability, and can display userdata.

To use, hit Esc to break out of your program and enter:

dbg(v)

Where v is the value to inspect.

You can drill into values by clicking the "+" (or collapse them again by clicking "-").
Mouse wheel to scroll up and down.
Press Esc when done.

-- debug inspector

-- lines to display and scroll offset
local dbg_lines,dbg_s={},0,false

-- add item to display
function dadd(n,v,i,p)
	-- insertion sort in name order
	while p>1 
	and dbg_lines[p-1].i==i 
	and type(dbg_lines[p-1].n)==type(n) 
	and dbg_lines[p-1].n>n do 
		p-=1 
	end 
	add(dbg_lines,{n=n,v=v,x=(type(v)=="table" or type(v)=="userdata") and "+" or " ",i=i},p)
end

-- expand/collapse item
function dexp(l,p)
	if l.x=="+" then
		-- add item properties to expand
		if type(l.v)=="userdata" then
			if l.v:height() and l.v:height()>1 then
				for i=0,l.v:height()-1 do
					p+=1
					dadd(i,l.v:row(i),l.i.." ",p)
				end
			elseif l.v:width() then
				for i=0,l.v:width()-1 do
					p+=1
					dadd(i,l.v[i],l.i.." ",p)
				end			
			end
		else
			for n,v in pairs(l.v) do 
				p+=1 
				dadd(n,v,l.i.." ",p)
			end 
		end
		l.x="-"
	elseif l.x=="-" then
		-- delete nested lines to collapse
		while p<#dbg_lines and #dbg_lines[p+1].i>#dbg_lines[p].i do 
			deli(dbg_lines,p+1)
		end
		l.x="+" 
	end 
end

-- handle input
function dinp()

	-- get mouse state
	local x,y,b,_,w=mouse()

	-- handle mouse click
	local click=b==1
	if click and not dbg_click then 
		local i=(y-14)\9+1 
		local p=dbg_s+i 
		local l=dbg_lines[p]
		if l and(x-220)\5==#l.i then 
			dexp(l,p)
		end 
	end

	-- mouse wheel scrolls
	dbg_s=max(min(dbg_s-w*4,#dbg_lines-27),0) 

	dbg_click=click
end

function dtostr(v,nopreview)
	if type(v)=="userdata" then
		local w,h,t,d=v:attribs()
		local r=t
		if h==1 then
			r..="["..w.."]"
			if w<=16 and not nopreview then
				if t=="f64" then 	r..=" ("..string.format("%.2f",v[0])
				else					r..=" ("..v[0]
				end
				for i=1,w-1 do
					if t=="f64" then		r..=","..string.format("%.2f",v[i])
					else					r..=","..v[0]
					end
				end
				r..=")"				
			end
		else
			r..="["..w.."x"..h.."]"
		end
		return r
	elseif type(v)=="table" then
		if #v>0 then
			local r="["..#v.."]"
			if #v<=16 and not nopreview then
				r..=" ("..dtostr(v[1], true)
				for i=2,#v do
					r..=","..dtostr(v[i], true)
				end
				r..=")"
			end
			return r
		else
			return "{}"
		end
	else
		return tostr(v)
	end
end

-- draw UI
function dui()
	while true do 
		rectfill(220,10,470,260,1)
		for i=1,27 do 
			local l=dbg_lines[i+dbg_s]
			if l then 
				local x,y=220,(i-1)*9+14
				x+=#l.i*5
				local lx=x
				print(l.x,x,y,10)
				x+=5
				print(l.n,x,y,12)
				x=max(x+#tostr(l.n)*5,lx+25)
				print(sub(dtostr(l.v),1,(470-x)\5),x,y,7)
			end 
		end 
		dinp()
		flip()
	end 
end

function dbg(v,n)
	dadd(n or "value",v,"",#dbg_lines+1)
	cursor()
	pal()
	clip()
	dui()
end
4


would be great to integrate this with yotta (https://www.lexaloffle.com/bbs/?tid=140833) for easier installation.



[Please log in to post a comment]