Version 0 of ilist

Updated 2003-07-01 18:22:52

by Theo Verelst

of course feel free to comment, extend, ..

I was trying out some network (read: graph) things in Bwise, and am thinking about coming back to some basics I did years ago when I worked at university, for instance making an inspector window without all kinds of extra packages having to be loaded in simple enough and reusable tcl/tk code, and started by wanting to be able to in short list a hierarchy of lets call them objects..

Suppose we want to list all objects in the tk graphics hierarchy, like we would start with:

 winfo children .

And get a list of all root objects.

I made a routine called ilist which in short can do this hierarchically, with a simple and practical prettyprint, and which can be used for various other purposes:

 proc ilist { {begin {.}} {listf {winfo children}} {maxdepth {100}} {ident {0}} } {
   if {$maxdepth <1} return
   set de {}; set o {}
   for {set i 0} {$i < $ident} {incr i} {append de "   "}
   foreach i [eval "$listf $begin"] {
       append o "$i "
       puts "$de $i"
       append o [ilist $i $listf [expr $maxdepth-1] [expr $ident +1]]
   }
   return $o
 } 

Of course advanced programmers know what a recursive procedure is and can easily do this maybe as a one liner from the top of their head, but it is good practice to think straight about things, and conceptually I find this pleasing enough.

calling simply ilist in bwise prints:

 .fb
    .fb.bnewb
    .fb.quit
    .fb.p
    .fb.bwire
    .fb.bcdrum
    .fb.bcscope
    .fb.bscan
    .fb.bdel
    .fb.bsave
    .fb.bload
 .mw
    .mw.hscroll
    .mw.vscroll
    .mw.c
       .mw.c.pm
 .f
    .f.l
    .f.t
    .f.f
       .f.f.b
       .f.f.b2
       .f.f.f
       .f.f.bs
 .tt
    .tt.t
    .tt.f
       .tt.f.e
       .tt.f.s
       .tt.f.l

Practically, the same routine can be reused also to list files: