3790 posts
Location
Lincoln, Nebraska
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/dXamY7JcMethods:Spoiler
writeC()
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
871 posts
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).
3790 posts
Location
Lincoln, Nebraska
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.
871 posts
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
3790 posts
Location
Lincoln, Nebraska
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?
871 posts
Posted 18 September 2012 - 07:34 PM
well, yeah, in my example. You could further enhance it as you see fit :)/>/>
8543 posts
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.
871 posts
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. :)/>/>
3790 posts
Location
Lincoln, Nebraska
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.
1548 posts
Location
That dark shadow under your bed...
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
214 posts
Posted 21 September 2012 - 08:25 PM
cant get it to work :)/>/> can someone help me out with this?
3790 posts
Location
Lincoln, Nebraska
Posted 21 September 2012 - 08:27 PM
- Is it in your APIs folder?
- How are you calling back to it?
- If not in your APIs folder, where?
214 posts
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
3790 posts
Location
Lincoln, Nebraska
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.
3790 posts
Location
Lincoln, Nebraska
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!
122 posts
Location
France
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
1847 posts
Location
/home/dannysmc95
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!