Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function newTerm(x, y, width, height, baseTerm)
- local textColor = colors.white
- local backgroundColor = colors.black
- local characters -- Characters as table of rows, i.e. a given is at characters[y][x]
- local textColors -- Text colors, same as above
- local backgroundColors -- Background colors, same as above
- local cursorPosX, cursorPosY = 1, 1
- local cursorBlink = false;
- local terminal -- Terminal redirect object
- local baseTerm = baseTerm or term.native;
- local function newRow() -- Creates an empty row (table of spaces)
- local row = {}
- for i=1,width do
- row[i] = ' '
- end
- return row
- end
- local function outputLine(rowNum, keepCursor)
- for j=1,width do
- baseTerm.setCursorPos(x+j-1, y+rowNum-1);
- baseTerm.setTextColor(textColors[rowNum][j]);
- baseTerm.setBackgroundColor(backgroundColors[rowNum][j]);
- baseTerm.write(characters[rowNum][j]);
- end
- end
- local function output()
- baseTerm.setCursorBlink(false)
- local cursorX, cursorY = baseTerm.getCursorPos()
- for i=1,height do
- outputLine(height);
- end
- baseTerm.setCursorPos(cursorX, cursorY)
- baseTerm.setCursorBlink(cursorBlink)
- end
- local function newCharTable() -- Creates an empty character table (table of empty rows)
- local charTable = {};
- for i=1,height do
- charTable[i] = newRow();
- end
- return charTable;
- end
- local function newColorRow(color) -- Creates a row of whatever the color is
- local row = {}
- for i=1,width do
- row[i] = color
- end
- return row
- end
- local function newColorTable(color) -- Creates a table of whatever the color is
- local colorTable = {}
- for i=1,height do
- colorTable[i]=newColorRow(color)
- end
- return colorTable
- end
- local function setup() -- Sets cursor position and color before writing to terminal
- baseTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
- baseTerm.setTextColor(textColor)
- baseTerm.setBackgroundColor(backgroundColor)
- baseTerm.setCursorBlink(cursorBlink)
- end
- terminal = {
- scroll = function(times)
- for i = 1,times do
- table.remove(characters, 1) -- Remove first row
- characters[#characters+1] = newRow() -- Add empty row at the end
- table.remove(textColors, 1)
- textColors[#textColors+1] = newColorRow(textColor)
- table.remove(backgroundColors, 1)
- backgroundColors[#backgroundColors+1] = newColorRow(backgroundColor)
- end
- times = -times -- Reverse for scrolling up
- for i = 1,times do
- characters[#characters] = nil -- Remove last row
- table.insert(characters, 1, newRow()) -- Add empty row at the beginning
- textColors[#textColors] = nil
- table.insert(textColors, 1, newColorRow(textColor))
- backgroundColors[#backgroundColors] = nil
- table.insert(backgroundColors, 1, newColorRow(backgroundColor))
- end
- cursorPosY = cursorPosY + times;
- if cursorPosY < 1 then
- cursorPosY = 1
- end
- if cursorPosY > height then
- cursorPosY = height
- end
- end,
- setCursorPos = function(newX, newY)
- --if newX <= width and newX >= 1 then
- cursorPosX = newX
- --end
- --if newY <= height and newY >= 1 then
- cursorPosY = newY
- --end
- baseTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
- end,
- write = function(text)
- setup()
- local totalLength = text:len();
- if cursorPosY>=1 and cursorPosY <= height then
- local textLength = math.min(math.max(0,width-cursorPosX+1), totalLength)
- local textStart = 1
- if cursorPosX < 1 then textStart = 1 - cursorPosX end
- text = text:sub(textStart, textLength)
- baseTerm.write(text)
- for i=1,textLength do
- if cursorPosX+i-1 >=1 and cursorPosX+i-1 <= width then
- characters[cursorPosY][cursorPosX+i-1] = text:sub(i,i)
- textColors[cursorPosY][cursorPosX+i-1] = textColor
- backgroundColors[cursorPosY][cursorPosX+i-1] = backgroundColor
- end
- end
- end
- cursorPosX = cursorPosX + totalLength;
- --[[if cursorPosX > width then
- cursorPosX = width
- end]]
- terminal.setCursorPos(cursorPosX, cursorPosY);
- end,
- clear = function()
- characters=newCharTable()
- textColors = newColorTable(textColor)
- backgroundColors = newColorTable(backgroundColor)
- output()
- end,
- setTextColor=function(color)
- textColor = color
- --baseTerm.setTextColor(color);
- end,
- setBackgroundColor = function(color)
- backgroundColor = color
- --baseTerm.setBackgroundColor(color)
- end,
- getTextColor=function()
- return textColor
- end,
- getBackgroundColor = function()
- return backgroundColor
- end,
- getCursorPos = function()
- return cursorPosX, cursorPosY
- end,
- setCursorBlink = function(blink)
- cursorBlink = blink
- baseTerm.setCursorBlink(blink)
- end,
- clearLine = function()
- if cursorPosY<1 or cursorPosY>height then return end
- characters[cursorPosY] = newRow() -- Set current row to empty row
- textColors[cursorPosY] = newColorRow(textColor)
- backgroundColors[cursorPosY] = newColorRow(backgroundColor)
- baseTerm.setCursorBlink(false)
- local cursorX, cursorY = baseTerm.getCursorPos()
- outputLine(cursorPosY, true)
- baseTerm.setCursorBlink(cursorBlink)
- baseTerm.setCursorPos(cursorX,cursorY)
- end,
- isColor = function()
- return baseTerm.isColor()
- end,
- getSize = function()
- return width, height
- end,
- getCursorBlink = function()
- return cursorBlink
- end
- }
- terminal.isColour = terminal.isColor
- terminal.setBackgroundColour=terminal.setBackgroundColor
- terminal.setTextColour=terminal.setTextColor
- terminal.getBackgroundColour=terminal.getBackgroundColor
- terminal.getTextColour=terminal.getTextColor
- terminal.clear()
- terminal.setCursorPos(1,1)
- return terminal
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement