view.love/main.lua

97 lines
2.2 KiB
Lua
Raw Normal View History

require 'keychord'
2022-05-02 15:28:33 +00:00
local utf8 = require 'utf8'
2022-05-02 04:55:57 +00:00
lines = {}
width, height, flags = 0, 0, nil
exec_payload = nil
2022-05-02 04:55:57 +00:00
function love.load()
table.insert(lines, '')
love.window.setMode(0, 0) -- maximize
width, height, flags = love.window.getMode()
2022-05-02 15:32:31 +00:00
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
2022-05-02 04:55:57 +00:00
end
function love.draw()
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle('fill', 1, 1, width-1, height-1)
2022-05-02 06:34:36 +00:00
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle('fill', 1, 1, 400, 10*12)
2022-05-02 04:55:57 +00:00
love.graphics.setColor(0, 0, 0)
2022-05-02 06:34:25 +00:00
local text
2022-05-02 04:55:57 +00:00
for i, line in ipairs(lines) do
2022-05-02 06:34:25 +00:00
text = love.graphics.newText(love.graphics.getFont(), line)
love.graphics.draw(text, 12, i*15)
2022-05-02 04:55:57 +00:00
end
2022-05-02 06:34:25 +00:00
-- cursor
love.graphics.print('_', 12+text:getWidth(), #lines*15)
if exec_payload then
call_gather(exec_payload)
end
2022-05-02 04:55:57 +00:00
end
function love.update(dt)
end
function love.textinput(t)
lines[#lines] = lines[#lines]..t
end
function keychord_pressed(chord)
-- Don't handle any keys here that would trigger love.textinput above.
if chord == 'return' then
2022-05-02 04:55:57 +00:00
table.insert(lines, '')
2022-05-02 15:28:33 +00:00
elseif chord == 'backspace' then
if #lines > 1 and lines[#lines] == '' then
table.remove(lines)
else
local byteoffset = utf8.offset(lines[#lines], -1)
if byteoffset then
lines[#lines] = string.sub(lines[#lines], 1, byteoffset-1)
end
end
elseif chord == 'C-r' then
eval(lines[#lines])
--? lines[#lines+1] = eval(lines[#lines])[1]
--? lines[#lines+1] = ''
2022-05-02 04:55:57 +00:00
end
end
function love.keyreleased(key, scancode)
end
function love.mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
end
2022-05-02 14:21:39 +00:00
function eval(buf)
local f = load('return '..buf, 'REPL')
if f then
exec_payload = f
return
--? return call_gather(f)
2022-05-02 14:21:39 +00:00
end
local f, err = load(buf, 'REPL')
if f then
exec_payload = f
return
--? return call_gather(f)
2022-05-02 14:21:39 +00:00
else
return {err}
end
end
-- based on https://github.com/hoelzro/lua-repl
function call_gather(f)
local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end))
return results
end
function gather_results(success, ...)
local n = select('#', ...)
return success, { n = n, ... }
end