[racket] Pretty printing and extra indentation

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Mon Jan 6 15:45:07 EST 2014

Hi all,

Does anyone know an easy way to add extra indentation to pretty printed
output using racket/pretty?

For example, suppose I want to add pretty printed output after part of
an error message like:

  expected: (<pretty printed value>
             <should be aligned like this>)

I would want any linebreak-separated portions to align with the first
line with "expected:".

So far, I've tried using the `pretty-print-print-line` parameter to add
some extra indentation, but it doesn't work like I expect. For example:

  #lang racket

  ;; how much extra indentation to add
  (define indent 2)

  ;; to parameterize `pretty-print-print-line`
  (define (print-line line-num out old-line-length cols)
    (cond [(and line-num
                (not (= line-num 0))
                (not (eq? cols 'infinity)))
           (newline out)
           ;; these two lines differ from the default
           (display (make-string indent #\space) out)
           ;; changing this to `indent` has weird behavior
           0]
          [else 0]))

  (parameterize ([pretty-print-print-line print-line])
    (display "  ")
    (pretty-display
     '(λ (x y z)
        (aaaaaaaa bbbbbbbbbbbbbb cccccccccccccc ddddddddd eeeeeeee ffffffff ggggggg hhhhhhh)))
    (newline)
    (display "  ")
    (pretty-display
     '(some-other-identifier (x y z)
        (aaaaaaaa bbbbbbbbbbbbbb cccccccccccccc ddddddddd eeeeeeee ffffffff ggggggg hhhhhhh)))
    (newline))

The output I'd like to get is (indentation significant):

  (lambda (x y z)
    (aaaaaaaa
     bbbbbbbbbbbbbb
     cccccccccccccc
     ddddddddd
     eeeeeeee
     ffffffff
     ggggggg
     hhhhhhh))
  (some-other-identifier
   (x y z)
   (aaaaaaaa
    bbbbbbbbbbbbbb
    cccccccccccccc
    ddddddddd
    eeeeeeee
    ffffffff
    ggggggg
    hhhhhhh))

but I get:

  (lambda (x y z)
    (aaaaaaaa
       bbbbbbbbbbbbbb
       cccccccccccccc
       ddddddddd
       eeeeeeee
       ffffffff
       ggggggg
       hhhhhhh))
  (some-other-identifier
   (x y z)
   (aaaaaaaa
      bbbbbbbbbbbbbb
      cccccccccccccc
      ddddddddd
      eeeeeeee
      ffffffff
      ggggggg
      hhhhhhh))

Any idea what I'm doing wrong? There seems to be some extra indentation
added to the output in the inner expression. Am I missing some obvious
easy method to do this?

Cheers,
Asumu

Posted on the users mailing list.