1
Fork 0

pretty menu on turtles

This commit is contained in:
Andy Killorin 2023-12-19 20:36:30 -06:00
parent 598fdf4df6
commit 1694c461ff
Signed by: ank
GPG key ID: B6241CA3B552BCA4
2 changed files with 48 additions and 6 deletions

View file

@ -43,6 +43,49 @@ local function inventoryinfo()
return { ["Inventory"] = peripheral.wrap("front").list() } return { ["Inventory"] = peripheral.wrap("front").list() }
end end
local function drawui(command, args, backoff)
term.setTextColor(colors.white)
local name = os.getComputerLabel()
local width, height = term.getSize()
local offset = math.floor((width - string.len(name)) / 2)
term.setCursorPos(offset, 2)
term.write(name)
term.setCursorPos(1,4)
term.clearLine()
term.write("C&C: ")
if backoff == 0 then
term.setTextColor(colors.green)
term.write("connected")
else
term.setTextColor(colors.red)
term.write("disconnected ")
term.setTextColor(colors.gray)
term.write(backoff+1 .. " failed attempts")
end
term.setCursorPos(1,6)
term.setTextColor(colors.white)
term.clearLine()
if command then
term.write("Command: ")
term.write(command .. " ")
term.setTextColor(colors.blue)
term.write(args)
end
term.setTextColor(colors.white)
term.setCursorPos(1,8)
term.clearLine()
term.write("fuel: ")
term.setTextColor(colors.blue)
term.write(string.format("%06d" ,turtle.getFuelLevel()))
term.setCursorPos(7,9)
term.write(string.format("%06d" ,turtle.getFuelLimit()))
term.setCursorPos(1,10)
end
local commands = { local commands = {
["Wait"] = sleep, ["Wait"] = sleep,
["Forward"] = cyclefn(turtle.forward), ["Forward"] = cyclefn(turtle.forward),
@ -137,13 +180,16 @@ else
idfile.close() idfile.close()
end end
term.clear()
repeat repeat
print(command)
local args = nil local args = nil
if type(command) == "table" then if type(command) == "table" then
command, args = pairs(command)(command) command, args = pairs(command)(command)
end end
drawui(command, args, backoff)
local ret = nil local ret = nil
if command then if command then
@ -204,11 +250,7 @@ repeat
backoff = 0 backoff = 0
command = textutils.unserialiseJSON(rsp.readAll()) command = textutils.unserialiseJSON(rsp.readAll())
else else
print("C&C server offline, waiting " .. backoff .. " seconds")
sleep(backoff) sleep(backoff)
backoff = backoff + 1 backoff = backoff + 1
end end
until command == "Poweroff" until command == "Poweroff"
::done:: -- I hate that this exists. What is this, NASM?
print("exited")

View file

@ -238,7 +238,7 @@ async fn command(
Json( Json(
turtle::process_turtle_update(id, &mut state, req).await turtle::process_turtle_update(id, &mut state, req).await
.unwrap_or(turtle::TurtleCommand::Update), .unwrap_or(turtle::TurtleCommand::Update)
) )
} }