Interactive terminal application

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Interactive terminal application

Post 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))))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Interactive terminal application

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Interactive terminal application

Post by cormullion »

Ah, that looks promising. Thanks!

Locked