johnbakers
johnbakers

Reputation: 24750

Format call doesn't indent as requested

I have this:

(format *standard-output* "~v@a ~a ~%" (* 5 indent) "End of Parent" (* 5 indent))

The curious issue is that (* 5 indent) is added as a debug item, and it is correct in the printout. However, the text is not indented when called inside my function, even though it prints out the correct indent value. If I just perform this line at the REPL, it prints out correctly, with indentation. My entire function is this:

(defun print-object-and-children-data (object indent)
  "Recursively prints a chart object and its children,
   with each child level indented further."
  (let ((children (get-chart-children object)))
    ;; This indents correctly.
    (format *standard-output* "~v@a Level: ~a~%"
            (* 5 indent) object indent)
    (mapc (lambda (x)
            (when x (print-object-and-children-data x (+ 1 indent))))
          children)
    ;; This does not.
    (format *standard-output* "~v@a ~a ~%"
            (* 5 indent) "End of Parent" (* 5 indent)))) 

All text content is correct. Also, whether that final format is part of the let makes no difference (it doesn't need anything from children). Something in this function is somehow affecting the indentation of the format call, but since the format prints out the correct indent value, what could be the cause of no indentation?

Upvotes: 2

Views: 725

Answers (2)

Terje D.
Terje D.

Reputation: 6315

In order to get indented output, you could also use format's tabulate (~t) directive:

(dolist (i '(0 5 10 15 20))
   (format t "~&~vt~a~%" i i))

The @modifier may be used if you want relative tabulation instead of absolute tabulation.

Upvotes: 2

Lars Brinkhoff
Lars Brinkhoff

Reputation: 14285

The v in ~v@A does not specify indentation. It specifies field width. Try this:

(dolist (i '(5 10 15))
  (dolist (x '(1 123 12345))
    (format t ">~v@A< (i=~D)~%" i x i)))

Note how the output is not aligned to the left.

Here's one possible way to have indented output:

(dolist (i '(5 10 15))
  (dolist (x '(1 123 12345))
    (format t ">~vA~A< (i=~D)~%" i " " x i)))

(Fixing the bug here is left as an exercise.)

Upvotes: 3

Related Questions