

local tArgs = { ... }

function slowPrint( sText, nRate )
	nRate = nRate or 20
	if nRate < 0 then
		error( "rate must be positive" )
	end
	local nSleep = 1 / nRate
		
	sText = tostring( sText )
	local x,y = term.getCursorPos(x,y)
	local len = string.len( sText )
	
	for n=1,len do
		term.setCursorPos( x, y )
		sleep( nSleep )
		local nLines = write( string.sub( sText, 1, n ) )
		local newX, newY = term.getCursorPos()
		y = newY - nLines
	end
	print()
end

function formatTime( nTime, bTwentyFourHour )
	local sTOD = nil
	if not bTwentyFourHour then
		if nTime >= 12 then
			sTOD = "PM"
		else
			sTOD = "AM"
		end
		if nTime >= 13 then
			nTime = nTime - 12
		end
	end

	local nHour = math.floor(nTime)
	local nMinute = math.floor((nTime - nHour)*60)
	if sTOD then
		return string.format( "%d:%02d %s", nHour, nMinute, sTOD )
	else
		return string.format( "%d:%02d", nHour, nMinute )
	end
end

function tabulate( ... )
	local tAll = { ... }
	
	local w,h = term.getSize()
	local nMaxLen = w / 8
	for n, t in pairs( tAll ) do
		for n, sItem in pairs(t) do
			nMaxLen = math.max( string.len( sItem ) + 1, nMaxLen )
		end
	end
	local nCols = math.floor( w / nMaxLen )

	local function drawCols( _t )
		local nCol = 1
		for n, s in ipairs( _t ) do
			if nCol > nCols then
				nCol = 1
				print()
			end

			local cx,cy = term.getCursorPos()
			cx = 1 + (nCol - 1) * (w / nCols)
			term.setCursorPos( cx, cy )
			term.write( s )

			nCol = nCol + 1  	
		end
		print()
	end
	for n, t in pairs( tAll ) do
		if #t > 0 then
			drawCols( t )
		end
	end	
end
