2019-02-06 04:03:57 +00:00
|
|
|
local Canvas = require('ui.canvas')
|
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
local colors = _G.colors
|
|
|
|
local term = _G.term
|
|
|
|
local _gsub = string.gsub
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
local Terminal = { }
|
2017-10-05 17:07:48 +00:00
|
|
|
|
2019-03-28 11:29:01 +00:00
|
|
|
local mapColorToGray = {
|
|
|
|
[ colors.white ] = colors.white,
|
|
|
|
[ colors.orange ] = colors.lightGray,
|
|
|
|
[ colors.magenta ] = colors.lightGray,
|
|
|
|
[ colors.lightBlue ] = colors.lightGray,
|
|
|
|
[ colors.yellow ] = colors.lightGray,
|
|
|
|
[ colors.lime ] = colors.lightGray,
|
|
|
|
[ colors.pink ] = colors.lightGray,
|
|
|
|
[ colors.gray ] = colors.gray,
|
|
|
|
[ colors.lightGray ] = colors.lightGray,
|
|
|
|
[ colors.cyan ] = colors.lightGray,
|
|
|
|
[ colors.purple ] = colors.gray,
|
|
|
|
[ colors.blue ] = colors.gray,
|
|
|
|
[ colors.brown ] = colors.gray,
|
|
|
|
[ colors.green ] = colors.lightGray,
|
|
|
|
[ colors.red ] = colors.gray,
|
|
|
|
[ colors.black ] = colors.black,
|
|
|
|
}
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
-- Replacement for window api with scrolling and buffering
|
|
|
|
function Terminal.window(parent, sx, sy, w, h, isVisible)
|
|
|
|
isVisible = isVisible ~= false
|
|
|
|
if not w or not h then
|
|
|
|
w, h = parent.getSize()
|
|
|
|
end
|
|
|
|
|
|
|
|
local win = { }
|
|
|
|
local maxScroll = 100
|
|
|
|
local cx, cy = 1, 1
|
|
|
|
local blink = false
|
|
|
|
local bg, fg = parent.getBackgroundColor(), parent.getTextColor()
|
|
|
|
|
|
|
|
local canvas = Canvas({
|
|
|
|
x = sx,
|
|
|
|
y = sy,
|
|
|
|
width = w,
|
|
|
|
height = h,
|
|
|
|
isColor = parent.isColor(),
|
2019-02-12 22:03:50 +00:00
|
|
|
offy = 0,
|
2019-02-06 04:03:57 +00:00
|
|
|
})
|
|
|
|
|
2019-02-07 08:10:05 +00:00
|
|
|
win.canvas = canvas
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
local function update()
|
|
|
|
if isVisible then
|
|
|
|
canvas:render(parent)
|
|
|
|
win.setCursorPos(cx, cy)
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
local function scrollTo(y)
|
|
|
|
y = math.max(0, y)
|
|
|
|
y = math.min(#canvas.lines - canvas.height, y)
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
if y ~= canvas.offy then
|
|
|
|
canvas.offy = y
|
|
|
|
canvas:dirty()
|
|
|
|
update()
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.write(str)
|
|
|
|
str = tostring(str) or ''
|
|
|
|
canvas:write(cx, cy + canvas.offy, str, bg, fg)
|
|
|
|
win.setCursorPos(cx + #str, cy)
|
|
|
|
update()
|
|
|
|
end
|
|
|
|
|
|
|
|
function win.blit(str, fg, bg)
|
|
|
|
canvas:blit(cx, cy + canvas.offy, str, bg, fg)
|
|
|
|
win.setCursorPos(cx + #str, cy)
|
|
|
|
update()
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.clear()
|
|
|
|
canvas.offy = 0
|
|
|
|
for i = #canvas.lines, canvas.height + 1, -1 do
|
|
|
|
canvas.lines[i] = nil
|
|
|
|
end
|
2019-02-12 01:25:12 +00:00
|
|
|
canvas:clear(bg, fg)
|
2019-02-06 04:03:57 +00:00
|
|
|
update()
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function win.clearLine()
|
2019-02-12 01:25:12 +00:00
|
|
|
canvas:clearLine(cy + canvas.offy, bg, fg)
|
2019-02-06 04:03:57 +00:00
|
|
|
win.setCursorPos(cx, cy)
|
|
|
|
update()
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.getCursorPos()
|
|
|
|
return cx, cy
|
|
|
|
end
|
|
|
|
|
|
|
|
function win.setCursorPos(x, y)
|
|
|
|
cx, cy = x, y
|
2019-02-18 08:17:29 +00:00
|
|
|
if isVisible then
|
|
|
|
parent.setCursorPos(x + canvas.x - 1, y + canvas.y - 1)
|
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function win.setCursorBlink(b)
|
|
|
|
blink = b
|
2019-02-18 08:17:29 +00:00
|
|
|
if isVisible then
|
|
|
|
parent.setCursorBlink(b)
|
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function win.isColor()
|
|
|
|
return canvas.isColor
|
|
|
|
end
|
|
|
|
win.isColour = win.isColor
|
|
|
|
|
|
|
|
function win.setTextColor(c)
|
|
|
|
fg = c
|
|
|
|
end
|
|
|
|
win.setTextColour = win.setTextColor
|
|
|
|
|
2019-02-07 08:10:05 +00:00
|
|
|
function win.getPaletteColor(n)
|
2019-02-09 00:21:20 +00:00
|
|
|
if parent.getPaletteColor then
|
|
|
|
return parent.getPaletteColor(n)
|
|
|
|
end
|
|
|
|
return 0, 0, 0
|
2019-02-07 08:10:05 +00:00
|
|
|
end
|
|
|
|
win.getPaletteColour = win.getPaletteColor
|
|
|
|
|
|
|
|
function win.setPaletteColor(n, r, g, b)
|
2019-02-09 00:21:20 +00:00
|
|
|
if parent.setPaletteColor then
|
|
|
|
return parent.setPaletteColor(n, r, g, b)
|
|
|
|
end
|
2019-02-07 08:10:05 +00:00
|
|
|
end
|
|
|
|
win.setPaletteColour = win.setPaletteColor
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.setBackgroundColor(c)
|
|
|
|
bg = c
|
|
|
|
end
|
|
|
|
win.setBackgroundColour = win.setBackgroundColor
|
|
|
|
|
|
|
|
function win.getSize()
|
|
|
|
return canvas.width, canvas.height
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.scroll(n)
|
|
|
|
n = n or 1
|
|
|
|
if n > 0 then
|
2019-02-12 01:25:12 +00:00
|
|
|
local lines = #canvas.lines
|
|
|
|
for i = 1, n do
|
|
|
|
canvas.lines[lines + i] = { }
|
|
|
|
canvas:clearLine(lines + i, bg, fg)
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
while #canvas.lines > maxScroll do
|
|
|
|
table.remove(canvas.lines, 1)
|
|
|
|
end
|
2019-02-12 01:25:12 +00:00
|
|
|
scrollTo(#canvas.lines)
|
2019-02-06 04:03:57 +00:00
|
|
|
canvas:dirty()
|
|
|
|
update()
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.getTextColor()
|
|
|
|
return fg
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
win.getTextColour = win.getTextColor
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.getBackgroundColor()
|
|
|
|
return bg
|
|
|
|
end
|
|
|
|
win.getBackgroundColour = win.getBackgroundColor
|
|
|
|
|
|
|
|
function win.setVisible(visible)
|
|
|
|
if visible ~= isVisible then
|
|
|
|
isVisible = visible
|
|
|
|
if isVisible then
|
|
|
|
canvas:dirty()
|
|
|
|
update()
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.redraw()
|
|
|
|
if isVisible then
|
|
|
|
canvas:dirty()
|
2019-02-12 22:03:50 +00:00
|
|
|
update()
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.restoreCursor()
|
2019-04-27 23:33:21 +00:00
|
|
|
if isVisible then
|
|
|
|
win.setCursorPos(cx, cy)
|
|
|
|
win.setTextColor(fg)
|
|
|
|
win.setCursorBlink(blink)
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.getPosition()
|
|
|
|
return canvas.x, canvas.y
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.reposition(x, y, width, height)
|
|
|
|
canvas.x, canvas.y = x, y
|
|
|
|
canvas:resize(width or canvas.width, height or canvas.height)
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[ Additional methods ]]--
|
2018-01-24 22:39:38 +00:00
|
|
|
function win.scrollDown()
|
2019-02-06 04:03:57 +00:00
|
|
|
scrollTo(canvas.offy + 1)
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.scrollUp()
|
|
|
|
scrollTo(canvas.offy - 1)
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.scrollTop()
|
|
|
|
scrollTo(0)
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function win.scrollBottom()
|
|
|
|
scrollTo(#canvas.lines)
|
|
|
|
end
|
|
|
|
|
|
|
|
function win.setMaxScroll(ms)
|
|
|
|
maxScroll = ms
|
|
|
|
end
|
|
|
|
|
|
|
|
function win.getCanvas()
|
|
|
|
return canvas
|
|
|
|
end
|
|
|
|
|
|
|
|
function win.getParent()
|
|
|
|
return parent
|
|
|
|
end
|
|
|
|
|
|
|
|
canvas:clear()
|
|
|
|
|
|
|
|
return win
|
2018-01-20 12:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- get windows contents
|
|
|
|
function Terminal.getContents(win, parent)
|
2018-01-24 22:39:38 +00:00
|
|
|
local oblit, oscp = parent.blit, parent.setCursorPos
|
|
|
|
local lines = { }
|
2018-01-20 12:18:13 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
parent.blit = function(text, fg, bg)
|
|
|
|
lines[#lines + 1] = {
|
|
|
|
text = text,
|
|
|
|
fg = fg,
|
|
|
|
bg = bg,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
parent.setCursorPos = function() end
|
2018-01-20 12:18:13 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
win.setVisible(true)
|
|
|
|
win.redraw()
|
2018-01-20 12:18:13 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
parent.blit = oblit
|
|
|
|
parent.setCursorPos = oscp
|
2018-01-20 12:18:13 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
return lines
|
2018-01-13 20:17:26 +00:00
|
|
|
end
|
|
|
|
|
2019-03-28 11:29:01 +00:00
|
|
|
function Terminal.colorToGrayscale(c)
|
|
|
|
return mapColorToGray[c]
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
|
2019-03-28 11:29:01 +00:00
|
|
|
function Terminal.toGrayscale(ct)
|
2018-01-24 22:39:38 +00:00
|
|
|
local methods = { 'setBackgroundColor', 'setBackgroundColour',
|
|
|
|
'setTextColor', 'setTextColour' }
|
|
|
|
for _,v in pairs(methods) do
|
|
|
|
local fn = ct[v]
|
|
|
|
ct[v] = function(c)
|
2019-03-28 11:29:01 +00:00
|
|
|
fn(mapColorToGray[c])
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local bcolors = {
|
|
|
|
[ '1' ] = '8',
|
|
|
|
[ '2' ] = '8',
|
|
|
|
[ '3' ] = '8',
|
|
|
|
[ '4' ] = '8',
|
|
|
|
[ '5' ] = '8',
|
|
|
|
[ '6' ] = '8',
|
|
|
|
[ '9' ] = '8',
|
|
|
|
[ 'a' ] = '7',
|
|
|
|
[ 'b' ] = '7',
|
|
|
|
[ 'c' ] = '7',
|
|
|
|
[ 'd' ] = '8',
|
|
|
|
[ 'e' ] = '7',
|
|
|
|
}
|
|
|
|
|
|
|
|
local function translate(s)
|
|
|
|
if s then
|
|
|
|
s = _gsub(s, "%w", bcolors)
|
|
|
|
end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
local fn = ct.blit
|
|
|
|
ct.blit = function(text, fg, bg)
|
|
|
|
fn(text, translate(fg), translate(bg))
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2016-12-18 19:38:48 +00:00
|
|
|
function Terminal.getNullTerm(ct)
|
2018-01-24 22:39:38 +00:00
|
|
|
local nt = Terminal.copy(ct)
|
2016-12-18 19:38:48 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local methods = { 'blit', 'clear', 'clearLine', 'scroll',
|
|
|
|
'setCursorBlink', 'setCursorPos', 'write' }
|
|
|
|
for _,v in pairs(methods) do
|
|
|
|
nt[v] = function() end
|
|
|
|
end
|
2016-12-18 19:38:48 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
return nt
|
2016-12-18 19:38:48 +00:00
|
|
|
end
|
|
|
|
|
2017-04-09 00:06:31 +00:00
|
|
|
function Terminal.copy(it, ot)
|
2018-01-24 22:39:38 +00:00
|
|
|
ot = ot or { }
|
|
|
|
for k,v in pairs(it) do
|
|
|
|
if type(v) == 'function' then
|
|
|
|
ot[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ot
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Terminal.mirror(ct, dt)
|
2019-02-20 06:24:37 +00:00
|
|
|
local t = { }
|
2018-01-24 22:39:38 +00:00
|
|
|
for k,f in pairs(ct) do
|
2019-02-20 06:24:37 +00:00
|
|
|
t[k] = function(...)
|
2018-01-24 22:39:38 +00:00
|
|
|
local ret = { f(...) }
|
|
|
|
if dt[k] then
|
|
|
|
dt[k](...)
|
|
|
|
end
|
|
|
|
return table.unpack(ret)
|
|
|
|
end
|
|
|
|
end
|
2019-02-20 06:24:37 +00:00
|
|
|
return t
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-05-06 01:43:17 +00:00
|
|
|
function Terminal.readPassword(prompt)
|
2018-01-24 22:39:38 +00:00
|
|
|
if prompt then
|
|
|
|
term.write(prompt)
|
|
|
|
end
|
|
|
|
local fn = term.current().write
|
|
|
|
term.current().write = function() end
|
|
|
|
local s
|
|
|
|
pcall(function() s = _G.read(prompt) end)
|
|
|
|
term.current().write = fn
|
|
|
|
|
|
|
|
if s == '' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return s
|
2017-05-06 01:43:17 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
return Terminal
|