This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MineRobber___T's profile picture

Printerm API - Use a printer as a terminal!

Started by MineRobber___T, 25 June 2017 - 10:24 PM
MineRobber___T #1
Posted 26 June 2017 - 12:24 AM
Printerm API

Have you ever wanted to have an easy interface to a printer? (pretend you have)

Is interacting with a printer confusing? (pretend it is)

Do you want to be able to term.redirect to a printer? (pretend you do)

In that case, this API is for you!

Wrapping all functions required of a terminal object, (down to British spellings) and creating an object you can use took a while but it works (to the best of my knowledge, haven't tested fringe cases) as good as I can get it.

Example (with Printerm):

os.loadAPI("printerm")
local paperTerm = printerm.getTerm(...)
local oldTerm = term.redirect(paperTerm)
print("Testing 1,2,3")
paperTerm.endPage()
term.redirect(oldTerm)

Example (without):

local printer = peripheral.wrap(...)
printer.newPage()
printer.write("Testing 1,2,3")
printer.endPage()

The benefits of this API don't become apparent in this example. Let's say you have a program that displays info to a screen. (Lots of info, like the status of all machines in your base.) Using printerm, you get the benefit of auto-word-wrap (print() and write()) as well as the convenience of a printout.

How to get the API (link):

pastebin get GzSunvu9 printerm


Documentation is here.
Edited on 25 June 2017 - 10:54 PM
Bomb Bloke #2
Posted 27 June 2017 - 12:41 AM
Lua itself doesn't really care that much as to how many args you pass during a given function call. Your stub() and stubonearg(a) functions are completely interchangable, for example, and you could also get away with "ret.blit = ret.write".

This line:

local printer = peripheral.wrap(sSide)

… could be replaced with:

local printer = type(printer) == "table" and printer or peripheral.wrap(sSide)

… allowing this sort of thing:

local newPrinterm = printerm.getTerm( peripheral.find( "printer" ) )

Having checkStatus() error is a bit nasty; I'd have it ask the user to press a key after supplying whatever consumables the printer might be missing, looping until they're detected. Also note that there's a good chance that the printer will be the current terminal when you want to write your error (resulting in a recursive loop), so you may want to have getTerm record term.current()'s output as a "safer" bet for where to write to if one of the "ret" functions really "needs" to show something to the user later.

You might consider setting up a new page whenever ty goes out the bottom of the old one.