From: Jimbo
Subject: reading ints from stream
Date: 
Message-ID: <20061011105048.A9A8.NO-EMAIL@nowhere.com>
Hi All,

I'm pretty new to Lisp, so here goes:

I would like write a function to get the rows and cols of a terminal:

$ stty size
24 80

No to capture these numbers in a lisp function:

(defun get-terminal-size ()
    (setf s (run-shell-command "stty size" :output :stream))
    (setf rows (read-integer s ???))
    (setf cols (read-integer s ???))
    (print rows)
    (print cols)
)

I'm using CLisp and am not sure how to read these integers.

Thanks,

Jim

From: K Livingston
Subject: Re: reading ints from stream
Date: 
Message-ID: <1160559175.237921.92850@m73g2000cwd.googlegroups.com>
A very minimalist version of the function you specified could be
something like this (i checked it in my cygwin clisp)

(defun get-terminal-size ()
  (let* ((stream (run-shell-command "stty size" :output :stream))
         (rows (read stream))
         (cols (read stream)))
    (values rows cols)))

I think this will generate hard lisp errors for good or bad, or
potentially get you non-numbers in cols and rows if the stty command
fails or returns an error message etc.  So depending on what you are
doing you might want to trap for that (or maybe not?) and check or
return something else before (or in) the values call.

you might also be interested in:
*print-lines*
*print-right-margin*

As far as general lisp stuff goes, unless you have rows and cols
(defined outside of the scope of get-terminal-size (for example with
defvar) you probably don't want to be setf-ing them like that.  If you
do, then you probably want to name them in the more standard convention
with asterisk (i don't know the plural of "asterisk") around their
names, as a naming cue that they are intended to be special.

if you were just looking for local variables then you should probably
be using a let form.

Kevin


Jimbo wrote:
> Hi All,
>
> I'm pretty new to Lisp, so here goes:
>
> I would like write a function to get the rows and cols of a terminal:
>
> $ stty size
> 24 80
>
> No to capture these numbers in a lisp function:
>
> (defun get-terminal-size ()
>     (setf s (run-shell-command "stty size" :output :stream))
>     (setf rows (read-integer s ???))
>     (setf cols (read-integer s ???))
>     (print rows)
>     (print cols)
> )
>
> I'm using CLisp and am not sure how to read these integers.
> 
> Thanks,
> 
> Jim
From: Timofei Shatrov
Subject: Re: reading ints from stream
Date: 
Message-ID: <452cb776.9607234@news.readfreenews.net>
On Wed, 11 Oct 2006 10:55:24 +0200, Jimbo <········@nowhere.com> tried to
confuse everyone with this message:

>Hi All,
>
>I'm pretty new to Lisp, so here goes:
>
>I would like write a function to get the rows and cols of a terminal:
>
>$ stty size
>24 80
>
>No to capture these numbers in a lisp function:
>
>(defun get-terminal-size ()
>    (setf s (run-shell-command "stty size" :output :stream))
>    (setf rows (read-integer s ???))
>    (setf cols (read-integer s ???))
>    (print rows)
>    (print cols)
>)
>
>I'm using CLisp and am not sure how to read these integers.

(read s)

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Alan Crowe
Subject: Re: reading ints from stream
Date: 
Message-ID: <86y7rnkmnb.fsf@cawtech.freeserve.co.uk>
Jimbo <········@nowhere.com> writes:

> $ stty size
> 24 80
> 
> No to capture these numbers in a lisp function:
> 
> (defun get-terminal-size ()
>     (setf s (run-shell-command "stty size" :output :stream))
>     (setf rows (read-integer s ???))
>     (setf cols (read-integer s ???))
>     (print rows)
>     (print cols)
> )
> 
> I'm using CLisp and am not sure how to read these integers.
> 

[1]> (defun get-terminal-size ()
       (let ((size (read-line (run-shell-command "stty size"
                                                :output :stream))))
         (multiple-value-bind (rows pointer)
             (parse-integer size :junk-allowed t)
           (let ((cols (parse-integer size :start pointer)))
             (values rows cols)))))
GET-TERMINAL-SIZE
[2]> (get-terminal-size)
24 ;
80

and after tugging on the corner of the xterm

[3]> (get-terminal-size)
52 ;
102 :-)

Alan Crowe
Edinburgh
Scotland
From: Bill Atkins
Subject: Re: reading ints from stream
Date: 
Message-ID: <m27iz6pxad.fsf@weedle-24.dynamic.rpi.edu>
Alan Crowe <····@cawtech.freeserve.co.uk> writes:

> [1]> (defun get-terminal-size ()
>        (let ((size (read-line (run-shell-command "stty size"
>                                                 :output :stream))))
>          (multiple-value-bind (rows pointer)
>              (parse-integer size :junk-allowed t)
>            (let ((cols (parse-integer size :start pointer)))
>              (values rows cols)))))

Seems like that might need a WITH-OPEN-STREAM to ensure that size gets
closed.