dbohdan/sqawk

patch for new option -numcolumns

kpvetter opened this issue · 1 comments

The ps | jimsh ./tabulate.tcl example on the tcler's wiki is kind of a red herring--in real life the 4th column is multiple words and the result is gibberish. I'm not sure how to make a patch so I've included the updated ::tabulate::main procedure to handle that new switch. This makes ps | tclsh ./tabulate.tcl -numcolumns 4 look good.

# Read the input, process the command line options and output the result.
proc ::tabulate::main {argv0 argv} {
    options::process $argv \
        store -FS in FS default {} \
        store -style in style default default \
        store -alignments or -align in alignments default {} \
        store -margins in margins default 0 \
        store -numcolumns in numcolumns default 0
    set data [split [string trim [read stdin]] \n]

    # Input field separator. If none is given treat each line of input as a Tcl
    # list.
    if {$FS ne {}} {
        set updateData {}
        foreach line $data {
            lappend updateData [split $line $FS]
        }
        set data $updateData
    }
    if {$numcolumns > 0} {
        if {$FS eq {}} { set FS " "}
        set updateData {}
        foreach line $data {
            set last [join [lrange $line $numcolumns-1 end] $FS]
            lappend updateData [lreplace $line $numcolumns-1 end $last]
        }
        set data $updateData
    }

    # Accept style names rather than style *values* that ::tabulate::tabulate
    # normally takes.
    set styleName [::tabulate::dict-get-default $argv default -style]

    puts [tabulate -data $data \
            -style [::tabulate::style::by-name $style] \
            -alignments $alignments \
            -margins $margins]
}

My bad that I did not explain it on the wiki, but the command line interface for Tabulate is pretty much a toy intended for demonstration purposes. It started out limited in what it could do, and because Tabulate was immediately folded into the Sqawk project it has staid limited to avoid duplicating Sqawk's functionality.

I think giving Tabulate even this much of a command line interface may have been a mistake, so I can't accept your patch. What I recommend instead is to use Sqawk. It can print the same kind of tables, but is more flexible in what it can do with its input. In particular, it can emulate -numcolumns 4 with the option fields=1,2,3,4-end (see Input formats).

I've updated the README to make the examples of parsing the output of ps more realistic (first, second). Replace -output json,indent=1 with -output table in the second example to print a table.