Created
November 21, 2022 15:07
-
-
Save m-doescode/814d0c9f84a0508b7878999ba8c7e4b3 to your computer and use it in GitHub Desktop.
ComputerCraft | more.lua | Paginate the output of a long program and access the next page by pressing any key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- by m-doescode | |
| local path = ... | |
| local program = shell.resolveProgram(path) | |
| if program == nil then | |
| error("No such program: " .. path,0) | |
| end | |
| local sx, sy = term.getSize() | |
| local oTerm = term.current() | |
| local win = window.create(term.current(),1,1,sx,sy - 1) | |
| function awaitKey() | |
| while true do | |
| local event, key, rpt = os.pullEventRaw("key") | |
| if event == "terminate" then | |
| term.redirect(term.current()) | |
| term.scroll(2) | |
| error() | |
| elseif not rpt and key ~= keys.leftCtrl and key ~= keys.rightCtrl then | |
| return | |
| end | |
| end | |
| end | |
| local nScrolled = 0 | |
| function updatePage() | |
| local px, py = term.getCursorPos() | |
| local pbg, pfg = term.getBackgroundColor(), term.getTextColor() | |
| oTerm.setCursorPos(1,sy) | |
| oTerm.setBackgroundColor(colors.black) | |
| oTerm.setTextColor(colors.white) | |
| oTerm.clearLine() | |
| oTerm.write(" == More - Press any key - Page " .. math.floor(nScrolled / (sy - 1)) .. " ==") | |
| oTerm.setCursorPos(px,py) | |
| oTerm.setBackgroundColor(pbg) | |
| oTerm.setTextColor(pfg) | |
| end | |
| local _scroll = win.scroll | |
| function win.scroll(n) | |
| nScrolled = nScrolled + n | |
| if nScrolled % (sy - 1) + 1 >= (sy - 1) then | |
| updatePage() | |
| awaitKey() | |
| end | |
| _scroll(n) | |
| end | |
| term.redirect(win) | |
| shell.execute(program) | |
| term.redirect(oTerm) | |
| print(" == Program finished == ") | |
| term.setCursorPos(1,sy) | |
| term.clearLine() | |
| term.scroll(2) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About no license: If anyone in the future sees this and wishes to use it, I am happy to license it under a copyleft license such as LGPL or MIT. However, for the time being I can't figure how/what license to use, but you can use as you wish in your personal projects.
Simply contact me so that I'll get on to adding a license.