Debug Console: Lua errors printing tables with long strings

Moderator: Moderator

Post Reply
Message
Author
Hachem_Muche
Uruivellas
Posts: 744
Joined: Thu Nov 18, 2010 6:42 pm

Debug Console: Lua errors printing tables with long strings

#1 Post by Hachem_Muche »

Printing out a table in the Debug Console will produce a lua error (and no output) if the table contains a string of 100 or more characters. Apparently the string format specifier. %ns, does not accept a width of more than 99 characters. To solve this I revised _M:historyColumns in engine\DebugConsole.lua to truncate such strings at 80 characters (and add an elipsis "..." to the end):

Code: Select all

--- Add a list of strings to the history with multiple columns
-- @param strings Array of strings to add to the history
-- @param offset Number of spaces to add on the left-hand side
function _M:historyColumns(strings, offset)
	local offset_str = string.rep(" ", offset and offset or 0)
	local ox, oy = self.font:size(offset_str)
	local longest_key = ""
	local width = 0  --
	local max_width = 80 -- Maximum field width to print 
	
--	for i, k in ipairs(strings) do
--		if #k > #longest_key then
--			longest_key = k
--		end
--	end

	for i, k in ipairs(strings) do
		if #k > width then
			longest_key = k
			width = #k
			if width >= max_width then
				width = max_width
				break
			end
		end
	end
	
--	local tx, ty = self.font:size(longest_key .. "  ")
	local tx, ty = self.font:size(string.sub(longest_key,1,width) .. "...  ") --
	local num_columns = math.floor((self.w - ox) / tx)
	local num_rows = math.ceil(#strings / num_columns)

--	local line_format = offset_str..string.rep("%-"..tostring(#longest_key).."s ", num_columns)
	local line_format = offset_str..string.rep("%-"..tostring(math.min(max_width+5,width+5)).."s ", num_columns) --
	
	for i=1,num_rows do
		vals = {}
		for j=1,num_columns do
			vals[j] = strings[i + (j - 1) * num_rows] or ""
			--Truncate and annotate if too long
			if #vals[j] > width then --
				vals[j] = string.sub(vals[j],1,width) .. "..." --
			end --
		end
		table.insert(_M.history, line_format:format(unpack(vals)))
	end
end
Author of the Infinite 500 and PlenumTooltip addons, and the joys of Scaling in ToME.

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: Debug Console: Lua errors printing tables with long stri

#2 Post by darkgod »

fixed
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

Post Reply