[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bug/problems with fmt (was Re: [Chicken-users] Columnar text display cod
From: |
Robin Lee Powell |
Subject: |
Bug/problems with fmt (was Re: [Chicken-users] Columnar text display code?) |
Date: |
Fri, 6 Jul 2007 13:59:26 -0700 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
On Fri, Jul 06, 2007 at 12:19:36PM -0700, Robin Lee Powell wrote:
> It may be that fmt-columns would help, but the explanation of
> fmt-columns is *totally* opaque to me; can anyone explain it?
Hoo-boy the problems I've had with fmt. :( It seems to be lovely
in general, but it really doesn't want to do what I want, which is:
1. Output in columns
2. With minimal usage (if a columns only takes up 3 characters,
don't use more than that
3. With a maximum total width
Short version: It's step 3 that's the problem. fmt-colmuns won't
do it, and trim or join or something hangs on me in *very* simple
cases.
Detailed version:
First of all, fmt-columns doesn't ever give access to the completed
lines; that is:
(fmt #t
(fmt-columns
(list
(lambda (x) (display (conc "\nx: " x "\n")) (dsp x))
"foo\nbar\nbaz\n")
(list
(lambda (x) (display (conc "\ny: " x "\n")) (dsp x))
"1\n2\n3\n")))
Outputs:
x: foo
foo
y: 1
1
x: bar
bar
y: 2
2
x: baz
baz
y: 3
3
Since at the end of fmt-columns, I've just got one big string, I
can't use any of fmt's facilities to operate on the lines within it
anymore; I have to split it back into lines myself.
Unfortunately, fmt hangs on the obvious solution:
(fmt #t
(join
(lambda (x) (trim 5 x))
(string-split "foo foo\nbar bar\n" "\n")
nl))
That just hangs. I don't begin to have the skills to debug fmt
itself here, so I guess I'll just post-process the string myself to
handle the trimming.
Here's my ad-hoc solution for turning the list "infos", which looks
like (("ID" "Artist" "Title" ...) (1 "Sting" "Fields Of Gold" ...)
...), into the columnar output I want. I'm sure I could use fmt
more effectively; suggestions (as always) welcome.
(define pretty-infos1
(fmt
#f
(apply
fmt-columns
(map
; Pad the column elements to their max width + 1, and return
; a list that fmt-columns can understand
(lambda (x)
(let*
; Get the max width of this
; column
[(maxw (apply max (map string-length x)))]
; list for fmt-columns
(list
; formatter
dsp
; data
(join dsp
(map (lambda (y)
(pad/right (+ 1 maxw) y))
x)
nl))))
(apply zip infos)))))
(fmt #t "done pretty1: " nl pretty-infos1 nl)
(define pretty-infos
(string-intersperse
(map
(lambda (x) (string-take x 127))
(string-split pretty-infos1 "\n"))
"\n"))
(fmt #t "done pretty: " nl pretty-infos nl)
-Robin
--
http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/
Reason #237 To Learn Lojban: "Homonyms: Their Grate!"
Proud Supporter of the Singularity Institute - http://singinst.org/