Jump to content

Module:SimpleDebug

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jmarchn (talk | contribs) at 21:50, 12 November 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

p.s = ''
p.tab = {
	oneline = true,
	allidx = false, 
	}
p.dec = -1
p.maxlines = {
	num = 100,
	doerror = true,
	}
p.enabled = true
p.nowiki = false

local LinCount = 0
local vep = '  •  '

local function arrow()
	return ' => '
end

function p.breakline ()
	if p.s ~= '' then
		LinCount = LinCount + 1
	end	
	p.s = p.s..'\n\n'
	if (LinCount > p.maxlines.num) and p.maxlines.doerror then
		error (p.s,0)
	end	
end	--breakline

local function CheckWhere (where)
	if where == nil then
		return '"where" == nil'
	else
		return where	
	end	
end --CheckWhere

local function var (avar)
	if type(avar) == 'table' then
		if p.tab.oneline then
			local wds = '{ '
			for k,v in pairs(avar) do
				if (p.tab.allidx == true) or (type(k) ~= 'number')  then 
					wds = wds..'['..k..']='..var(v)..', '
				else	
					wds = wds..var(v)..', '
				end
			end
			EndStr = wds .. '} '
		else
			EndStr = mw.dumpObject(avar)
		end	
	elseif type(avar) == 'number' then
		if (p.dec == -1) or (avar == math.floor(avar)) then
			EndStr = tostring(avar)
		else
			EndStr = tostring (math.floor ((avar*10^p.dec)+0.5) / (10^p.dec))
		end	
	elseif type(avar) == 'boolean' then
		if avar == true then
			EndStr = 'true'
		else
			EndStr = 'false'
		end	
	elseif avar == nil then	
		EndStr = 'nil'
	elseif p.nowiki then
		EndStr = '<nowiki>'..tostring(avar)..'</nowiki>'
	else			
		EndStr = '"'..tostring(avar)..'"'
	end
	return EndStr
end --var

function p.w (where)
	if p.enabled then
		return CheckWhere (where)
	end	
end --w

function p.v (...)
	if p.enabled then
		local str = ''
		for i = 1, #arg do
			if str ~= '' then
				str = str..vep
			end	
			str = str..var(arg[i])
		end	
		return str
	end	
end	--v

function p.wv (where, ...)
	if p.enabled then
		return CheckWhere(where)..arrow()..p.v(unpack(arg))
	end	
end	--wv

function p.nv (...)
	if p.enabled then
		if math.mod(#arg,2) ~= 0 then	
			EndStr = 'Any parameter has not a name or variable'
		else
			local s = ''
			local IsName = true
			function Concat(wds)
				if s ~= '' then
					if IsName then
						s = s..vep
					else	
						s = s..': '
					end	
				end
				s = s..wds
			end
			for i = 1, #arg do
				if IsName then
					Concat (CheckWhere(arg[i]))
					IsName = false
				else	
					Concat (var(arg[i]))
					IsName = true
				end	
			end
			EndStr = s
		end
		return EndStr
	end	
end --nv

function p.wnv (where, ...)
	if p.enabled then
		return CheckWhere(where)..arrow()..p.nv (unpack(arg))
	end	
end

----------

local function EnabAndBl ()
	if p.enabled and (LinCount < p.maxlines.num) then
		if p.s ~= '' then
			p.breakline ()
		end	
		return true
	else
		return false
	end
end --EnabAndBl

function p.wtos (where)
	if EnabAndBl () then
		p.s = p.s..p.w (where)
	end	
end --wtos

function p.vtos (...)
	if EnabAndBl () then
		p.s = p.s..p.v (unpack(arg))
	end	
end --vtos

function p.wvtos (where, ...)
	if EnabAndBl () then
		p.s = p.s..p.wv (where,unpack(arg))
	end	
end --wvtos

function p.nvtos (...)
	if EnabAndBl () then
		p.s = p.s..p.nv (unpack(arg))
	end	
end --nvtos

function p.wnvtos (where, ...)
	if EnabAndBl () then
		p.s = p.s..p.wnv (where, unpack(arg))
	end	
end --wnvtos

return p