console: optimize table generation

Showing all properties was terribly slow.
Instead of starting at one row and increasing the row count until it
fits, the column count can be increased until it doesn't fit anymore.

That alone already reduces the required iterations, but from the column
count an upper and lower bound for the row count can be calculated.

For large tables this dramatically reduces the amount of iterations.
This commit is contained in:
Christoph Heinrich 2023-09-27 21:44:52 +02:00 committed by Dudemanguy
parent bca0b20c09
commit ff131d7a20
1 changed files with 31 additions and 19 deletions

View File

@ -166,29 +166,41 @@ function format_table(list, width_max, rows_max)
end
-- use as many columns as possible
for rows = 1, list_size do
local columns = math.ceil(list_size / rows)
column_widths = {}
width_total = 0
for columns = 2, list_size do
local old_row_count = row_count
local rows_lower_bound = math.ceil(list_size / columns)
local rows_upper_bound = math.min(list_size, math.ceil(list_size / (columns - 1) - 1))
for rows = rows_upper_bound, rows_lower_bound, -1 do
cw = {}
width_total = 0
-- find out width of each column
for column = 1, columns do
local width = 0
for row = 1, rows do
local i = row + (column - 1) * rows
if i > #list then break end
local item_width = list_widths[i]
if width < item_width then
width = item_width
-- find out width of each column
for column = 1, columns do
local width = 0
for row = 1, rows do
local i = row + (column - 1) * rows
local item_width = list_widths[i]
if not item_width then break end
if width < item_width then
width = item_width
end
end
cw[column] = width
width_total = width_total + width
if width_total + (columns - 1) * spaces_min > width_max then
break
end
end
column_widths[column] = width
width_total = width_total + width
end
if width_total + columns * spaces_min <= width_max then
row_count = rows
column_count = columns
if width_total + (columns - 1) * spaces_min <= width_max then
row_count = rows
column_count = columns
column_widths = cw
else
break
end
end
if row_count == old_row_count then
break
end
end