From a37cc462133a30b3bee4bef37674047bd05e5fc1 Mon Sep 17 00:00:00 2001 From: slembcke Date: Thu, 2 Mar 2023 09:51:45 -0600 Subject: [PATCH] Make dbg.pretty() overloadable. --- README.md | 1 + debugger.lua | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a2a1a01..1d7f6ca 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ There are several overloadable functions you can use to customize debugger.lua. * `dbg.write(str)` - Write a string to the output. (Defaults to write to stdout) * `dbg.shorten_path(path)` - Return a shortened version of a path. (Defaults to simply return `path`) * `dbg.exit(err)` - Stop debugging. (Defaults to `os.exit(err)`) +* `dbg.pretty(obj)' - Output a pretty print string for an object. (Defaults to a reasonable version using __tostring metamethods and such) Using these you can customize the debugger to work in your environment. For instance, you can divert the I/O over a network socket or to a GUI window. diff --git a/debugger.lua b/debugger.lua index 073cd42..b6f328b 100644 --- a/debugger.lua +++ b/debugger.lua @@ -290,7 +290,7 @@ local function cmd_print(expr) else local output = "" for i = 2, results.n do - output = output..(i ~= 2 and ", " or "")..pretty(results[i]) + output = output..(i ~= 2 and ", " or "")..dbg.pretty(results[i]) end if output == "" then output = "" end @@ -395,7 +395,7 @@ local function cmd_locals() -- Skip the debugger object itself, "(*internal)" values, and Lua 5.2's _ENV object. if not rawequal(v, dbg) and k ~= "_ENV" and not k:match("%(.*%)") then - dbg_writeln(" "..COLOR_BLUE..k.. GREEN_CARET..pretty(v)) + dbg_writeln(" "..COLOR_BLUE..k.. GREEN_CARET..dbg.pretty(v)) end end @@ -517,7 +517,7 @@ dbg.writeln = dbg_writeln dbg.pretty_depth = 3 dbg.pretty = pretty -dbg.pp = function(value, depth) dbg_writeln(pretty(value, depth)) end +dbg.pp = function(value, depth) dbg_writeln(dbg.pretty(value, depth)) end dbg.auto_where = false dbg.auto_eval = false @@ -527,7 +527,7 @@ local lua_error, lua_assert = error, assert -- Works like error(), but invokes the debugger. function dbg.error(err, level) level = level or 1 - dbg_writeln(COLOR_RED.."ERROR: "..COLOR_RESET..pretty(err)) + dbg_writeln(COLOR_RED.."ERROR: "..COLOR_RESET..dbg.pretty(err)) dbg(false, level, "dbg.error()") lua_error(err, level) @@ -546,7 +546,7 @@ end -- Works like pcall(), but invokes the debugger on an error. function dbg.call(f, ...) return xpcall(f, function(err) - dbg_writeln(COLOR_RED.."ERROR: "..COLOR_RESET..pretty(err)) + dbg_writeln(COLOR_RED.."ERROR: "..COLOR_RESET..dbg.pretty(err)) dbg(false, 1, "dbg.call()") return err @@ -556,7 +556,7 @@ end -- Error message handler that can be used with lua_pcall(). function dbg.msgh(...) if debug.getinfo(2) then - dbg_writeln(COLOR_RED.."ERROR: "..COLOR_RESET..pretty(...)) + dbg_writeln(COLOR_RED.."ERROR: "..COLOR_RESET..dbg.pretty(...)) dbg(false, 1, "dbg.msgh()") else dbg_writeln(COLOR_RED.."debugger.lua: "..COLOR_RESET.."Error did not occur in Lua code. Execution will continue after dbg_pcall().")