Advertisement
faubiguy

Terminal redirect API

Mar 27th, 2013
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 KB | None | 0 0
  1. function newTerm(x, y, width, height, baseTerm)
  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 = false;
  9.     local terminal -- Terminal redirect object
  10.     local baseTerm = baseTerm or 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 outputLine(rowNum, keepCursor)
  21.         for j=1,width do
  22.             baseTerm.setCursorPos(x+j-1, y+rowNum-1);
  23.             baseTerm.setTextColor(textColors[rowNum][j]);
  24.             baseTerm.setBackgroundColor(backgroundColors[rowNum][j]);
  25.             baseTerm.write(characters[rowNum][j]);
  26.         end
  27.     end
  28.    
  29.     local function output()
  30.         baseTerm.setCursorBlink(false)
  31.         local cursorX, cursorY = baseTerm.getCursorPos()
  32.         for i=1,height do
  33.             outputLine(height);
  34.         end
  35.         baseTerm.setCursorPos(cursorX, cursorY)
  36.         baseTerm.setCursorBlink(cursorBlink)
  37.     end
  38.    
  39.     local function newCharTable() -- Creates an empty character table (table of empty rows)
  40.         local charTable = {};
  41.         for i=1,height do
  42.             charTable[i] = newRow();
  43.         end
  44.         return charTable;
  45.     end
  46.    
  47.     local function newColorRow(color) -- Creates a row of whatever the color is
  48.         local row = {}
  49.         for i=1,width do
  50.             row[i] = color
  51.         end
  52.         return row
  53.     end
  54.    
  55.     local function newColorTable(color) -- Creates a table of whatever the color is
  56.         local colorTable = {}
  57.         for i=1,height do
  58.             colorTable[i]=newColorRow(color)
  59.         end
  60.         return colorTable
  61.     end
  62.    
  63.     local function setup() -- Sets cursor position and color before writing to terminal
  64.         baseTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
  65.         baseTerm.setTextColor(textColor)
  66.         baseTerm.setBackgroundColor(backgroundColor)
  67.         baseTerm.setCursorBlink(cursorBlink)
  68.     end
  69.    
  70.     terminal = {
  71.         scroll = function(times)
  72.             for i = 1,times do
  73.                 table.remove(characters, 1) -- Remove first row
  74.                 characters[#characters+1] = newRow() -- Add empty row at the end
  75.                 table.remove(textColors, 1)
  76.                 textColors[#textColors+1] = newColorRow(textColor)
  77.                 table.remove(backgroundColors, 1)
  78.                 backgroundColors[#backgroundColors+1] = newColorRow(backgroundColor)
  79.             end
  80.             times = -times -- Reverse for scrolling up
  81.             for i = 1,times do
  82.                 characters[#characters] = nil -- Remove last row
  83.                 table.insert(characters, 1, newRow()) -- Add empty row at the beginning
  84.                 textColors[#textColors] = nil
  85.                 table.insert(textColors, 1, newColorRow(textColor))
  86.                 backgroundColors[#backgroundColors] = nil
  87.                 table.insert(backgroundColors, 1, newColorRow(backgroundColor))
  88.             end
  89.             cursorPosY = cursorPosY + times;
  90.             if cursorPosY < 1 then
  91.                 cursorPosY = 1
  92.             end
  93.             if cursorPosY > height then
  94.                 cursorPosY = height
  95.             end
  96.         end,
  97.         setCursorPos = function(newX, newY)
  98.             --if newX <= width and newX >= 1 then
  99.                 cursorPosX = newX
  100.             --end
  101.             --if newY <= height and newY >= 1 then
  102.                 cursorPosY = newY
  103.             --end
  104.             baseTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
  105.         end,
  106.         write = function(text)
  107.             setup()
  108.             local totalLength = text:len();        
  109.             if cursorPosY>=1 and cursorPosY <= height then
  110.                 local textLength = math.min(math.max(0,width-cursorPosX+1), totalLength)
  111.                 local textStart = 1
  112.                 if cursorPosX < 1 then textStart = 1 - cursorPosX end
  113.                 text = text:sub(textStart, textLength)
  114.                 baseTerm.write(text)
  115.                 for i=1,textLength do
  116.                     if cursorPosX+i-1 >=1 and cursorPosX+i-1 <= width then
  117.                         characters[cursorPosY][cursorPosX+i-1] = text:sub(i,i)
  118.                         textColors[cursorPosY][cursorPosX+i-1] = textColor
  119.                         backgroundColors[cursorPosY][cursorPosX+i-1] = backgroundColor
  120.                     end
  121.                 end
  122.             end
  123.             cursorPosX = cursorPosX + totalLength;
  124.             --[[if cursorPosX > width then
  125.                 cursorPosX = width
  126.             end]]
  127.             terminal.setCursorPos(cursorPosX, cursorPosY);
  128.         end,
  129.         clear = function()
  130.             characters=newCharTable()
  131.             textColors = newColorTable(textColor)
  132.             backgroundColors = newColorTable(backgroundColor)
  133.             output()
  134.         end,
  135.         setTextColor=function(color)
  136.             textColor = color
  137.             --baseTerm.setTextColor(color);
  138.         end,
  139.         setBackgroundColor = function(color)
  140.             backgroundColor = color
  141.             --baseTerm.setBackgroundColor(color)
  142.         end,
  143.         getTextColor=function()
  144.             return textColor
  145.         end,
  146.         getBackgroundColor = function()
  147.             return backgroundColor
  148.         end,
  149.         getCursorPos = function()
  150.             return cursorPosX, cursorPosY
  151.         end,
  152.         setCursorBlink = function(blink)
  153.             cursorBlink = blink
  154.             baseTerm.setCursorBlink(blink)
  155.         end,
  156.         clearLine = function()
  157.             if cursorPosY<1 or cursorPosY>height then return end
  158.             characters[cursorPosY] = newRow() -- Set current row to empty row
  159.             textColors[cursorPosY] = newColorRow(textColor)
  160.             backgroundColors[cursorPosY] = newColorRow(backgroundColor)
  161.             baseTerm.setCursorBlink(false)
  162.             local cursorX, cursorY = baseTerm.getCursorPos()
  163.             outputLine(cursorPosY, true)
  164.             baseTerm.setCursorBlink(cursorBlink)
  165.             baseTerm.setCursorPos(cursorX,cursorY)
  166.         end,
  167.         isColor = function()
  168.             return baseTerm.isColor()
  169.         end,
  170.         getSize = function()
  171.             return width, height
  172.         end,
  173.         getCursorBlink = function()
  174.             return cursorBlink
  175.         end
  176.     }
  177.     terminal.isColour = terminal.isColor
  178.     terminal.setBackgroundColour=terminal.setBackgroundColor
  179.     terminal.setTextColour=terminal.setTextColor
  180.     terminal.getBackgroundColour=terminal.getBackgroundColor
  181.     terminal.getTextColour=terminal.getTextColor
  182.    
  183.     terminal.clear()
  184.     terminal.setCursorPos(1,1)
  185.    
  186.     return terminal
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement