feat: add commands

This commit is contained in:
2025-05-08 13:33:24 -03:00
parent 283ee5cb71
commit 6fbb54c2cd
+48 -7
View File
@@ -16,7 +16,7 @@
]] ]]
--[[ --[[
network variables general variables
]] ]]
local HOST_CHANNEL = 1337 local HOST_CHANNEL = 1337
local TURTLE_CHANNEL_BASE = 1338 local TURTLE_CHANNEL_BASE = 1338
@@ -29,6 +29,8 @@ modem.open(HOST_CHANNEL)
local monitor = peripheral.find("monitor") local monitor = peripheral.find("monitor")
local scr = monitor or term local scr = monitor or term
local shouldRun = true
--[[ --[[
helper functions helper functions
]] ]]
@@ -263,7 +265,36 @@ registerEventListener("key", scrollLogs)
--[[ --[[
console console
]] ]]
local consoleCommands = {}
local commandBuffer = "" local commandBuffer = ""
function registerConsoleCommand(name, handler)
consoleCommands[name] = handler
end
function processCommand()
local cmd
local args = {}
for word in commandBuffer:gmatch('[^%s"]+|"[^"]*"') do
word = word:gsub('^"(.*)"$', '%1')
if not cmd then
cmd = word
else
table.insert(args, word)
end
end
if consoleCommands[cmd] then
log.info("> %s", cmd)
consoleCommands[cmd](args)
else
log.warning("Unknown command: %s", cmd)
end
commandBuffer = ""
end
function handleConsoleInput(char) function handleConsoleInput(char)
commandBuffer = commandBuffer .. char commandBuffer = commandBuffer .. char
end end
@@ -271,10 +302,8 @@ end
registerEventListener("char", handleConsoleInput) registerEventListener("char", handleConsoleInput)
function handleConsoleKeys(key) function handleConsoleKeys(key)
if key == GLFW_KEY_ENTER then if key == GLFW_KEY_ENTER and commandBuffer:len() > 0 then
-- executeCommand(commandBuffer) processCommand()
log.info("Executed command: %s", commandBuffer)
commandBuffer = ""
elseif key == GLFW_KEY_BACKSPACE then elseif key == GLFW_KEY_BACKSPACE then
local len = commandBuffer:len() local len = commandBuffer:len()
if len >= 1 then if len >= 1 then
@@ -285,10 +314,22 @@ end
registerEventListener("key", handleConsoleKeys) registerEventListener("key", handleConsoleKeys)
--[[
console commands
]]
function echoCommand(args)
log.info(table.concat(args, " "))
end
registerConsoleCommand("echo", echoCommand)
function exitCommand(args)
shouldRun = false
end
registerConsoleCommand("exit", exitCommand)
--[[ --[[
drawing functions drawing functions
]] ]]
-- to-do: dont apply offset based on log entries but on lines
function drawLogs(x0, y0, x1, y1) function drawLogs(x0, y0, x1, y1)
local w = x1 - x0 local w = x1 - x0
local h = y1 - y0 local h = y1 - y0
@@ -432,7 +473,7 @@ log.general("tutel host init")
log.info("broadcasting SCAN") log.info("broadcasting SCAN")
sendToAll("SCAN", { ["hostChannel"] = HOST_CHANNEL }) sendToAll("SCAN", { ["hostChannel"] = HOST_CHANNEL })
while true do while shouldRun do
updateScreen() updateScreen()
pollEvents() pollEvents()
end end