Advertisement
faubiguy

Terminal redirect

Mar 26th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. function newTerm(x, y, width, height)
  2.     local textColor = colors.white
  3.     local backgroundColor = colors.black
  4.     local characters -- Characters as table of rows, i.e. a given is at characters[y][x]
  5.     local textColors -- Text colors, same as above
  6.     local backgroundColors -- Background colors, same as above
  7.     local cursorPosX, cursorPosY = 1, 1
  8.     local cursorBlink = true;
  9.     local terminal -- Terminal redirect object
  10.     local oldTerm = term.native;
  11.    
  12.     local function newRow() -- Creates an empty row (table of spaces)
  13.         local row = {}
  14.         for i=1,width do
  15.             row[i] = ' '
  16.         end
  17.         return row
  18.     end
  19.    
  20.     local function output()
  21.         local cursorX, cursorY = terminal.getCursorPos()
  22.         for i=1,height do
  23.             for j=1,width do
  24.                 oldTerm.setCursorPos(x+j-1, y+i-1)
  25.                 oldTerm.setTextColor(textColors[i][j])
  26.                 oldTerm.setBackgroundColor(backgroundColors[i][j])
  27.                 oldTerm.write(characters[i][j])
  28.             end
  29.         end
  30.         terminal.setCursorPos(cursorX, cursorY)
  31.     end
  32.    
  33.     local function newCharTable() -- Creates an empty character table (table of empty rows)
  34.         local charTable = {}
  35.         for i=1,height do
  36.             charTable[i] = newRow()
  37.         end
  38.         return charTable
  39.     end
  40.    
  41.     local function newColorRow(color) -- Creates a row of whatever the color is
  42.         local row = {}
  43.         for i=1,width do
  44.             row[i] = color
  45.         end
  46.         return row
  47.     end
  48.    
  49.     local function newColorTable(color) -- Creates a table of whatever the color is
  50.         local colorTable = {}
  51.         for i=1,height do
  52.             colorTable[i]=newColorRow(color)
  53.         end
  54.         return colorTable
  55.     end
  56.    
  57.     local function setup() -- Sets cursor position and color before writing to terminal
  58.         oldTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
  59.         oldTerm.setTextColor(textColor)
  60.         oldTerm.setBackgroundColor(backgroundColor)
  61.         oldTerm.setCursorBlink(cursorBlink)
  62.     end
  63.    
  64.     terminal = {
  65.         scroll = function(times)
  66.             for i = 1,times do
  67.                 table.remove(characters, 1) -- Remove first row
  68.                 characters[#characters+1] = newRow() -- Add empty row at the end
  69.                 table.remove(textColors, 1)
  70.                 textColors[#textColors+1] = newColorRow(textColor)
  71.                 table.remove(backgroundColors, 1)
  72.                 backgroundColors[#backgroundColors+1] = newColorRow(backgroundColor)
  73.             end
  74.             times = -times -- Reverse for scrolling up
  75.             for i = 1,times do
  76.                 characters[#characters] = nil -- Remove last row
  77.                 table.insert(characters, 1, newRow()) -- Add empty row at the beginning
  78.                 textColors[#textColors] = nil
  79.                 table.insert(textColors, 1, newColorRow(textColor))
  80.                 backgroundColors[#backgroundColors] = nil
  81.                 table.insert(backgroundColors, 1, newColorRow(backgroundColor))
  82.             end
  83.             cursorPosY = cursorPosY + times;
  84.             if cursorPosY < 1 then
  85.                 cursorPosY = 1
  86.             end
  87.             if cursorPosY > height then
  88.                 cursorPosY = height
  89.             end
  90.         end,
  91.         setCursorPos = function(newX, newY)
  92.             if newX <= width and newX >= 1 then
  93.                 cursorPosX = newX
  94.             end
  95.             if newY <= height and newY >= 1 then
  96.                 cursorPosY = newY
  97.             end
  98.             oldTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
  99.         end,
  100.         write = function(text)
  101.             setup()        
  102.             local textLength = math.min(width-cursorPosX, text:len())
  103.             text = text:sub(1, textLength)
  104.             oldTerm.write(text)
  105.             for i=1,textLength do
  106.                 characters[cursorPosY][cursorPosX+i-1] = text:sub(i,i)
  107.             end
  108.             cursorPosX = cursorPosX + textLength
  109.             if cursorPosX > width then
  110.                 cursorPosX = width
  111.             end
  112.             terminal.setCursorPos(cursorPosX, cursorPosY);
  113.         end,
  114.         clear = function()
  115.             characters=newCharTable()
  116.             textColors = newColorTable(textColor)
  117.             backgroundColors = newColorTable(backgroundColor)
  118.             output()
  119.         end,
  120.         setTextColor=function(color)
  121.             textColor = color
  122.             oldTerm.setTextColor(color);
  123.         end,
  124.         setBackgroundColor = function(color)
  125.             backgroundColor = color
  126.             oldTerm.setBackgroundColor(color)
  127.         end,
  128.         getCursorPos = function()
  129.             return cursorPosX, cursorPosY
  130.         end,
  131.         setCursorBlink = function(blink)
  132.             cursorBlink = blink
  133.             oldTerm.setCursorBlink(blink)
  134.         end,
  135.         clearLine = function()
  136.             characters[cursorPosY] = newRow() -- Set current row to empty row
  137.             textColors[cursorPosY] = newColorRow(textColor)
  138.             backgroundColors[cursorPosY] = newColorRow(backgroundColor)
  139.             output()
  140.         end,
  141.         isColor = function()
  142.             return oldTerm.isColor()
  143.         end,
  144.         getSize = function()
  145.             return width, height
  146.         end
  147.     }
  148.     terminal.isColour = terminal.isColor
  149.     terminal.setBackgroundColour=terminal.setBackgroundColor
  150.     terminal.setTextColour=terminal.setTextColor
  151.    
  152.     terminal.clear()
  153.     terminal.setCursorPos(1,1)
  154.    
  155.     return terminal
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement