[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Y or N with timeout

I would like to have a y-or-n-with-timeout function.  By that
I mean a function like y-or-n which will accept an answer
for a specified period of time and otherwise return a default

I tried to impelement this using read-char-no-hang, but this
always seems to return NIL.  I suppose I could use a dialog.

Here is the code I implemented:

;;; Like the normal Y-OR-N-P function, except that after <seconds> delay
;;; this will return the <Result> given as a default.
(defun y-or-n-with-timeout (seconds result fmt-str &rest fmt-args)
  (apply #'format t fmt-str fmt-args)
  (let ((timeout (+ seconds (get-universal-time)))) 
    (loop for chr = (read-char-no-hang)
          do (cond ((null chr)
                    (if (> (get-universal-time) timeout)
                      (return result)
                      (sleep 0.1)
                   ((member chr '(#\Y #\y #\space))
                    (return t))
                   ((member chr '(#\N #\n))
                    (return nil))
                   (t (terpri)
                      (apply #'format t fmt-str fmt-args))))))

Chris Eliot