Implement /install
This commit is contained in:
1
lua/.gitignore
vendored
Normal file
1
lua/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
out
|
||||
19
lua/justfile
Normal file
19
lua/justfile
Normal file
@@ -0,0 +1,19 @@
|
||||
default:
|
||||
@just --list
|
||||
|
||||
teal_files := "main.tl framebuffer.tl ringbuffer.tl"
|
||||
|
||||
build:
|
||||
mkdir -p out
|
||||
cp json.lua out
|
||||
for file in {{teal_files}}; do \
|
||||
tl gen $file; \
|
||||
mv ${file%.tl}.lua out; \
|
||||
done
|
||||
|
||||
alias b := build
|
||||
|
||||
clean:
|
||||
rm -r out
|
||||
|
||||
alias c := clean
|
||||
61
lua/main.tl
61
lua/main.tl
@@ -8,17 +8,40 @@ print("[MAIN] Init")
|
||||
|
||||
local enum SocketState
|
||||
"reset"
|
||||
"connecting" -- currently unused
|
||||
"error"
|
||||
"ok"
|
||||
"viewer_connected"
|
||||
end
|
||||
|
||||
local BAD_STATES <const> : {SocketState: boolean} = {
|
||||
["reset"] = true,
|
||||
["error"] = true,
|
||||
}
|
||||
|
||||
local type SocketStateCallback = function(new_state: SocketState)
|
||||
|
||||
local record Socket
|
||||
state: SocketState
|
||||
bad_state: function(self: Socket): boolean
|
||||
set_state: function(self: Socket, state: SocketState)
|
||||
on_state_change: function(self: Socket, cb: SocketStateCallback)
|
||||
_callback: SocketStateCallback
|
||||
ws: http.Websocket
|
||||
end
|
||||
|
||||
local socket: Socket = {
|
||||
state = "reset",
|
||||
bad_state = function(self: Socket): boolean
|
||||
return BAD_STATES[self.state] ~= nil
|
||||
end,
|
||||
set_state = function(self: Socket, state: SocketState)
|
||||
self.state = state
|
||||
self._callback(state)
|
||||
end,
|
||||
on_state_change = function(self: Socket, cb: SocketStateCallback)
|
||||
self._callback = cb
|
||||
end,
|
||||
_callback = function(_: SocketState) end,
|
||||
ws = nil,
|
||||
}
|
||||
|
||||
@@ -26,13 +49,13 @@ local function send(message: string)
|
||||
-- "message" needs to be valid JSON
|
||||
-- otherwise the server will not accept it
|
||||
|
||||
if socket.state ~= "ok" then return end
|
||||
if socket:bad_state() then return end
|
||||
|
||||
local r = { pcall(socket.ws.send, message) }
|
||||
|
||||
if r[1] == false then
|
||||
if (r[2] as string):sub(-11) == "closed file" then
|
||||
socket.state = "reset"
|
||||
socket:set_state("reset")
|
||||
elseif (r[2] as string):sub(-9) == "too large" then
|
||||
-- TODO handle
|
||||
-- the connection stays open though
|
||||
@@ -66,22 +89,32 @@ end
|
||||
|
||||
-- Create tasks
|
||||
|
||||
local bar_codes: { SocketState: {string} } = {
|
||||
["reset"] = {"[WS] RST", "78870111"},
|
||||
["error"] = {"[WS] ERR", "78870EEE"},
|
||||
["ok"] = {"[WS] OK\x03", "78870DD5"},
|
||||
["viewer_connected"] = {"[WS] CON", "78870999"},
|
||||
}
|
||||
|
||||
socket:on_state_change(function(new_state: SocketState)
|
||||
set_bar(table.unpack(bar_codes[new_state]))
|
||||
end)
|
||||
|
||||
local ws_task = coroutine.create(function()
|
||||
while true do
|
||||
if socket.state == "reset" then
|
||||
set_bar("[WS] RST", "78870111")
|
||||
if socket:bad_state() then
|
||||
--set_bar("[WS] RST", "78870111")
|
||||
local r = http.websocket(ENDPOINT)
|
||||
if r ~= false then
|
||||
socket.ws = r as http.Websocket
|
||||
set_bar("[WS] OK\x03", "78870DD5")
|
||||
socket.state = "ok"
|
||||
--set_bar("[WS] OK\x03", "78870DD5")
|
||||
socket:set_state("ok")
|
||||
else
|
||||
set_bar("[WS] ERR", "78870EEE")
|
||||
socket:set_state("error")
|
||||
--set_bar("[WS] ERR", "78870EEE")
|
||||
end
|
||||
end
|
||||
repeat
|
||||
sleep(1)
|
||||
until socket.state ~= "ok"
|
||||
sleep(1)
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -89,7 +122,8 @@ local report_task = coroutine.create(function()
|
||||
local last_report = -1.0
|
||||
while true do
|
||||
local now = os.clock()
|
||||
if now - last_report >= 0.05 then
|
||||
local interval = (socket.state == "viewer_connected") and 0.05 or 1
|
||||
if now - last_report >= interval then
|
||||
local message = json.encode({
|
||||
screen = buffer.serialize()
|
||||
})
|
||||
@@ -151,6 +185,9 @@ while shell_running do
|
||||
end
|
||||
end
|
||||
|
||||
if socket.state == "ok" then
|
||||
socket.ws.close()
|
||||
end
|
||||
|
||||
term.native = orig_native
|
||||
term.redirect(term.native())
|
||||
|
||||
Reference in New Issue
Block a user