Advertisement
craniumkid22

SmartWrite

Sep 17th, 2012
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. function getVersion()
  2. return 1.006
  3. end
  4.  
  5. --justifying functions
  6.  
  7. function writeC(string,manX,manY,peripheral) --center justified
  8. if not peripheral then mon = term else mon = peripheral end
  9. local x,y=mon.getSize()
  10. local y = y/2
  11. local b = string.len(string)/2
  12. local x = (x/2)-b
  13. if manX==0 and manY==0 then
  14.     mon.setCursorPos(x,y)
  15.     mon.write(string)
  16. elseif manX==0 and manY~=0 then
  17.     mon.setCursorPos(x,manY)
  18.     mon.write(string)
  19. elseif manX~=0 and manY==0 then
  20.     mon.setCursorPos(x+manX,y)
  21.     mon.write(string)
  22. else error("Manual selection of X and Y is not necessary")
  23. end
  24. end
  25.  
  26. function writeRT(string,manX,manY,peripheral) --right justified
  27. if not peripheral then mon = term else mon = peripheral end
  28. local x,y=mon.getSize()
  29. local b = string.len(string)
  30. local x = x-b
  31. if manX==0 and manY==0 then
  32.     mon.setCursorPos(x,1)
  33.     mon.write(string)
  34. elseif manX~=0 and manY~=0 then
  35.     mon.setCursorPos(x+manX,manY)
  36.     mon.write(string)
  37. elseif manX~=0 and manY==0 then
  38.     mon.setCursorPos(x+manX,1)
  39.     mon.write(string)
  40. elseif manX==0 and manY~=0 then
  41.     mon.setCursorPos(x,manY)
  42.     mon.write(string)
  43. else error("Manual selection of X and Y is not necessary")
  44. end
  45. end
  46.  
  47. function fractionWrite(string,percentageX,percentageY,peripheral) --fraction justified. Usage would be as api.fractionWrite("This is the string",(1/3),(2/3))
  48. if not peripheral then mon = term else mon = peripheral end
  49. local x,y = mon.getSize()
  50. local b = string.len(string)
  51. local x = x*percentageX
  52. local y = y*percentageY
  53. if percentageX ~= 0 and percentageY ~= 0 then
  54.     mon.setCursorPos(x,y)
  55.     mon.write(string)
  56. else error("X or Y result must be greater than 0")
  57. end
  58. end
  59.  
  60. --pattern functions
  61.  
  62. function writeV(string,wX,wY,peripheral)  --will write the string in a vertical line for each character.
  63. if not peripheral then mon = term else mon = peripheral end
  64. local function separate(input) --Good for making on screen tables.
  65.     local chars = {}
  66.     for x=1,string.len(input) do
  67.         table.insert(chars,string.sub(input,x,x))
  68.     end
  69. return chars
  70. end
  71. local sepString = separate(string)
  72. local pY = wY - 1
  73. local tX,tY = mon.getSize()
  74. term.setCursorPos(wX,wY)
  75. if wX ~= nil and wX ~= 0 then
  76.     if wY ~= nil and wY ~= 0 then
  77.         for i = 1,#sepString do
  78.             if pY<=tY then
  79.                 mon.setCursorPos(wX,pY+1)
  80.                 mon.write(sepString[i])
  81.             elseif pY>tY then
  82.                 mon.setCursorPos(wX,(pY-i)+1)
  83.                 mon.write(sepString[i])
  84.             end
  85.         end
  86.     else error("Y coordinate cannot be nil or 0")
  87.     end
  88. else error("X coordinate cannot be nil or 0")
  89. end
  90. end
  91.  
  92. --clearing functions
  93.  
  94. function clear(x,y,peripheral) --will clear the screen, and return to the specified x,y position
  95. if not peripheral then mon = term else mon = peripheral end
  96. mon.clear()
  97. mon.setCursorPos(x,y)
  98. end
  99.  
  100. function clearLine(x,y,peripheral) --will clear the specified line.
  101. if not peripheral then mon = term else mon = peripheral end
  102. local tX,tY = term.getCursorPos()
  103. mon.setCursorPos(x,y)
  104. mon.clearLine()
  105. mon.setCursorPos(tX,tY)
  106. end
  107.  
  108. -- limit read function
  109. function limitRead(limX, rChar)
  110. term.setCursorBlink(true)
  111. local origX, origY = term.getCursorPos()
  112. local returnString = ""
  113. while true do
  114.     local xPos, yPos = term.getCursorPos()
  115.     local event, p1, p2 = os.pullEvent()
  116.     if event == "char" then
  117.         returnString = returnString..p1
  118.         if not rChar then
  119.             if not limX then
  120.                 write(p1)
  121.             else
  122.                 if string.len(returnString) >= limX then
  123.                     term.setCursorPos(origX, origY)
  124.                     write(string.sub(returnString, (string.len(returnString)-limX)+1))
  125.                 elseif string.len(returnString) < limX then
  126.                     write(p1)
  127.                 end
  128.             end
  129.         else
  130.             if not limX then
  131.                 write(rChar)
  132.             else
  133.                 if string.len(returnString) >= limX then
  134.                     term.setCursorPos(origX, origY)
  135.                     write(string.rep(rChar, limX))
  136.                 elseif string.len(returnString) < limX then
  137.                     write(rChar)
  138.                 end
  139.             end
  140.         end
  141.     elseif event == "key" and p1 == 14 then --backspace
  142.         returnString = string.sub(returnString, 1, (string.len(returnString))-1)
  143.         term.setCursorPos(xPos-1,yPos)
  144.         write(" ")
  145.         term.setCursorPos(origX, origY)
  146.         if string.len(returnString) >= limX then
  147.             if not rChar then
  148.                 write(string.sub(returnString, (string.len(returnString)-limX)+1))
  149.             else
  150.                 write(string.rep(rChar,limX))
  151.             end
  152.         else
  153.             if not rChar then
  154.                 write(returnString)
  155.             else
  156.                 write(string.rep(rChar, string.len(returnString)))
  157.             end
  158.         end
  159.     elseif event == "key" and p1 == 28 then --enter
  160.         break
  161.     end
  162. end
  163. term.setCursorBlink(false)
  164. return returnString
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement