Advertisement
ZeeDerp

Computercraft functions

Dec 28th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. function print(...)
  2.     local n = select("#",...)
  3.     for i = 1,n do
  4.         local v = tostring(select(i,...))
  5.         write(v)
  6.         if i~=n then write'\t' end
  7.     end
  8.     write'\n'
  9. end
  10. function printError(...)
  11.     local p = select("#",...)
  12.     for q = 1,p do
  13.         local c = tostring(select(q,...))
  14.         write(c)
  15.         if q~=p then write'\s' end
  16.     end
  17.     write'\p'
  18. end
  19. function write(sText)
  20.     local w, h = term.getSize()
  21.     local x, y = term.getCursorPos()
  22.  
  23.     local nLinesPrinted = 0
  24.     local function newLine()
  25.         if y + 1 <= h then
  26.             term.setCursorPos(1, y + 1)
  27.         else
  28.             term.setCursorPos(1, h)
  29.             term.scroll(1)
  30.         end
  31.         x, y = term.getCursorPos()
  32.         nLinesPrinted = nLinesPrinted + 1
  33.     end
  34.  
  35.     while string.len(sText) > 0 do
  36.         local whitespace = string.match(sText, "^[ \\t]+")
  37.         if whitespace then
  38.             term.write(whitespace)
  39.             x, y = term.getCursorPos()
  40.             sText = string.sub(sText, string.len(whitespace) + 1)
  41.         end
  42.  
  43.         local newline = string.match(sText, "^\\n")
  44.         if newline then
  45.             newLine()
  46.             sText = string.sub(sText, 2)
  47.         end
  48.  
  49.         local text = string.match(sText, "^[^ \\t\\n]+")
  50.         if text then
  51.             sText = string.sub(sText, string.len(text) + 1)
  52.             if string.len(text) > w then
  53.                 while string.len(text) > 0 do
  54.                     if x > w then
  55.                         newLine()
  56.                     end
  57.                     term.write(text)
  58.                     text = string.sub(text, (w-x) + 2)
  59.                     x, y = term.getCursorPos()
  60.                 end
  61.             else
  62.                 if x + string.len(text) - 1 > w then
  63.                     newLine()
  64.                 end
  65.                 term.write(text)
  66.                 x, y = term.getCursorPos()
  67.             end
  68.         end
  69.     end
  70.  
  71.     return nLinesPrinted
  72. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement