Posted to tcl by aspect at Thu Sep 20 07:14:18 GMT 2012view raw
- #!/usr/bin/tclsh
- #
- # inspired by https://bitbucket.org/larsyencken/anytop/
- # does not include windowing function (limit to last N lines)
- # a bit more terminal-awareness would be nice
- package require term::ansi::send
- fconfigure stdin -blocking 0
- fileevent stdin readable consume
- set ::status "Reading ..."
- set ::start [clock seconds]
- set ::lines 0
- set ::counts {}
- set ::eof 0
- proc consume {} {
- if {[gets stdin data]<1} {
- if {[eof stdin]} {
- close stdin
- set ::eof 1
- set ::status "EOF"
- }
- } else {
- incr ::lines
- dict incr ::counts $data
- }
- }
- proc topN {n counts} {
- lrange [lsort -integer -decreasing -stride 2 [lreverse $counts]] 0 [expr {$n*2}]
- }
- proc elapsed {} {
- clock format [expr {[clock seconds]-$::start}] -format "%M:%S"
- }
- proc refresh {} {
- ::term::ansi::send::clear
- puts "[format "%6s" [elapsed]] elapsed, $::lines lines, [dict size $::counts] distinct values"
- puts ""
- foreach {count value} [topN 20 $::counts] {
- puts "[format %6s $count] $value"
- }
- if {$::eof} {
- puts "\nReached EOF: Press ^C to exit .."
- } else {
- after 250 refresh
- }
- }
- refresh
- vwait forever