Displaying Lists as Tables in a gnocl::text widget.

WJG (01/02/22) Making the most of a text widget can be a challenging task but sometimes the solutions can be quite simple. The gnocl::text widget allows the embedding of widgets into a page so the package's gnocl::table widget packed with labels is an option. Can something more simple be realized? The following snippet show how its possible to split a list into row and insert the row elements with a \t separator. Then, using tags, to set the display parameter for the table. Its not a table widget for sure, but visually it serves a similar function.

# !/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "[email protected]"

package require Gnocl

## insert the list contents as in text widget as a formatted table
#
# @param wid   text widget-id
# @param lst   tcl list
# @param id    table id
# @param args  additional tag parameters
#
proc tabulate {wid lst {id 1} args} {

   if {$args == ""} { set args "-tabs 100 -editable 0" }

   foreach row $lst { append res [join $row \t]\n }

   $wid tag create _tab_table_$id {*}$args ;# note: no error checking
   $wid insert end $res -tags _tab_table_$id

}

set txt [gnocl::text]
gnocl::window -child $txt -setSize 0.4

set lst(1) {{red orange yellow green blue indigo violet} {magenta cyan yellow brown grey black white}}
set lst(2) {{how now brown cow} {she sells sea shells by the sea shore}}
set lst(3) {{peter piper picked a peck of pickled peppers} {the peck of peppers peter piper picked}}

tabulate $txt $lst(1) 1 -tabs 120 -paragraph red -font {12}
tabulate $txt $lst(2) 2 -tabs 80 -foreground magenta
tabulate $txt $lst(3) 3 -tabs 80 -foreground white -paragraph #000000 -font {10}
tabulate $txt $lst(2) 4