[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
- To: common-lisp@SAIL.STANFORD.EDU
- Subject: Clipping output
- From: MURRAY%cs.umass.edu@RELAY.CS.NET
- Date: Mon, 6 Apr 87 15:46 EDT
> From: ELIOT%cs.umass.edu@RELAY.CS.NET
> Is there any good way, preferably in FORMAT to
> clip output longer than a specified length?
> The effect I need is equivalent to:
> (subseq (format nil "~a" XXX) 0 20)
> but I would rather not do all the consing that this implies.
> In general, I would like to be able to specify
> the maximum field width of most of the format directives.
The code below would truncate output with minimal consing (at least
close to a constant).
Of course, this only works for ONE call to FORMAT, and so you
would need to break down a format string
into separate calls to get the effect on more than one directive.
With all the other whiz-bang features of format, a max-width field is
probably not too much to ask for. Anybody have any idea how difficult
it would be to add it in?
(defvar *truncate-buffer* (make-array 200 ;; The bigger it is, less consing
(defun TRUNCATE-FORMAT (max-width stream format-string &rest format-args)
(setf (fill-pointer *truncate-buffer*) 0) ;; Start Empty
(with-output-to-string (string-stream *truncate-buffer*) ;; Cons a stream.
(apply #'format string-stream format-string format-args)) ;; Fill it up
(when (> (fill-pointer *truncate-buffer*) max-width) ;; Truncate
(setf (fill-pointer *truncate-buffer*) max-width))
(format stream "~A" *truncate-buffer*)) ;; Output