telephil / cl-ascii-table

Common Lisp API to present tabular data in ASCII-art tables
MIT License
18 stars 5 forks source link

Added a way to define custom formatter for each field #2

Closed svetlyak40wt closed 4 years ago

svetlyak40wt commented 4 years ago

It might be useful, for example, if you want to render float numbers with a limited number of digits after the point.

This can be done like that:

CL-USER> (let ((ascii-table:*default-value-formatter*
                 (lambda (value)
                   (format nil
                           (typecase value
                             (double-float "~,2F")
                             (t "~A"))
                           value))))
             (let ((ascii (ascii-table:make-table '("Name" "Value"))))
               (ascii-table:add-row ascii
                                    (list "Pi" pi))
               (ascii-table:display ascii t)))
+------+-------+
| Name | Value |
+------+-------+
| Pi   |  3.14 |
+------+-------+
telephil commented 4 years ago

Looks good to me. Thanks.