漂亮打印TCL列表

时间:2015-06-15 17:39:58

标签: scripting tcl

有一个如下所示的TCL列表(格式化以获得更好的可见性):

set mylist [list \
    "title_1                 title_2        title_3 " \
    "row1col1_bla            row1col2       row1col3 " \
    "r2c1                    r2c2_blablabla r2c3" \
    "r3c1_really_long_string r3c2           r3c3" \
]

我需要一个打印$mylist的程序,如:

   title_1      title_2  title_3
1) row1col1_bla row1col2 row1col3
2) r2c1         &FN_1    r2c3
3) &FN_2        r3c2     r3c3

  Footnotes:
  FN_1: r2c2_blablabla
  FN_2: r3c1_really_long_string

该程序应作为输入:

  • COLMAXLEN:来自$mylist的任何单个字符串的最大长度,该字符串将转到Footnotes部分(由$SHORTCUT_$index值替换)< / LI>
  • SHORTCUT:替换$mylist的任何个人成员的字符串,如果其长度大于$COLMAXLEN

以下功能就是这样做的。有没有可能简化或改进它的建议?

#!/usr/bin/tclsh

set COLMAXLEN   12
set SHORTCUT    "FN"

proc puts_list {mylist} {
    global COLMAXLEN SHORTCUT
    set num_row [llength $mylist]
    set num_col [llength [lindex $mylist 0]]
    set ref_list {}

    # Define/init col_width (a list having $num_col elements)
    set col_width {}
    for {set col 0} {$col < $num_col} {incr col} {
        lappend col_width 0
    }

    # Get the max width of each column AND
    # replace the elements > $COLMAXLEN with footnote shortcuts!
    for {set row 0} {$row < $num_row} {incr row} {
        set new_row {}
        for {set col 0} {$col < $num_col} {incr col} {
            set myrow  [lindex $mylist $row]
            set myitem [lindex $myrow $col]
            set mysize [string length $myitem]
            if { $mysize > $COLMAXLEN } {
                lappend ref_list $myitem
                set myitem "&[subst $SHORTCUT]_[llength $ref_list]"
                set mysize [string length $myitem]
            }
            if { $mysize > [lindex $col_width $col] } {
                lset col_width $col $mysize
            }
            lappend new_row $myitem
        }
        lset mylist $row $new_row
    }

    # Start printing
    set num_col_width [expr [string length $num_row] +1]
    puts ""
    for {set row 0} {$row < $num_row} {incr row} {
        if { $row == 0 } {
            puts -nonewline [format "%[subst $num_col_width]s" { }]
        } else {
            puts -nonewline [format "%[subst $num_col_width]s" "$row)"]
        }
        puts -nonewline " "
        for {set col 0} {$col < $num_col} {incr col} {
            set myrow  [lindex $mylist $row]
            set myitem [lindex $myrow $col]
            set mysize [expr [lindex $col_width $col] +1]
            puts -nonewline [format "%-[subst $mysize]s" $myitem]
        }
        puts ""
    }
    puts ""
    puts "  Footnotes:"
    set ref_num [llength $ref_list]
    for {set i 0} {$i < $ref_num} {incr i} {
        puts "  [subst $SHORTCUT]_[format %-[subst [string length $ref_num]]s [expr $i + 1]]: [lindex $ref_list $i]"
    }
    puts ""
}

假设:TCL版本为8.4

1 个答案:

答案 0 :(得分:1)

# Print the row number
proc put_row_number {rowNumber} {
    if {$rowNumber == 0} {
        puts -nonewline "   "
    } else {
        puts -nonewline "$rowNumber) "
    }
}

proc put_table {table {colmaxlen 12} {shortcut FN}} {
    set ref {}
    set refCount 0
    set rowCount 0

    foreach row $table {
        put_row_number $rowCount
        incr rowCount
        foreach cell $row {
            if {[string length $cell] > $colmaxlen} {
                incr refCount
                set key "${shortcut}_${refCount}"
                lappend ref $key $cell
                set cell "&$key"
            }
            puts -nonewline [format "%-*s " $colmaxlen $cell]
        }
        puts ""
    }

    puts "\n   Footnotes:"
    foreach {key value} $ref {
        puts "   $key: $value"
    }
}

set mylist {
    "title_1                 title_2        title_3 "
    "row1col1_bla            row1col2       row1col3 "
    "r2c1                    r2c2_blablabla r2c3"
    "r3c1_really_long_string r3c2           r3c3"
}
put_table $mylist