feat: add commands
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
]]
|
||||
|
||||
--[[
|
||||
network variables
|
||||
general variables
|
||||
]]
|
||||
local HOST_CHANNEL = 1337
|
||||
local TURTLE_CHANNEL_BASE = 1338
|
||||
@@ -29,6 +29,8 @@ modem.open(HOST_CHANNEL)
|
||||
local monitor = peripheral.find("monitor")
|
||||
local scr = monitor or term
|
||||
|
||||
local shouldRun = true
|
||||
|
||||
--[[
|
||||
helper functions
|
||||
]]
|
||||
@@ -263,7 +265,36 @@ registerEventListener("key", scrollLogs)
|
||||
--[[
|
||||
console
|
||||
]]
|
||||
local consoleCommands = {}
|
||||
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)
|
||||
commandBuffer = commandBuffer .. char
|
||||
end
|
||||
@@ -271,10 +302,8 @@ end
|
||||
registerEventListener("char", handleConsoleInput)
|
||||
|
||||
function handleConsoleKeys(key)
|
||||
if key == GLFW_KEY_ENTER then
|
||||
-- executeCommand(commandBuffer)
|
||||
log.info("Executed command: %s", commandBuffer)
|
||||
commandBuffer = ""
|
||||
if key == GLFW_KEY_ENTER and commandBuffer:len() > 0 then
|
||||
processCommand()
|
||||
elseif key == GLFW_KEY_BACKSPACE then
|
||||
local len = commandBuffer:len()
|
||||
if len >= 1 then
|
||||
@@ -285,10 +314,22 @@ end
|
||||
|
||||
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
|
||||
]]
|
||||
-- to-do: dont apply offset based on log entries but on lines
|
||||
function drawLogs(x0, y0, x1, y1)
|
||||
local w = x1 - x0
|
||||
local h = y1 - y0
|
||||
@@ -432,7 +473,7 @@ log.general("tutel host init")
|
||||
log.info("broadcasting SCAN")
|
||||
sendToAll("SCAN", { ["hostChannel"] = HOST_CHANNEL })
|
||||
|
||||
while true do
|
||||
while shouldRun do
|
||||
updateScreen()
|
||||
pollEvents()
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user