Writing to a Terminal with Common Lisp and IOLib

Sometimes I do numerical calculations that also sometimes produce lots of output. Other times I do real-time sampling and I want to see the stream of data coming in (e.g., from a microphone).

With Emacs and SLIME, writing tons of junk to the terminal is usually very slow. You can mitigate this issue somewhat with the buggy

(defparameter *use-dedicated-output-stream* t)

by adding that to your .swank.lisp file.

But even then, sometimes you don’t want to trash your REPL and have your line count turn into ??.

With Stelian Ionescu’s IOLib and netcat, we can open a stream to which data can be sent and printed.

First, start netcat on a particular port in a terminal you’d like to get output from.

### OS X
$ nc -k -l 6007

### Linux
$ nc -l -p 6007

In Lisp, now open a stream, first loading iolib.trivial-sockets:

> (ql:quickload :iolib.trivial-sockets)
> (defvar *console* (iolib.trivial-sockets:open-stream "" 6007))

Now we can use *console* like any other output stream.

> (dotimes (i 10)
    (format *console* "~D " (random most-positive-fixnum)))

Whoops, nothing seems to show in our terminal. We need to flush the buffers.

> (finish-output *console*)

And we will see (something similar to)

1193941380623146742 2873853940934582137 2342493223442167775 3013752598065164257 2441597211547797803
1799302827895858725 525699342094945562 864811387508848656 3827825384193791716 3963993791726803706 

in our terminal.

Very useful indeed.

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Before you post, please prove you are sentient.

what is 2 + 9?