Page 1 of 1

Interactive terminal application

Posted: Mon Oct 10, 2011 5:04 pm
by cormullion
I've been writing an IRC library, but I'm a bit stuck on the best way to arrange things so that you can type input at a terminal while still running an event loop that polls the server periodically. As we know, there's no way you can have a non-blocking read-line, so how would it be possible to combine both 'read' and 'write' tasks into a single terminal window? (My previous attempt - using fork - doesn't seem to be what I'm looking for.)

My 'read' code looks like this:

Code: Select all

(define (read-irc)
    (let ((buffer {}))
        (while  (not (net-select Iserver "read" 500))
                (sleep 500))    
        (net-receive Iserver buffer 8192 "\n")
        (unless (empty? buffer)
            (parse-buffer buffer))))

(define (read-irc-loop)
    (let ((buffer {}))
        (while Iconnected
            (read-irc))))

Re: Interactive terminal application

Posted: Mon Oct 10, 2011 6:09 pm
by Lutz
The following program will print a dot after every second until you enter something from the keyboard:

Code: Select all

#!/usr/bin/newlisp

(while (zero? (peek 0)) (sleep 1000) (println ".") ) ; 0 for stdin

(println "--->" (read-line 0))

(exit)
Instead of waiting and printing dots, something else could be done, like waiting for and displaying input from IRC.


See also here: http://www.newlisp.org/downloads/newlis ... .html#peek

Re: Interactive terminal application

Posted: Mon Oct 10, 2011 9:29 pm
by cormullion
Ah, that looks promising. Thanks!