diff --git a/.lite_project.lua b/.lite_project.lua index 3a629b2..9723012 100644 --- a/.lite_project.lua +++ b/.lite_project.lua @@ -9,7 +9,12 @@ config.plugins.jpdebug = { ["test"] = { type = "shell", cmd = {"lua", "test.lua"} - } + }, + ["luadebug"] = { + type = "luadebug", + entry = "test.lua", + cwd = ".", + }, }, - default_target = "test" + default_target = "luadebug" } diff --git a/init.lua b/init.lua index ee39ed9..6cd1590 100644 --- a/init.lua +++ b/init.lua @@ -13,7 +13,9 @@ core.jpdebug = core.jpdebug or {} -- Global list of all the runners core.jpdebug.runners = core.jpdebug.runners or {} 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_luadebug.name] = runner_luadebug -- Currently used view local active_view = nil @@ -67,6 +69,18 @@ local function get_plugin_directory() return nil 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 ---------- ---@class JPDebugView : core.view local JPDebugView = View:extend() @@ -92,10 +106,13 @@ end function JPDebugView:push(kind, s) --TODO do some things with kind here if not s or s == "" then return end - self.lines[#self.lines + 1] = s - if #self.lines > self.max_lines then - local drop = #self.lines - self.max_lines - for _ = 1, drop do table.remove(self.lines, 1) end + local lines = stringsplit(s, "\n") + for _,l in pairs(lines) do + self.lines[#self.lines + 1] = l + 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 -- autoscroll to bottom 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 if runner_name == target.type then -- Found a runner - running_proc = runner:run(target.cmd, { - cwd = target.cwd or ".", - env = target.env or {}, - stdout = process.REDIRECT_PIPE, - stderr = process.REDIRECT_PIPE - }, name) + running_proc = runner:run(target, name) running_runner = runner if running_proc == nil then core.error("[jpdebug] Could not run the target") + view:push("stderr", "Could not run the target") return end @@ -192,8 +205,10 @@ local function run_target(target, name) local err = runner:read_stderr(running_proc) if err ~= nil and err ~= "" then view:push("stderr", err) end end - local code = runner:wait(process.WAIT_INFINITE) - view:push("stdout", ("\n[exit] code=%s\n"):format(tostring(code))) + if running_proc then + 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_runner = nil end) @@ -203,6 +218,7 @@ local function run_target(target, name) end -- No suitable runners found core.error("[jpdebug] No suitable runners found for target %s", name) + view:push("stderr", "No suitable runners found for target "..name) end -- ---------- Add toolbar to treeview if plugins are installed ------ diff --git a/runners/luadebug.lua b/runners/luadebug.lua new file mode 100644 index 0000000..c5bd084 --- /dev/null +++ b/runners/luadebug.lua @@ -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 diff --git a/runners/shell.lua b/runners/shell.lua index 60ab351..0cfb479 100644 --- a/runners/shell.lua +++ b/runners/shell.lua @@ -7,14 +7,19 @@ local M = { } -- Run a shell command ----@param cmd table|string List of command pieces or in full ----@param opts table Options to run the command. Should contain env (table), cwd (string), stdout and stdin +---@param target table Target table ---@param name string Name of the target to run ---@return process|nil -function M:run(cmd, opts, name) +function M:run(target, name) core.log("[jpdebug] Running shell command") - if cmd then - local proc = process.start(cmd, opts) + local 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 else core.error("[jpdebug] command not specified for target %s", name) diff --git a/test.lua b/test.lua index 677bb2a..0ab3dc9 100644 --- a/test.lua +++ b/test.lua @@ -1,8 +1,6 @@ -local socket = require 'socket' - print("Starting loop: test 123") -for i = 1,100 do - socket.sleep(0.5) - print("i =", i) -- we'll stop here +for i = 1,10 do + print("i = ", i) -- we'll stop here + require("socket").sleep(1) end print("Yaaayyyy it works perfectly fine :)")