diff --git a/samples/Tcl/owh b/samples/Tcl/owh new file mode 100755 index 00000000..9354c704 --- /dev/null +++ b/samples/Tcl/owh @@ -0,0 +1,63 @@ +#!/usr/bin/env tclsh +# http://wiki.tcl.tk/906 + +if {[llength $argv] < 1} { + puts "usage: owh ?init? body ?exit? + performs body (in Tcl) for each line (\$0) from stdin + owh: Ousterhout - Welch - Hobbs, to name a few" + exit -1 +} + +proc awksplit {text {split default}} { + set no 0 + if {$split eq "default"} { + set t {} + foreach string [split $text] { + if {$string ne {}} { + lappend t $string + } + } + } else { + set t [list $text $split] + } + uplevel 1 [list set NF [llength $t]] + foreach i $t {uplevel 1 [list set [incr no] $i]} + uplevel 1 {set 0 {};trace variable 0 ru 0} +} +proc 0 {_name index op} { + switch $op { + r { + uplevel { + set 0 {} + for {set i 1} {$i <= $NF} {incr i} {lappend 0 [set $i]} + set 0 [join $0 $OFS] + } + } + u {rename 0 {} ;# leave no traces of the trace..} + } +} + +proc print s {if {[catch {puts $s}]} exit} ;# good for broken pipe + +set FS default +set OFS { } + +if {[llength $argv] > 1} { + eval [lindex $argv 0] + set _body [lindex $argv 1] ;# strip outer braces + set _exit [lindex $argv 2] +} else { + set _body [lindex $argv 0] ;# strip outer braces + set _exit {} +} + +set NR 1 +while 1 { + gets stdin line + if {[eof stdin]} break + awksplit $line $FS + eval $_body + incr NR +} +set res [eval $_exit] +if {[string length $res]} {puts $res} diff --git a/samples/Tcl/starfield b/samples/Tcl/starfield new file mode 100755 index 00000000..674ba779 --- /dev/null +++ b/samples/Tcl/starfield @@ -0,0 +1,28 @@ +#!/usr/bin/env wish +# http://wiki.tcl.tk/14140 + +proc stars'go {c factor} { + set w [winfo width $c] + set h [winfo height $c] + $c scale all [expr {$w/2}] [expr {$h/2}] $factor $factor + foreach item [$c find all] { + if {[llength [$c bbox $item]] == 0} {$c delete $item; continue} ;# (1) + foreach {x0 y0 x1 y1} [$c bbox $item] break + if {$x1<0 || $x0>$w || $y1<0 || $y0>$h} {$c delete $item} + } + time { + set x [expr {rand()*$w}] + set y [expr {rand()*$h}] + set col [lpick {white yellow beige bisque cyan}] + $c create oval $x $y [expr {$x+1}] [expr {$y+1}] -fill $col \ + -outline $col + } 10 + after $::ms [info level 0] +} +proc lpick list {lindex $list [expr {int(rand()*[llength $list])}]} +#-- Let's go! +pack [canvas .c -bg black] -fill both -expand 1 +set ms 40 +bind . {incr ms -5} +bind . {incr ms 5} +stars'go .c 1.05