#!/bin/sh # This grossness allows wish to be anywhere in PATH: \ exec wish "$0" ${1+"$@"} # dateclock 1.2.2 (2003-May-07-Wed) # Adam M. Costello # http://www.nicemice.net/amc/ # Displays the date as a text string configurable via the dateFormat # and useGMT resources (see the Tcl clock(n) man page). These can be # overridden by the + and -u options, respectively, similar to # the UNIX date command. # # The sleep interval continually adapts to try to wake up the process # just when the seconds need to change. # # Copying and pasting from the running clock is supported, which is # pretty slick, if I do say so myself. # # The geometry can be set via the wmGeometry resource (see the Tk wm(n) # man page), which is overridden by the -geometry option. # # Colors, fonts, etc. can be specified using resources (see the Tk # options(n) man page), and some can be overridden by the following # Xt-like options: -bg (-background), -fg (-foreground), -fn (-font), # -iconic, -title, -xrm. Additionally, wish supports the Xt-like # options -display, -name, -sync. # Set default options: option add *dateFormat %c startupFile option add *useGMT false startupFile option add *BorderWidth 0 startupFile option add *Pad 1 startupFile option add *date.Font 8x13bold startupFile # Read options that we handle explicitly: set date_format [option get . dateFormat DateFormat] set use_gmt [option get . useGMT UseGMT ] # Parse command-line options: # We try to support the standard Xt options, but we don't support # all of them. Note that -display, -geometry, -name, and -sync (not # -synchronous) are handled by wish. We also support + and -u # like UNIX date. while {[llength $argv] > 0} { set opt [lindex $argv 0] switch -glob -- $opt { -bg - -background { if {[llength $argv] < 2} { error "$opt needs an argument" } option add *background [lindex $argv 1] set argv [lrange $argv 2 end] } -fg - -foreground { if {[llength $argv] < 2} { error "$opt needs an argument" } option add *foreground [lindex $argv 1] set argv [lrange $argv 2 end] } -fn - -font { if {[llength $argv] < 2} { error "$opt needs an argument" } option add *Font [lindex $argv 1] set argv [lrange $argv 2 end] } -iconic { wm iconify . set argv [lrange $argv 1 end] } -title { if {[llength $argv] < 2} { error "$opt needs an argument" } wm title . [lindex $argv 1] set argv [lrange $argv 2 end] } -u { set use_gmt true set argv [lrange $argv 1 end] } -xrm { if {[llength $argv] < 2} { error "$opt needs an argument" } set resource [lindex $argv 1] set argv [lrange $argv 2 end] if {! [regexp {([^:]*): *(.*)$} $resource junk key value]} { error "resource string lacks a colon: $resource" } option add $key $value } +* { set date_format [string range $opt 1 end] set argv [lrange $argv 1 end] } -* { error "unrecognized option $opt" } default { error "unrecognized argument $opt" } } } # Create GUI: if {$tk_version >= 8.4} { set date_state readonly } else { set date_state disabled } entry .date -width 0 -state $date_state pack .date # Advance the clock: proc advance {} { global interval date_format use_gmt last_refresh date_state set now [clock seconds] if {$now == $last_refresh} { incr interval after 1 advance return } incr interval -1 set date [clock format $now -format $date_format -gmt $use_gmt] set anchor [.date index anchor] .date configure -state normal if {! [.date selection present]} { .date delete 0 end .date insert 0 $date } else { set first [.date index sel.first] set last [.date index sel.last] .date delete 0 end .date insert 0 $date .date selection range $first $last } .date configure -state $date_state .date selection from $anchor set last_refresh $now after $interval advance } # Initialize the clock: set interval 1001 set last_refresh -1 advance # If -geometry was supplied on the command line, let it override # wmGeometry, otherwise use wmGeometry: if {! [info exists geometry]} { set geometry [option get . wmGeometry WmGeometry] if {$geometry != ""} { tkwait visibility . wm geometry . $geometry } }