Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function newTerm(x, y, width, height)
- 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 = true;
- local terminal -- Terminal redirect object
- local oldTerm = 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 output()
- local cursorX, cursorY = terminal.getCursorPos()
- for i=1,height do
- for j=1,width do
- oldTerm.setCursorPos(x+j-1, y+i-1)
- oldTerm.setTextColor(textColors[i][j])
- oldTerm.setBackgroundColor(backgroundColors[i][j])
- oldTerm.write(characters[i][j])
- end
- end
- terminal.setCursorPos(cursorX, cursorY)
- 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
- oldTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
- oldTerm.setTextColor(textColor)
- oldTerm.setBackgroundColor(backgroundColor)
- oldTerm.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
- oldTerm.setCursorPos(x+cursorPosX-1,y+cursorPosY-1)
- end,
- write = function(text)
- setup()
- local textLength = math.min(width-cursorPosX, text:len())
- text = text:sub(1, textLength)
- oldTerm.write(text)
- for i=1,textLength do
- characters[cursorPosY][cursorPosX+i-1] = text:sub(i,i)
- end
- cursorPosX = cursorPosX + textLength
- 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
- oldTerm.setTextColor(color);
- end,
- setBackgroundColor = function(color)
- backgroundColor = color
- oldTerm.setBackgroundColor(color)
- end,
- getCursorPos = function()
- return cursorPosX, cursorPosY
- end,
- setCursorBlink = function(blink)
- cursorBlink = blink
- oldTerm.setCursorBlink(blink)
- end,
- clearLine = function()
- characters[cursorPosY] = newRow() -- Set current row to empty row
- textColors[cursorPosY] = newColorRow(textColor)
- backgroundColors[cursorPosY] = newColorRow(backgroundColor)
- output()
- end,
- isColor = function()
- return oldTerm.isColor()
- end,
- getSize = function()
- return width, height
- end
- }
- terminal.isColour = terminal.isColor
- terminal.setBackgroundColour=terminal.setBackgroundColor
- terminal.setTextColour=terminal.setTextColor
- terminal.clear()
- terminal.setCursorPos(1,1)
- return terminal
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement