I'm a bit tired of carrying these around my machines so it would be nice to see them in gears.debug. It is cold and empty there anyway.

The function names should definitely be changed. I use "d" because it is easy to write, but perhaps something more comprehensible might do better.
>From fec0997188f708a0a16aafeb590a9865a6b0dc61 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 |   65 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/lib/gears/debug.lua.in b/lib/gears/debug.lua.in
index 634faa0..6ee92d2 100644
--- a/lib/gears/debug.lua.in
+++ b/lib/gears/debug.lua.in
@@ -7,9 +7,16 @@
 local error = error
 local tostring = tostring
 local traceback = debug.traceback
+local print = print
+local type = type
+local pairs = pairs
+local require = require
 
 module("gears.debug")
 
+-- Variable to keep naughty module once it is required.
+local naughty = nil
+
 --- Check that the given condition holds true, else throw an error
 -- @param cond If this is false, throw a lua error with a backtrace.
 -- @param message Message to print in the error (optional).
@@ -20,4 +27,62 @@ 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 d_raw(data, shift, tag)
+    local result = ""
+
+    if tag then
+        result = result .. tostring(tag) .. " : "
+    end
+    if not data then
+        result = result .. "nil"
+    else
+        result = result .. tostring(data)
+    end
+    if type(data) ~= "table" then
+        return result .. " (" .. type(data) .. ")"
+    end
+
+    shift = (shift ~= nil) and shift .. "  " or "  "
+    for k, v in pairs(data) do
+        result = result .. "\n" .. shift .. d_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 d_return(data, tag)
+    return d_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 d(data, tag)
+    print(d_return(data, tag))
+end
+
+--- Show the table (or any other value) using Naughty.
+-- @param data Table to show.
+-- @param tag The name of the table.
+function d_show(data, tag)
+    if not naughty then
+        naughty = require("naughty")
+    end
+    naughty.notify( { title = "ETDP",
+                      text = log.d_return(s),
+                      timeout = 0})
+end
+
 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
-- 
1.7.9.4

Reply via email to