first commit

This commit is contained in:
frese 2004-09-15 14:49:54 +00:00
parent 5921374d20
commit 03d7ee90f6
1 changed files with 37 additions and 0 deletions

37
examples/timed-choice.scm Executable file
View File

@ -0,0 +1,37 @@
#!/bin/sh
exec scsh -lel expect/load.scm -o expect -o srfi-13 -e main -s "$0" "$@"
!#
(define (display-usage)
(display "Usage: timed-choice.scm timeout query choices...\n")
(display "Provides a query of the user with a timeout and a default answer.\n"))
(define (raise-too-few-arguments)
(display "timed-choice.scm: too few arguments\n")
(display-usage)
(exit))
(define (main args)
(if (< (length args) 4)
(raise-too-few-arguments))
(let ((timeout (string->number (second args)))
(query (third args))
(choices (cdddr args)))
(let ((default (car choices))
(pattern (re-choice (map re-string choices))))
(display (format #f "~a [~a] " query (string-join choices ", ")))
(display
(expect loop ()
(option (timeout timeout)
(on-timeout default))
((user-task)
((rx (: bos ,pattern (| "\n" "\r\n"))) (m)
(match:substring m))
((rx (: bos (| "\n" "\r\n"))) ()
(string-append default "\n"))
(else
(display (format #f "Please enter one of the choices: ~a.\n"
(string-join choices ", ")))
(loop)))))
;;(newline)
)))