erdos/src/term/type.lua

76 lines
1.3 KiB
Lua

return function(con)
local buf = {''}
function con.keydown(k)
if k == 'backspace' then
if #buf[#buf] > 0 then con.bs() end
elseif k == 'return' then
con.type '\n'
table.insert(buf, '')
end
end
function con.popbuf(line)
local v = buf[#buf-1] or ''
if line then
if v:sub(-1) == '\n' then
buf[#buf-1] = ''
return v:sub(1, -2)
end
else
buf[#buf] = bs(buf[#buf])
v = v:sub(1,1)
end
return nil
end
function con.type(text)
buf[#buf] = buf[#buf].. text
con.print(text)
end
local function bs(v)
local off = utf8.offset(v, -1)
if off then v = v:sub(1, off - 1) end
return v, off ~= nil
end
function con.bs()
buf[#buf] = bs(buf[#buf])
con.out[#con.out] = bs(con.out[#con.out])
end
function con.cls()
buf = {''}
con.text = {''}
con.oy = 0
con.reszT()
end
function con.ret()
table.insert(con.out, '')
end
local function conc(a, b)
return tostring(a)..tostring(b)
end
function con.print(text)
for i, v in utf8.codes(tostring(text)) do
v = utf8.char(v)
if v == '\n' then con.ret()
elseif v == '\b' then con.bs()
-- \r\t\v is in loop.lua
else con.out[#con.out] = conc(con.out[#con.out],v)
end
end
con.reszT()
end
function con.println(text)
con.print(conc(text,'\n'))
end
end