1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-28 08:12:18 +00:00

Added improved help viewer (#595)

- Pagination, with (page) up/down, q(uit) and scrolling support.
 - Render markdown style bullets ('-'/'*') using a '•' instead.
This commit is contained in:
JackMacWindows 2020-12-02 14:22:12 -05:00 committed by Jummit
parent d5eb82db60
commit 1d1d6227eb
2 changed files with 122 additions and 8 deletions

View File

@ -415,3 +415,10 @@ d83a68f3ff6e3833278a38798d06215293656e85
Allow $private HTTP rule to block any private IP
```
The config still uses a `blacklist` and `whitelist` array.
```
24d3777722812f975d2bc4594437fbbb0431d910
Added improved help viewer (#595)
```
Didn't port the lua tests over.

View File

@ -15,12 +15,119 @@ end
local sFile = help.lookup(sTopic)
local file = sFile ~= nil and io.open(sFile) or nil
if file then
local sContents = file:read("*a")
file:close()
local _, nHeight = term.getSize()
textutils.pagedPrint(sContents, nHeight - 3)
else
print("No help available")
if not file then
printError("No help available")
return
end
local contents = file:read("*a"):gsub("(\n *)[-*]( +)", "%1\7%2")
file:close()
local width, height = term.getSize()
local buffer = window.create(term.current(), 1, 1, width, height, false)
local old_term = term.redirect(buffer)
local print_height = print(contents) + 1
-- If we fit within the screen, just display without pagination.
if print_height <= height then
term.redirect(old_term)
print(contents)
return
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 function draw()
for y = 1, height - 1 do
term.setCursorPos(1, y)
if y + offset > print_height then
-- Should only happen if we resize the terminal to a larger one
-- than actually needed for the current text.
term.clearLine()
else
term.blit(buffer.getLine(y + offset))
end
end
end
local function draw_menu()
term.setTextColor(colors.yellow)
term.setCursorPos(1, height)
term.clearLine()
local tag = "Help: " .. sTopic
term.write("Help: " .. sTopic)
if width >= #tag + 16 then
term.setCursorPos(width - 14, height)
term.write("Press Q to exit")
end
end
draw_buffer(width)
draw()
draw_menu()
while true do
local event, param = os.pullEvent()
if event == "key" then
if param == keys.up and offset > 0 then
offset = offset - 1
draw()
elseif param == keys.down and offset < print_height - height then
offset = offset + 1
draw()
elseif param == keys.pageUp and offset > 0 then
offset = math.max(offset - height + 2, 0)
draw()
elseif param == keys.pageDown and offset < print_height - height then
offset = math.min(offset + height - 2, print_height - height)
draw()
elseif param == keys.home then
offset = 0
draw()
elseif param == keys["end"] then
offset = print_height - height
draw()
elseif param == keys.q then
sleep(0) -- Super janky, but consumes stray "char" events.
break
end
elseif event == "mouse_scroll" then
if param < 0 and offset > 0 then
offset = offset - 1
draw()
elseif param > 0 and offset < print_height - height then
offset = offset + 1
draw()
end
elseif event == "term_resize" then
local new_width, new_height = term.getSize()
if new_width ~= width then
buffer.setCursorPos(1, 1)
buffer.reposition(1, 1, new_width, print_height)
term.redirect(buffer)
print_height = print(contents) + 1
draw_buffer(new_width)
end
width, height = new_width, new_height
offset = math.max(math.min(offset, print_height - height), 0)
draw()
draw_menu()
end
end
term.redirect(old_term)
term.setCursorPos(1, 1)
term.clear()