Advertisement
guitarplayer616

BUFFER

Jun 23rd, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. local w,h = term.getSize()
  2. local buffer = {}
  3.  
  4. function initBuffer()
  5.     for x=1,w do
  6.         buffer[x] = {}
  7.         for y = 1,h do
  8.             buffer[x][y] = {}
  9.         end
  10.     end
  11. end
  12.        
  13. function writeToBuffer(char,col,back,x,y)
  14.     buffer[x][y] = {char,col,back}
  15. end
  16.  
  17. function displayBuffer()
  18.     rednet.broadcast(textutils.serialize(buffer),"stream")
  19.     for x = 1, #buffer do
  20.         for y = 1, #buffer[x] do
  21.             term.setCursorPos(x,y)
  22.             if #buffer[x][y] == 3 then
  23.                 term.setTextColor(buffer[x][y][2])
  24.                 term.setBackgroundColor(buffer[x][y][3])
  25.                 term.write(buffer[x][y][1])
  26.             end
  27.         end
  28.     end
  29. end
  30.  
  31. term.clear()
  32. initBuffer()
  33. term.setCursorPos(1,1)
  34. local cx,cy = 1,1
  35. while true do
  36.     local e = {coroutine.yield()}
  37.     if e[1] == "char" then
  38.         writeToBuffer(e[2],123,43,cx,cy)
  39.         displayBuffer()
  40.         if cx == w then
  41.             cx = 1
  42.             cy = cy + 1
  43.         elseif cx < w then
  44.             cx = cx + 1
  45.         end
  46.     elseif e[1] == "key" then
  47.         if e[2] == keys.enter then
  48.             cy = cy + 1
  49.             cx = 1
  50.         elseif e[2] == keys.backspace then
  51.             if cx > 1 then
  52.                 cx = cx - 1
  53.             elseif cx == 1 then
  54.                 cx = w
  55.                 cy = cy - 1
  56.             end
  57.             writeToBuffer(" ",colors.black,colors.black,cx,cy)
  58.             displayBuffer()
  59.         end
  60.     end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement