1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 12:44:50 +00:00
opus/sys/apis/terminal.lua

146 lines
3.0 KiB
Lua
Raw Normal View History

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
2018-01-14 04:40:53 +00:00
function Terminal.scrollable(win, parent)
2018-01-13 20:17:26 +00:00
local w, h = win.getSize()
2018-01-14 04:40:53 +00:00
local _, ph = parent.getSize()
2018-01-13 20:17:26 +00:00
local scrollPos = 0
local scp = win.setCursorPos
win.setCursorPos = function(x, y)
2018-01-14 04:40:53 +00:00
_G._p = y
if y > scrollPos + ph then
win.scrollTo(y - ph)
2018-01-13 20:17:26 +00:00
end
2018-01-14 04:40:53 +00:00
scp(x, y)
2018-01-13 20:17:26 +00:00
end
win.scrollUp = function()
win.scrollTo(scrollPos - 1)
end
win.scrollDown = function()
win.scrollTo(scrollPos + 1)
end
win.scrollTo = function(p)
2018-01-14 04:40:53 +00:00
p = math.min(math.max(p, 0), h - ph)
2018-01-13 20:17:26 +00:00
if p ~= scrollPos then
scrollPos = p
2018-01-14 04:40:53 +00:00
win.reposition(1, -scrollPos + 1, w, h)
2018-01-13 20:17:26 +00:00
end
end
end
2016-12-11 19:24:52 +00:00
function Terminal.toGrayscale(ct)
local scolors = {
[ 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,
}
local methods = { 'setBackgroundColor', 'setBackgroundColour',
'setTextColor', 'setTextColour' }
for _,v in pairs(methods) do
2017-10-08 21:45:01 +00:00
local fn = ct[v]
2016-12-11 19:24:52 +00:00
ct[v] = function(c)
2017-04-09 00:06:31 +00:00
fn(scolors[c])
2016-12-11 19:24:52 +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
2017-10-08 21:45:01 +00:00
s = _gsub(s, "%w", bcolors)
2016-12-11 19:24:52 +00:00
end
return s
end
local fn = ct.blit
ct.blit = function(text, fg, bg)
fn(text, translate(fg), translate(bg))
end
end
2016-12-18 19:38:48 +00:00
function Terminal.getNullTerm(ct)
local nt = Terminal.copy(ct)
local methods = { 'blit', 'clear', 'clearLine', 'scroll',
'setCursorBlink', 'setCursorPos', 'write' }
for _,v in pairs(methods) do
nt[v] = function() end
end
return nt
end
2017-04-09 00:06:31 +00:00
function Terminal.copy(it, ot)
ot = ot or { }
for k,v in pairs(it) do
2017-10-08 21:45:01 +00:00
if type(v) == 'function' then
ot[k] = v
end
2016-12-11 19:24:52 +00:00
end
2017-04-09 00:06:31 +00:00
return ot
2016-12-11 19:24:52 +00:00
end
function Terminal.mirror(ct, dt)
for k,f in pairs(ct) do
ct[k] = function(...)
local ret = { f(...) }
if dt[k] then
2018-01-07 03:25:33 +00:00
dt[k](...)
end
2018-01-14 04:40:53 +00:00
return table.unpack(ret)
2016-12-11 19:24:52 +00:00
end
end
end
2017-05-06 01:43:17 +00:00
function Terminal.readPassword(prompt)
if prompt then
term.write(prompt)
end
2017-05-10 10:11:25 +00:00
local fn = term.current().write
term.current().write = function() end
local s
2017-10-08 21:45:01 +00:00
pcall(function() s = _G.read(prompt) end)
2017-05-10 10:11:25 +00:00
term.current().write = fn
2017-05-06 01:43:17 +00:00
if s == '' then
return
end
return s
end
2016-12-11 19:24:52 +00:00
return Terminal