Added separate debugger file and fixed shell to new form

This commit is contained in:
2025-10-25 20:07:49 +02:00
parent f6c031849f
commit abedca2394
4 changed files with 148 additions and 110 deletions

View File

@ -1,19 +1,32 @@
local core = require "core"
local process = require "process"
---@class M
local M = {
name = "shell"
---@class shell: runner
local shell = {
name = "shell",
proc = nil ---@type process|nil
}
-- Run a shell command
---@param target table Target table
---@param name string Name of the target to run
---@praam debuginfo table Debugging information
---@return process|nil
---@diagnostic disable-next-line: unused-local
function M:run(target, name, debuginfo)
core.log("[jpdebug] Running shell command")
function shell:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
---@meta
function shell.log(msg) end
---@meta
function shell.error(msg) end
---@meta
function shell.stdout(msg) end
---@meta
function shell.stderr(msg) end
---@meta
function shell.exited() end
function shell:run(target, name)
local opts = {
cwd = target.cwd or ".",
env = target.env or {},
@ -21,43 +34,46 @@ function M:run(target, name, debuginfo)
stderr = process.REDIRECT_PIPE
}
if target.cmd then
local proc = process.start(target.cmd, opts)
return proc
self.proc = process.start(target.cmd, opts)
if not self.proc then
self.error("Could not start process...")
return
end
else
core.error("[jpdebug] command not specified for target %s", name)
self.error(string.format("command not specified for target %s", name))
end
-- output pump
core.add_thread(function()
while true do
core.redraw = true
coroutine.yield(0.016) -- 60FPS
local sout = self.proc:read_stdout()
local serr = self.proc:read_stderr()
if sout == nil and serr == nil then
self.exited()
break
end
if sout and sout~="" then self.stdout(sout) end
if serr and serr~="" then self.stderr(serr) end
end
end)
end
-- Wait untill it ends, possibly with timeout
---@param proc process Process object
---@param time any Time field, process.WAIT_INFINITE
function M:wait(proc, time)
return proc:wait(time)
function shell:wait(time)
if not self.proc then return end
return self.proc:wait(time)
end
-- Read the stdout, returns nil if process has stopped
---@param proc process Process object
function M:read_stdout(proc)
return proc:read_stdout()
function shell:kill()
if not self.proc then return end
self.proc:kill()
end
-- Read the stderr
---@param proc process Process object
function M:read_stderr(proc)
return proc:read_stderr()
function shell:terminate()
if not self.proc then return end
self.proc:terminate()
end
-- Kill the process
---@param proc process Process object
function M:kill(proc)
proc:kill()
end
-- Terminate the process
---@param proc process Process object
function M:terminate(proc)
proc:terminate()
end
return M
return shell