-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty-printing.lisp
31 lines (24 loc) · 1.11 KB
/
pretty-printing.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(in-package :language)
(defun rotate-rows-to-columns (rows)
(loop for remaining = rows then (mapcar #'cdr remaining)
while (not (every #'null remaining))
collect (mapcar #'car remaining)))
(defun maximize-length (list &key (key #'identity))
(loop for element in list maximizing (length (funcall key element))))
(defun pad-list (list length &optional (pad-element nil))
(loop for el on list
for x from 1
do (when (null (cdr el))
(setf (cdr el) (make-list (- length x) :initial-element pad-element))
(return list))))
(defun print-table (rows &key (gap " ") (align :left))
(loop
with max-row-length = (apply #'max (mapcar #'length rows))
with control-string =
(format nil
(concatenate
'string "~{~~~D" (ecase align (:right "@") (:left "")) "A~^" gap "~}~%")
(mapcar (lambda (row) (maximize-length row :key #'princ-to-string))
(rotate-rows-to-columns rows)))
for row in (mapcar (lambda (row) (pad-list row max-row-length "")) rows)
do (apply #'format t control-string row)))