Updated 2012-09-21 10:38:39 by LkpPo

AF - Here is a small app which sits on the root window and displays a bar graph and numerical percentage. The color of the graph varies with the value. All options are configurable by editing the defaults or on the command line. The default display is my usage: displaying percentage of swap space used on FreeBSD.
 #!/bin/sh
 # \
 exec wish "$0" -- "$@"

 set orient h
 set geom 150x25+[expr {[winfo vrootwidth .] - 150}]+[expr {[winfo vrootheight .] - 25}]
 array set colors "0 white"
 set interval 5000
 set command {[string trimright [lindex [exec pstat -s] 10] %]}
 set from 0
 set to 100
 set background black
 set foreground white
 set label 1
 set font ""

 proc ref {} {
    global tag rect orient interval command to from geom
    eval set data $command
    if {$orient == "h"} {set w [winfo width .c]} elseif {$orient == "v"} {set w [winfo height .c]}
    set size [expr ($data - $from.00) / ($to.00 - $from.00)]
    set per [expr {round($size * 100)}]
    set size [expr $size * $w.00]
    .l configure -text ${per}%
    .c itemconfigure $tag -fill [getcolor $per]
    eval .c coords $tag $rect
    after $interval ref
 }

 proc getcolor {num} {
    global colors colorn
    foreach x $colorn {
        if {$x <= $num} {break}
        set ret $x
    }
    return $colors($x)
 }

 proc ParseCommandline { } {
    global argv argc
    set num 0
    while {[set tmp [lrange $argv $num end]] != ""} {
        switch -glob -- [lindex $tmp 0] {
            -geometry {
                global geom
                set geom [lindex $tmp 1]
                incr num 2
            }
            -colors {
                global colors
                array set colors [lindex $tmp 1]
                incr num 2
            }
            -orient {
                global orient
                set orient [lindex $tmp 1]
                incr num 2
            }
            -from {
                global from
                set from [lindex $tmp 1]
                incr num 2
            }
            -to {
                global to
                set to [lindex $tmp 1]
                incr num 2
            }
            -foreground {
                global foreground
                set foreground [lindex $tmp 1]
                incr num 2
            }
            -background {
                global background
                set background [lindex $tmp 1]
                incr num 2
            }
            -command {
                global command
                eval set command \"[lindex $tmp 1]\"
                incr num 2
            }
            -interval {
                global interval
                set interval [lindex $tmp 1]
                incr num 2
            }
            -label {
                global label
                set label 0
                incr num 1
            }
            -font {
                global font
                set font [lindex $tmp 1]
                incr num 2
            }
            -* {
                puts stderr "Unknown option [lindex $tmp 0]"
                exit
            }
            default {
                puts stderr "Too many arguments"
                exit
            }
        }
    }
 }

 ParseCommandline

 wm withdraw .
 . configure -borderwidth 0 -highlightthickness 0 -takefocus 0 -background ""
 wm title . xswapinfo
 wm geometry . $geom
 wm overrideredirect . 1
 update idletasks
 canvas .c -bg $background -borderwidth 0 -highlightthickness 0
 label .l -bg $background -fg $foreground -text 0%
 if {$font != ""} {
    .l configure -font $font
 }
 if {$orient == "h"} {
    if $label {pack .l -side left -fill both}
    pack .c -side right -expand 1 -fill both
    set rect "-1 -1 \$size \[winfo height .c\]"
 } elseif {$orient == "v"} {
    if $label {pack .l -side bottom -fill both}
    pack .c -side top -expand 1 -fill both
    set rect "-1 \[winfo height .c\] \[winfo width .c\] \[expr \[winfo height .c\] - \$size\]"
 }
 set colorn [lsort -integer -decreasing [array names colors]]
 set size 0
 update idletasks
 wm deiconify .
 set tag [eval .c create rectangle $rect]
 ref