On 03/25/2012 10:11 PM, Uli Schlachter wrote:
Perhaps these should be called "dump"?
Good enough for me. I'll be renaming it locally to something shorted anyway.
Personally, I'd prefer:

   shift = (shift or "") .. "  "

(Because the two spaces are repeated one time less)
Yes, this one is better.
+    for k, v in pairs(data) do
+        result = result .. "\n" .. shift .. d_raw(v, shift, k)
+    end
I wonder if this should also print the table keys. If I print e.g. the arguments
to awful.wibox(), that would be helpful.
I guess it isn't easy to come up with a good format for that. Ideas?
Actually it does print the table keys. It just doesn't seem obvious. You can see that a key is passed as a third argument to the d_raw recursively so it gets printed.
What's "ETDP"? What is "log.d_return"? I guess this is some old stuff and that
this code was previously called "log"? Which would mean that this does not work
anymore...
My bad.
Also, I don't really like the idea of require()'ing "naughty" from here. This
leads to a cyclic dependency between "naughty" and "gears.debug". :-(

Yes, this is helpful, but I don't know a good way around this problem (except
for adding e.g. naughty.debug() which uses gears.debug.d_return (which will then
be called dump_return)?)
This sounds better. Sending in another patch.

Best regards,
Alexander
>From 4f79981f38a702c0ddb23f1d04397830aa373e2b Mon Sep 17 00:00:00 2001
From: Alexander Yakushev <yakushev.a...@gmail.com>
Date: Fri, 16 Mar 2012 22:03:23 +0200
Subject: [PATCH] gears.debug: Add functions for inspecting tables

Signed-off-by: Alexander Yakushev <yakushev.a...@gmail.com>
---
 lib/gears/debug.lua.in |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/lib/gears/debug.lua.in b/lib/gears/debug.lua.in
index 634faa0..910e62d 100644
--- a/lib/gears/debug.lua.in
+++ b/lib/gears/debug.lua.in
@@ -7,6 +7,9 @@
 local error = error
 local tostring = tostring
 local traceback = debug.traceback
+local print = print
+local type = type
+local pairs = pairs
 
 module("gears.debug")
 
@@ -20,4 +23,46 @@ function assert(cond, message)
     end
 end
 
+-- Given a table (or any other data) return a string that contains its
+-- tag, value and type. If data is a table then recursively call d_raw
+-- on each of its values.
+-- @param data Value to inspect.
+-- @param shift Spaces to indent lines with.
+-- @param tag The name of the value.
+-- @return a string which contains tag, value, value type and table key/value
+-- pairs if data is a table.
+local function dump_raw(data, shift, tag)
+    local result = ""
+
+    if tag then
+        result = result .. tostring(tag) .. " : " .. tostring(data)
+    end
+
+    if type(data) ~= "table" then
+        return result .. " (" .. type(data) .. ")"
+    end
+
+    shift = (shift or "") .. "  "
+    for k, v in pairs(data) do
+        result = result .. "\n" .. shift .. dump_raw(v, shift, k)
+    end
+
+    return result
+end
+
+--- Inspect the value in data.
+-- @param data Value to inspect.
+-- @param tag The name of the value.
+-- @return a string that contains the expanded value of data.
+function dump_return(data, tag)
+    return dump_raw(data, nil, tag)
+end
+
+--- Print the table (or any other value) to the console.
+-- @param data Table to print.
+-- @param tag The name of the table.
+function dump(data, tag)
+    print(dump_return(data, tag))
+end
+
 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
-- 
1.7.9.4

Reply via email to