Start with luadebug

This commit is contained in:
2025-10-23 23:13:48 +02:00
parent 8cde56a4e8
commit 1cd7b378b6
5 changed files with 94 additions and 24 deletions

View File

@ -9,7 +9,12 @@ config.plugins.jpdebug = {
["test"] = { ["test"] = {
type = "shell", type = "shell",
cmd = {"lua", "test.lua"} cmd = {"lua", "test.lua"}
} },
["luadebug"] = {
type = "luadebug",
entry = "test.lua",
cwd = ".",
},
}, },
default_target = "test" default_target = "luadebug"
} }

View File

@ -13,7 +13,9 @@ core.jpdebug = core.jpdebug or {}
-- Global list of all the runners -- Global list of all the runners
core.jpdebug.runners = core.jpdebug.runners or {} core.jpdebug.runners = core.jpdebug.runners or {}
local runner_shell = require("plugins.jpdebug.runners.shell") local runner_shell = require("plugins.jpdebug.runners.shell")
local runner_luadebug = require("plugins.jpdebug.runners.luadebug")
core.jpdebug.runners[runner_shell.name] = runner_shell core.jpdebug.runners[runner_shell.name] = runner_shell
core.jpdebug.runners[runner_luadebug.name] = runner_luadebug
-- Currently used view -- Currently used view
local active_view = nil local active_view = nil
@ -67,6 +69,18 @@ local function get_plugin_directory()
return nil return nil
end end
-- Simple function splitting strings
local function stringsplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
-- ---------- JPDebugView: a simple scrollable log view ---------- -- ---------- JPDebugView: a simple scrollable log view ----------
---@class JPDebugView : core.view ---@class JPDebugView : core.view
local JPDebugView = View:extend() local JPDebugView = View:extend()
@ -92,10 +106,13 @@ end
function JPDebugView:push(kind, s) function JPDebugView:push(kind, s)
--TODO do some things with kind here --TODO do some things with kind here
if not s or s == "" then return end if not s or s == "" then return end
self.lines[#self.lines + 1] = s local lines = stringsplit(s, "\n")
if #self.lines > self.max_lines then for _,l in pairs(lines) do
local drop = #self.lines - self.max_lines self.lines[#self.lines + 1] = l
for _ = 1, drop do table.remove(self.lines, 1) end if #self.lines > self.max_lines then
local drop = #self.lines - self.max_lines
for _ = 1, drop do table.remove(self.lines, 1) end
end
end end
-- autoscroll to bottom -- autoscroll to bottom
self.scroll.to.y = self:get_scrollable_size() self.scroll.to.y = self:get_scrollable_size()
@ -160,16 +177,12 @@ local function run_target(target, name)
for runner_name,runner in pairs(core.jpdebug.runners) do for runner_name,runner in pairs(core.jpdebug.runners) do
if runner_name == target.type then if runner_name == target.type then
-- Found a runner -- Found a runner
running_proc = runner:run(target.cmd, { running_proc = runner:run(target, name)
cwd = target.cwd or ".",
env = target.env or {},
stdout = process.REDIRECT_PIPE,
stderr = process.REDIRECT_PIPE
}, name)
running_runner = runner running_runner = runner
if running_proc == nil then if running_proc == nil then
core.error("[jpdebug] Could not run the target") core.error("[jpdebug] Could not run the target")
view:push("stderr", "Could not run the target")
return return
end end
@ -192,8 +205,10 @@ local function run_target(target, name)
local err = runner:read_stderr(running_proc) local err = runner:read_stderr(running_proc)
if err ~= nil and err ~= "" then view:push("stderr", err) end if err ~= nil and err ~= "" then view:push("stderr", err) end
end end
local code = runner:wait(process.WAIT_INFINITE) if running_proc then
view:push("stdout", ("\n[exit] code=%s\n"):format(tostring(code))) local code = runner:wait(running_proc, process.WAIT_INFINITE)
view:push("stdout", ("\n[exit] code=%s\n"):format(tostring(code)))
end
running_proc = nil running_proc = nil
running_runner = nil running_runner = nil
end) end)
@ -203,6 +218,7 @@ local function run_target(target, name)
end end
-- No suitable runners found -- No suitable runners found
core.error("[jpdebug] No suitable runners found for target %s", name) core.error("[jpdebug] No suitable runners found for target %s", name)
view:push("stderr", "No suitable runners found for target "..name)
end end
-- ---------- Add toolbar to treeview if plugins are installed ------ -- ---------- Add toolbar to treeview if plugins are installed ------

46
runners/luadebug.lua Normal file
View File

@ -0,0 +1,46 @@
local core = require "core"
local process = require "process"
---@class LDB
local LDB = {
name = "luadebug"
}
---@param target table Target table
---@param name string Name of the target to run
---@return process|nil
function LDB:run(target, name)
end
-- Wait untill it ends, possibly with timeout
---@param proc process Process object
---@param time any Time field, process.WAIT_INFINITE
function LDB:wait(proc, time)
return proc:wait(time)
end
-- Read the stdout, returns nil if process has stopped
---@param proc process Process object
function LDB:read_stdout(proc)
return proc:read_stdout()
end
-- Read the stderr
---@param proc process Process object
function LDB:read_stderr(proc)
return proc:read_stderr()
end
-- Kill the process
---@param proc process Process object
function LDB:kill(proc)
proc:kill()
end
-- Terminate the process
---@param proc process Process object
function LDB:terminate(proc)
proc:terminate()
end
return LDB

View File

@ -7,14 +7,19 @@ local M = {
} }
-- Run a shell command -- Run a shell command
---@param cmd table|string List of command pieces or in full ---@param target table Target table
---@param opts table Options to run the command. Should contain env (table), cwd (string), stdout and stdin
---@param name string Name of the target to run ---@param name string Name of the target to run
---@return process|nil ---@return process|nil
function M:run(cmd, opts, name) function M:run(target, name)
core.log("[jpdebug] Running shell command") core.log("[jpdebug] Running shell command")
if cmd then local opts = {
local proc = process.start(cmd, opts) cwd = target.cwd or ".",
env = target.env or {},
stdout = process.REDIRECT_PIPE,
stderr = process.REDIRECT_PIPE
}
if target.cmd then
local proc = process.start(target.cmd, opts)
return proc return proc
else else
core.error("[jpdebug] command not specified for target %s", name) core.error("[jpdebug] command not specified for target %s", name)

View File

@ -1,8 +1,6 @@
local socket = require 'socket'
print("Starting loop: test 123") print("Starting loop: test 123")
for i = 1,100 do for i = 1,10 do
socket.sleep(0.5) print("i = ", i) -- we'll stop here
print("i =", i) -- we'll stop here require("socket").sleep(1)
end end
print("Yaaayyyy it works perfectly fine :)") print("Yaaayyyy it works perfectly fine :)")