lua-users home
lua-l archive

Useful function printGlobalVariables(), column style

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A Useful code snippit that I wrote:
function printGlobalVariables()
	local i;
 local scrwidth = 80; -- screen width
 local maxwidth = 0;
 local formatstring;
 -- first found out the max function name width
 i = nextvar(nil);
	while (i) do
	 if (type(getglobal(i)) == "function") then
		maxwidth = max(maxwidth,strlen(tostring(i)));
 end
 i = nextvar(i);
 end
 maxwidth = maxwidth + 2;
 local numcols = floor(scrwidth / maxwidth);
 local curcol = 0;
	formatstring = format("%%%ds ",maxwidth);
 -- next column print all the function names
 i = nextvar(nil);
	while (i) do
	 if (type(getglobal(i)) == "function") then
		write(format(formatstring,tostring(i)));
 curcol = curcol + 1;
 if (curcol >= numcols) then
 curcol = 0;
 write("\n");
 end
 end
 i = nextvar(i);
 end
 
 write("\n");
	-- next, print out the variables:
	i = nextvar(nil);
 	while (i) do
 	 if (type(getglobal(i)) ~= "function") then
 if (type(getglobal(i)) == "string") then
 	 write(format("%25s = '%25s' %10s\n", tostring(i), 
			tostring(getglobal(i)), 
			type(getglobal(i))));
 elseif (type(getglobal(i)) == "number") then
 	 write(format("%25s = %25s %10s\n", tostring(i), 
			tostring(getglobal(i)), 
			type(getglobal(i))));
 else -- who knows what type it is, just print it...
 	 write(format("%25s = '%25s' %10s\n", tostring(i), 
			tostring(getglobal(i)), 
			type(getglobal(i))));
 end;
 end
 i = nextvar(i);
 end
end
-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net

AltStyle によって変換されたページ (->オリジナル) /