Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9578941

Browse files
committedNov 3, 2022
console: cull lines outside of visible area
Lines that would be outside of the visible area are now culled. The log messages were already culled, however with the introduction of suggestions, they also needed a small update.
1 parent 6712ac4 commit 9578941

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed
 

‎player/lua/console.lua

+26-18
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,14 @@ function ass_escape(str)
150150
return str
151151
end
152152

153-
-- Takes a list of strings and a max width in characters
154-
-- returns a string containing the formatted table
155-
function format_table(list, width_max)
153+
-- Takes a list of strings, a max width in characters and
154+
-- optionally a max row count.
155+
-- The result contains at least one column.
156+
-- Rows are cut off from the top if rows_max is specified.
157+
-- returns a string containing the formatted table and the row count
158+
function format_table(list, width_max, rows_max)
156159
if #list == 0 then
157-
return ''
160+
return '', 0
158161
end
159162

160163
local spaces_min = 2
@@ -198,7 +201,7 @@ function format_table(list, width_max)
198201
for row = 1, rows_needed do
199202
result = result .. list[row] .. '\n'
200203
end
201-
return result
204+
return result, rows_needed
202205
end
203206

204207
local spaces = math.floor((width_max - width_total) / (columns_needed - 1))
@@ -209,7 +212,9 @@ function format_table(list, width_max)
209212
for column = 1, columns_needed - 1 do
210213
column_widths[column] = column_widths[column] + spaces
211214
end
212-
for row = 1, rows_needed do
215+
-- cut off rows
216+
local start = rows_max and math.max(1, rows_needed - rows_max + 1) or 1
217+
for row = start, rows_needed do
213218
local row_text = ''
214219
for column = 1, columns_needed do
215220
local i = row + (column - 1) * rows_needed
@@ -219,7 +224,7 @@ function format_table(list, width_max)
219224
end
220225
result = result .. row_text .. '\n'
221226
end
222-
return result
227+
return result, rows_needed
223228
end
224229

225230
-- Render the REPL and console as an ASS OSD
@@ -265,26 +270,29 @@ function update()
265270
local before_cur = ass_escape(line:sub(1, cursor - 1))
266271
local after_cur = ass_escape(line:sub(cursor))
267272

268-
-- Render log messages as ASS. This will render at most screeny / font_size
269-
-- messages.
273+
-- Render log messages as ASS.
274+
-- This will render at most screeny / font_size - 1 messages.
275+
276+
-- lines above the prompt
277+
-- subtract 1.5 to account for the input line
278+
local screeny_factor = 1 - global_margin_top - global_margin_bottom
279+
local lines_max = math.ceil(screeny * screeny_factor / opts.font_size - 1.5)
280+
-- Estimate how many characters fit in one line
281+
local width_max = math.ceil(screenx / opts.font_size * opts.font_hw_ratio)
282+
283+
local suggestions, rows = format_table(suggestion_buffer, width_max, lines_max)
284+
local suggestion_ass = style .. styles.suggestion .. ass_escape(suggestions)
285+
270286
local log_ass = ''
271287
local log_messages = #log_buffer
272-
local screeny_factor = (1 - global_margin_top - global_margin_bottom)
273-
-- subtract 1.5 to account for the input line
274-
local log_max_lines = screeny * screeny_factor / opts.font_size - 1.5
275-
log_max_lines = math.ceil(log_max_lines)
288+
local log_max_lines = math.max(0, lines_max - rows)
276289
if log_max_lines < log_messages then
277290
log_messages = log_max_lines
278291
end
279292
for i = #log_buffer - log_messages + 1, #log_buffer do
280293
log_ass = log_ass .. style .. log_buffer[i].style .. ass_escape(log_buffer[i].text)
281294
end
282295

283-
-- estimate how many characters fit in a line
284-
local width_max = math.ceil(screenx / opts.font_size * opts.font_hw_ratio)
285-
local table_string = ass_escape(format_table(suggestion_buffer, width_max))
286-
local suggestion_ass = style .. styles.suggestion .. table_string
287-
288296
ass:new_event()
289297
ass:an(1)
290298
ass:pos(2, screeny - 2 - global_margin_bottom * screeny)

0 commit comments

Comments
 (0)
Please sign in to comment.