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

[API] SmartWrite: New write API for easier writing.

Started by Cranium, 18 September 2012 - 03:39 PM
Cranium #1
Posted 18 September 2012 - 05:39 PM
I have written my first API to be used to help shorten some of the tedious calculations most of us go through to have the words we type show up in a specific area of the screen. Included is a center printing function, right justifying, fraction justifying, and vertical printing. Also included, but not too noteworthy is a terminal clearing function, that clears, the whole screen, and returns to the specified x and y positions. This will save most people ONE WHOLE LINE OF CODE! The same goes for the line clearing function, but it will just clear the specified line, and return to the previous position, saving up to three whole lines. This is my first API, so I would greatly appreciate any suggestions or constructive criticism as I learn to make this better.
Code:
http://pastebin.com/dXamY7Jc

Methods:
SpoilerwriteC()
Spoiler

smartWrite.writeC(string,x,y,peripheral)
  --The string to center in the screen is defined as string, then you select EITHER x or y positions to call to
  --If you do not use one or the other, then enter 0 as the value for the position you do not want to use.
  --if neither is defined, it will write to the center of both x and y axis.
  --Peripheral is the definition of the wrapper you defined, such as for the monitor. Define by using:
peripheral = peripheral.wrap("side")

writeRT()
Spoiler

smartWrite.writeRT(string,x,y,peripheral)
  --This is the same as writeC, with the exception that it will write to
  --the right hand side of the screen for any string defined.

fractionWrite()
Spoiler

smartWrite.fractionWrite(string,percentageX,percentageY,peripheral)
  --Essentially gives the ability to define a fraction of the screen to print to.
  --Example would be:
smartWrite.fractionWrite(string,1/3,1/4)
  --This will print starting on the first third of the x axis, while also printing on the first quarter of the y axis.

writeV()
Spoiler

smartWrite.writeV(string,x,y,peripheral)
  --This will write your strings vertically. No idea why you would want this, but there you go.
  --Define the x position to start at, and y defines the y position to start at.
  --Future versions, (if I ever do them) will allow for more functions, and improvements on the
  --existing functions...

clear()
Spoiler

smartWrite.clear(x,y,peripheral)
  --This will clear the screen or monitor, and return to the specified positions.

clearLine()
Spoiler

smartWrite.clearLine(x,y,peripheral)
  --This will only clear the specified line, and return back to the previous line it was on.

limitRead()
Spoiler

smartWrite.limitRead(limit, replaceCharacter)
  --This will limit the normal read function to only the specified width that you are needing.
  --limit is the number of characters you want to limit the read function to write to.
  --For example, limitRead(5) will only print 5 characters (12345)
  --If the string exceeds the limit, it will scroll with the new text. (23456)
  --It will still return the entire string, but only writes in that limited area.
  --You can still define a replacement character if you use this for passwords (for example)
  --Use limitRead(5, "*") to replace the entire string with asterisks on the screen.
Edited on 09 November 2012 - 12:01 PM
GopherAtl #2
Posted 18 September 2012 - 05:56 PM
Simple but useful, no particular criticisms except to say it would be easier to follow if you fix the indentation throughout and threw some blank lines to separate functions, it sortof runs together right now.

As for suggestions, you could add support for an optional monitor parameter to each function. Just replace term with that parameter and, at the start, if it's nil set it to term. When the new version comes out, you might be able to support printers the same way (not certain, not in beta so dunno what the actual printer peripheral interface is like).
Cranium #3
Posted 18 September 2012 - 06:01 PM
I didn't even think about monitors, but I can see where it would be a little more useful there. So just do local term = term or mon? Not sure how i would implement monitors in. As I said, it's my first API.
GopherAtl #4
Posted 18 September 2012 - 06:08 PM
well, you could use a local term variable, I was thinking of using the parameter

ex using clear:


function clear(x,y,screen) --will clear the screen, and return to the specified x,y position
  --default screen to term if it is nil
  if not screen then screen=term end
  --use screen from here on
  screen.clear()
  screen.setCursorPos(x,y)
end
Cranium #5
Posted 18 September 2012 - 07:21 PM
So for example:

function clear(x,y,peripheral)
if not peripheral then mon = term end
mon.clear()
mon.setCursorPos(x,y)
end
So just to make sure, the user would have to use their own peripheral.wrap() function to determine the peripheral parameter, right?
GopherAtl #6
Posted 18 September 2012 - 07:34 PM
well, yeah, in my example. You could further enhance it as you see fit :)/>/>
Lyqyd #7
Posted 18 September 2012 - 07:39 PM
While a valid suggestion, it is not required to enable use with monitors. If output is redirected, the term functions automatically refer to the correct location.
GopherAtl #8
Posted 18 September 2012 - 07:44 PM
true, but it is requried to display to terminal and monitor from one program withouth redirecting the terminal all the time. Ex, monitor can be dedicated status display while terminal shows log and command input area. Certainly there are more sophisticated solutions possible, but simple isn't necessarily bad. :)/>/>
Cranium #9
Posted 18 September 2012 - 07:49 PM
Actually, I agree with GopherAtl, since I use that very function to set up my Master Control System monitor output. It runs the monitor while simultaneously running the terminal. So I think I will have to add monitor support. This gives me a few ideas for more functions as well.
KaoS #10
Posted 19 September 2012 - 06:35 AM
all that I would do in this situation is term redirect and use term.native.command for the local changes
cheekycharlie101 #11
Posted 21 September 2012 - 08:25 PM
cant get it to work :)/>/> can someone help me out with this?
Cranium #12
Posted 21 September 2012 - 08:27 PM
  1. Is it in your APIs folder?
  2. How are you calling back to it?
  3. If not in your APIs folder, where?
cheekycharlie101 #13
Posted 03 October 2012 - 06:58 PM
Hey, i was using your write api a while ago. but now ive forgotten. can you please help me out.
thanks -Cheeky
Cranium #14
Posted 03 October 2012 - 08:22 PM
I have updated a list of function calls in the OP, and how to call to them properly, if you don't understand the code.
Cranium #15
Posted 09 November 2012 - 01:07 PM
I added a new function:

smartWrite.limitRead()
Now you don't have to have the read() or io.read() functions delete the end of your masterful ASCII art when you need to collect a string from the user!
Cross_Sans #16
Posted 18 February 2016 - 01:47 PM
I'm so sorry uping this post, i don't watched the time :(/>
Edited on 18 February 2016 - 12:48 PM
DannySMc #17
Posted 18 February 2016 - 02:21 PM
I'm so sorry uping this post, i don't watched the time :(/>/>

Don't be! I have been looking for this type of thing for ages now, well the limitRead function anyway!!! So thank you! Just saved my day!