I also don't want to re-invent the wheel if there's a library that has
a non-blocking read-line.
(defvar *read-line-no-hang-buffer* (make-array 80 :element-type 'character :adjustable t :fill-pointer 0))
(defun read-line-no-hang (&optional (stream *standard-input*) (eof-error-p t) eof-value)
(loop
:for ch = (read-char-no-hang stream nil :eof)
:do (case ch
((nil) (return nil))
((:eof) (if eof-error-p
(error 'end-of-file :stream stream)
(return eof-value)))
((#\newline) (return (prog1 (copy-seq *read-line-no-hang-buffer*)
(setf (fill-pointer *read-line-no-hang-buffer*) 0))))
(otherwise (vector-push-extend ch *read-line-no-hang-buffer*)))))
(defun interactive-test/read-line-no-hang ()
(loop
:named test
:for line = (read-line-no-hang)
:for counter :from 0
:initially (terpri) (princ "Type something while I work (type done when done): ")
:do (when line
(princ "After ") (princ counter) (princ " loops, read: ") (write-line line)
(terpri) (princ "Type something while I work (type done when done): "))
:until (string-equal "done" (string-trim #(#\space #\tab) line))))
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
If this is under Unix and you haven't messed with the default terminal
settings, then no characters will be "ready" [in the sense of LISTEN or
READ-CHAR-NO-HANG] until you type a newline, so this should be all you need:
(and (listen) (read-line))
That is:
> (loop for line = (and (listen) (read-line))
until line
do
;; Your real "background" application goes here:
(format t "~&Help! Get me out of here! Type something, then <RET>: ")
(force-output)
(sleep 3)
finally
(format t "Thanks! You typed: ~s~%" line)
(return line))
Help! Get me out of here! Type something, then <RET>:
Help! Get me out of here! Type something, then <RET>:
Help! Get me out of here! Type something, then <RET>: O.k., y
Help! Get me out of here! Type something, then <RET>: ou mean
Help! Get me out of here! Type something, then <RET>: like th
Help! Get me out of here! Type something, then <RET>: is?
Thanks! You typed: "O.k., you mean like this?"
"O.k., you mean like this?"
>
-Rob
-----
Rob Warnock <rp...@rpw3.org>
627 26th Avenue <http://rpw3.org/>
San Mateo, CA 94403
Ah cool, I tested it out in Windows with CLISP (I think it uses Cygwin
or the default terminal) and it almost worked well. The only problem
is that a character or two were re-read. I don't care about that
though, I'm only going to be using it in a GNU/Linux terminal anyway ;p