#!/usr/local/bin/wish -f
# Calling up a requester and returning its result
# Calling:
# Request [-operation parameters] [-operation parameters]...
# Possible operations are:
#
#   -text "string"                           Displays text
#   -button "button Text" "Return String"    Makes Button
#   -input  "preset text"                    String input
#   -timeout seconds                         Times out after spec. seconds

set lcnt 0
set bcnt 0
set scnt 0
set timeout 20

for {set i 0} {$i < $argc} {incr i} {
   set cmd [lindex $argv $i]
   switch -- $cmd {
      "-text" {
         incr i
         set txt [lindex $argv $i]
         set  nl [expr [llength [split "$txt" "\n"]] - 1]
         set w 80
         if {$nl == 0} {set w [string length $txt]}
         incr nl
         text .l$lcnt -height $nl -width $w
         .l$lcnt insert end $txt
         pack .l$lcnt -side top
         incr lcnt
      }
      "-button" {
         incr i
         set btext [lindex $argv $i]
         incr i
         set result [lindex $argv $i]
         set x "button .b$bcnt -text \"$btext\" -command {puts $result; exit}"
         eval $x
         pack .b$bcnt -side left -pady 2m -padx 2m
         incr bcnt
      }
      "-input" {
         incr i
         entry .s$scnt -relief sunken -bd 2
         .s$scnt insert 0 [lindex $argv $i]
         set x "bind .s$scnt <Return> {puts \[.s$scnt get\] ; exit}"
         eval $x
         pack .s$scnt -side top -pady 2m -padx 2m
         focus .s$scnt
         incr scnt
      }
      "-timeout" {
         incr i
         set timeout [lindex $argv $i]
      }
   }
}

set timeout [expr $timeout * 10]
for {set i 0} {$i < $timeout} {incr i} {
   update
   after 100
}

exit
