Manually wrap strings for help (#602)

This saves us writing to a buffer multiple times, and so makes things much,
much faster.
This commit is contained in:
Jonathan Coates 2020-12-23 16:33:58 +00:00 committed by GitHub
parent 16d74dd2e8
commit ed3913c1f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 23 deletions

View File

@ -13,6 +13,19 @@ if sTopic == "index" then
return return
end end
local strings = require "cc.strings"
local function word_wrap(text, width)
local lines = strings.wrap(text, width)
-- Normalise the strings suitable for use with blit. We could skip this and
-- just use term.write, but saves us a clearLine call.
for k, line in pairs(lines) do
lines[k] = strings.ensure_width(line, width)
end
return lines
end
local sFile = help.lookup(sTopic) local sFile = help.lookup(sTopic)
local file = sFile ~= nil and io.open(sFile) or nil local file = sFile ~= nil and io.open(sFile) or nil
if not file then if not file then
@ -24,37 +37,27 @@ local contents = file:read("*a"):gsub("(\n *)[-*]( +)", "%1\7%2")
file:close() file:close()
local width, height = term.getSize() local width, height = term.getSize()
local buffer = window.create(term.current(), 1, 1, width, height, false) local lines = word_wrap(contents, width)
local old_term = term.redirect(buffer) local print_height = #lines
local print_height = print(contents) + 1
-- If we fit within the screen, just display without pagination. -- If we fit within the screen, just display without pagination.
if print_height <= height then if print_height <= height then
term.redirect(old_term)
print(contents) print(contents)
return return
end end
local function draw_buffer(width)
buffer.reposition(1, 1, width, print_height)
buffer.clear()
buffer.setCursorPos(1, 1)
print(contents)
term.redirect(old_term)
end
local offset = 0 local offset = 0
local function draw() local function draw()
local fg, bg = ("0"):rep(width), ("f"):rep(width)
for y = 1, height - 1 do for y = 1, height - 1 do
term.setCursorPos(1, y) term.setCursorPos(1, y)
if y + offset > print_height then if y + offset > print_height then
-- Should only happen if we resize the terminal to a larger one -- Should only happen if we resize the terminal to a larger one
-- than actually needed for the current text. -- than actually needed for the current text.
term.clearLine() term.clearLine()
else else
term.blit(buffer.getLine(y + offset)) term.blit(lines[y + offset], fg, bg)
end end
end end
end end
@ -73,7 +76,6 @@ local function draw_menu()
end end
end end
draw_buffer(width)
draw() draw()
draw_menu() draw_menu()
@ -114,11 +116,8 @@ while true do
local new_width, new_height = term.getSize() local new_width, new_height = term.getSize()
if new_width ~= width then if new_width ~= width then
buffer.setCursorPos(1, 1) lines = word_wrap(contents, new_width)
buffer.reposition(1, 1, new_width, print_height) print_height = #lines
term.redirect(buffer)
print_height = print(contents) + 1
draw_buffer(new_width)
end end
width, height = new_width, new_height width, height = new_width, new_height
@ -128,6 +127,5 @@ while true do
end end
end end
term.redirect(old_term)
term.setCursorPos(1, 1) term.setCursorPos(1, 1)
term.clear() term.clear()