38 lines
1.1 KiB
Scheme
38 lines
1.1 KiB
Scheme
|
#!/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)
|
||
|
)))
|