BrunoZockt

TerminalAPI

Nov 17th, 2020
1,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. function xPrint(str, x, y)
  2.   term.setCursorPos(x, y)
  3.   term.write(str)
  4. end
  5.  
  6. function printCentered(str, y)
  7.   term.setCursorPos(w/2 - #str/2 + 1, y)
  8.   term.write(str)
  9. end
  10.  
  11. function printRight(str, y, xoffset)
  12.   xoffset = xoffset or 0
  13.   term.setCursorPos(w - xoffset - (#str - 1), y)
  14.   term.write(str)
  15. end
  16.  
  17. function printLeft(str, y, xoffset)
  18.   xoffset = xoffset or 0
  19.   term.setCursorPos(1 + xoffset, y)
  20.   term.write(str)
  21. end
  22.  
  23. function printSpacedOut(tab, y, leftEdge, rightEdge)
  24.   rightEdge = rightEdge or w
  25.   leftEdge = leftEdge or 0
  26.   local letters = 0
  27.   for i in pairs(tab) do
  28.     letters = letters + #tab[i]
  29.   end
  30.   local freeSpace = (rightEdge-leftEdge)+1-letters
  31.   term.setCursorPos(leftEdge, y)
  32.   if (freeSpace/(#tab+1))%1 == 0 then
  33.     for i in ipairs(tab) do
  34.       term.write(string.rep(" ", freeSpace/(#tab+1))..tab[i])
  35.     end
  36.   elseif (freeSpace/(#tab))%2 == 0 then
  37.     term.write(string.rep(" ", (freeSpace/#tab)/2))
  38.     for i in ipairs(tab) do
  39.       term.write(tab[i]..string.rep(" ", freeSpace/#tab))
  40.     end
  41.   elseif freeSpace == #tab-1 then
  42.     term.write(tab[1])
  43.     for i = 2, #tab do
  44.       term.write(" "..tab[i])
  45.     end
  46.   else
  47.     local spacing
  48.     for i in ipairs(tab) do
  49.       spacing = round(freeSpace/(#tab-i+2))
  50.       term.write(string.rep(" ", spacing)..tab[i])
  51.       freeSpace = freeSpace - spacing
  52.     end
  53.   end
  54. end
  55.  
  56. --tab must be a list of strings with no string longer than space
  57. function printWrapped(tab, x, y, space, sep)
  58.   local sep = sep or " "
  59.   local spaceLeft = space
  60.   local leftEdge = x
  61.   for word = 1, #tab do
  62.     if space < #tab[word] + #sep then
  63.       return false
  64.     elseif spaceLeft >= #tab[word]+#sep then
  65.       xPrint(tab[word]..sep, x, y)
  66.       spaceLeft = spaceLeft - (#tab[word]+#sep)
  67.       x += #tab[word]+#sep
  68.     else
  69.       y++
  70.       spaceLeft = space
  71.       xPrint(tab[word]..sep, x, y)
  72.       spaceLeft = spaceLeft - (#tab[word]+#sep)
  73.       x = leftEdge + (#tab[word]+#sep)
  74.     end
  75.   end
  76.   return y
  77. end
  78.  
  79. function verticalLine(x, y1, y2, startSymbol, endSymbol)
  80.   for i = y1, y2 do
  81.     xPrint("|", x, i)
  82.   end
  83.   if startSymbol then
  84.     xPrint(startSymbol, x, y1)
  85.     xPrint((endSymbol or startSymbol), x, y2)
  86.   end
  87. end
  88.  
  89. function split(inputstr, sep)
  90.   if inputstr == nil then
  91.     return nil
  92.   elseif sep == nil then
  93.     sep = "%s"
  94.   end
  95.   local t, i = {}, 1
  96.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  97.     t[i] = str
  98.     i = i + 1
  99.   end
  100.   return t
  101. end
  102.  
  103. function round(float, dez, half)
  104.   half = half or 0.5
  105.   dez = dez or 1
  106.   return math.floor(float/dez+half)*dez
  107. end
  108.  
  109. function close()
  110.   term.clear()
  111.   term.setCursorPos(1,1)
  112.   if term.isColor() then
  113.     term.setTextColor(colors.yellow)
  114.     print(os.version())
  115.     term.setTextColor(colors.white)
  116.   else
  117.     print(os.version())
  118.   end
  119.   term.setCursorPos(1, 2)
  120. end
  121.  
Advertisement
Add Comment
Please, Sign In to add comment