Version 5 of pdict: Pretty print a dict

Updated 2010-06-01 11:51:50 by AxN

I couldn't find a quick way to print a dict item when I was debugging some code so I wrote this pretty printer. Enjoy...tjk


# -- pdict
#
# Pretty print a dict similar to parray.
#
# USAGE:
#
#   pdict d [i [p [s]]]
#
# WHERE:
#  d - dict to be printed
#  i - indent level
#  p - prefix string for one level of indent
#  s - separator string between key and value
#
# EXAMPLE:
# % set d [dict create a {1 i 2 j 3 k} b {x y z} c {i m j {q w e r} k o}]
# % a {1 i 2 j 3 k} b {x y z} c {i m j {q w e r} k o}
# % pdict $d
# a ->
#   1 -> 'i'
#   2 -> 'j'
#   3 -> 'k'
# b -> 'x y z'
# c ->
#   i -> 'm'
#   j ->
#     q -> 'w'
#     e -> 'r'
#   k -> 'o'
#
proc pdict { d {i 0} {p "  "} {s " -> "} } {
    if { [catch {dict keys ${d}}] } {
        error "error: pdict - argument is not a dict"
    }
    set prefix [string repeat ${p} ${i}]
    set max 0
    foreach key [dict keys ${d}] {
        if { [string length ${key}] > ${max} } {
            set max [string length ${key}]
        }
    }
    dict for {key val} ${d} {
        puts -nonewline "${prefix}[format "%-${max}s" ${key}]${s}"
        if { [catch {dict keys ${val}}] } {
            puts "'${val}'"
        } else {
            puts ""
            pdict ${val} [expr ${i}+1] ${p} ${s}
        }
    }
}

LV I wonder if something like this should be included in the same place that parray is located in the Tcl distribution...