Skip to content

Latest commit

 

History

History
915 lines (765 loc) · 23.1 KB

ascii-tables.md

File metadata and controls

915 lines (765 loc) · 23.1 KB

ASCII tables

The ASCII tables module provides a simple way to render tabular data in pure ascii.

Basic without styling

Without Header & Footer Header Header & Footer
(do
  (load-module :ascii-table)
  (ascii-table/print 
    nil 
    [[1 "1"   "2"  ] 
     [2 "10"  "20" ] 
     [3 "100" "200"]] 
    nil 
    :standard
    1))
(do
  (load-module :ascii-table)
  (ascii-table/print 
    ["" "header 1" "header 2"] 
    [[1 "1"   "2"  ] 
     [2 "10"  "20" ] 
     [3 "100" "200"]] 
    nil
    :standard
    1))
(do
  (load-module :ascii-table)
  (ascii-table/print 
    ["" "header 1" "header 2"] 
    [[1 "1"   "2"  ] 
     [2 "10"  "20" ] 
     [3 "100" "200"]] 
    ["" "footer 1" "footer 3"] 
    :standard
    1))
+---+-----+-----+
| 1 | 1   | 2   |
+---+-----+-----+
| 2 | 10  | 20  |
+---+-----+-----+
| 3 | 100 | 200 |
+---+-----+-----+




+---+----------+----------+
|   | header 1 | header 2 |
+---+----------+----------+
| 1 | 1        | 2        |
+---+----------+----------+
| 2 | 10       | 20       |
+---+----------+----------+
| 3 | 100      | 200      |
+---+----------+----------+


+---+----------+----------+
|   | header 1 | header 2 |
+---+----------+----------+
| 1 | 1        | 2        |
+---+----------+----------+
| 2 | 10       | 20       |
+---+----------+----------+
| 3 | 100      | 200      |
+---+----------+----------+
|   | footer 1 | footer 3 |
+---+----------+----------+

Header, footer, cell alignment

Without Header & Footer Header Header & Footer
(do
  (load-module :ascii-table)
  (ascii-table/print
    ;; columns 
    [{:body  {:align :left, :overflow :newline}
      :width 7}
     {:body  {:align :center, :overflow :newline}
      :width 7}
     {:body  {:align :right, :overflow :newline}
      :width 7}] 
    ;; data
    [[1 "1"     "2"    ] 
     [2 "100"   "200"  ] 
     [3 "10000" "20000"]] 
    :double
    1))
     
     
     
(do
  (load-module :ascii-table)
  (ascii-table/print 
    ;; columns 
    [{:header {:text "hd 1", :align :left }
      :body   {:align :left, :overflow :newline}
      :width  7}
     {:header {:text "hd 2", :align :center }
      :body   {:align :center, :overflow :newline}
      :width  7}
     {:header {:text "hd 3", :align :right }
      :body   {:align :right, :overflow :newline}
      :width  7}] 
     ;; data
     [[1 "1"     "2"    ] 
      [2 "100"   "200"  ] 
      [3 "10000" "20000"]] 
     :double
     1))
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:header {:text "hd 1", :align :left }
      :body   {:align :left, :overflow :newline}
      :footer {:text "ft 1", :align :left }
      :width  7}
     {:header {:text "hd 2", :align :center }
      :body   {:align :center, :overflow :newline}
      :footer {:text "ft 2", :align :center }
      :width  7}
     {:header {:text "hd 3", :align :right }
      :body   {:align :right, :overflow :newline}
      :footer {:text "ft 3", :align :right }
      :width  7}] 
     [[1 "1"     "2"    ] 
      [2 "100"   "200"  ] 
      [3 "10000" "20000"]] 
    :double
    1))
╔═════════╤═════════╤═════════╗
║ 1       │    1    │       2 ║
╟─────────┼─────────┼─────────╢
║ 2       │   100   │     200 ║
╟─────────┼─────────┼─────────╢
║ 3       │  10000  │   20000 ║
╚═════════╧═════════╧═════════╝




╔═════════╤═════════╤═════════╗
║ hd 1    │   hd 2  │    hd 3 ║
╠═════════╪═════════╪═════════╣
║ 1       │    1    │       2 ║
╟─────────┼─────────┼─────────╢
║ 2       │   100   │     200 ║
╟─────────┼─────────┼─────────╢
║ 3       │  10000  │   20000 ║
╚═════════╧═════════╧═════════╝



╔═════════╤═════════╤═════════╗
║ hd 1    │   hd 2  │    hd 3 ║
╠═════════╪═════════╪═════════╣
║ 1       │    1    │       2 ║
╟─────────┼─────────┼─────────╢
║ 2       │   100   │     200 ║
╟─────────┼─────────┼─────────╢
║ 3       │  10000  │   20000 ║
╠═════════╪═════════╪═════════╣
║ ft 1    │   ft 2  │    ft 3 ║
╚═════════╧═════════╧═════════╝

Column width and padding

Column width
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:header {:text "hd 1", :align :left }
      :body   {:align :left, :overflow :newline}
      :footer {:text "ft 1", :align :left }
      :width  5}
     {:header {:text "hd 2", :align :center }
      :body   {:align :center, :overflow :newline}
      :footer {:text "ft 2", :align :center }
      :width  9}
     {:header {:text "hd 3", :align :right }
      :body   {:align :right, :overflow :newline}
      :footer {:text "ft 3", :align :right }
      :width  13}] 
     [[1 "1"     "2"    ] 
      [2 "100"   "200"  ] 
      [3 "10000" "20000"]] 
    :double
    1))
╔═══════╤═══════════╤═══════════════╗
║ hd 1  │    hd 2   │          hd 3 ║
╠═══════╪═══════════╪═══════════════╣
║ 1     │     1     │             2 ║
╟───────┼───────────┼───────────────╢
║ 2     │    100    │           200 ║
╟───────┼───────────┼───────────────╢
║ 3     │   10000   │         20000 ║
╠═══════╪═══════════╪═══════════════╣
║ ft 1  │    ft 2   │          ft 3 ║
╚═══════╧═══════════╧═══════════════╝

Padding 1 Padding 2 Padding 4
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:header {:text "hd 1", :align :left }
      :body   {:align :left, :overflow :newline}
      :footer {:text "ft 1", :align :left }
      :width  5}
     {:header {:text "hd 2", :align :center }
      :body   {:align :center, :overflow :newline}
      :footer {:text "ft 2", :align :center }
      :width  5}
     {:header {:text "hd 3", :align :right }
      :body   {:align :right, :overflow :newline}
      :footer {:text "ft 3", :align :right }
      :width  5}] 
     [[1 "1"     "2"    ] 
      [2 "100"   "200"  ] 
      [3 "10000" "20000"]] 
    :double
    1))
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:header {:text "hd 1", :align :left }
      :body   {:align :left, :overflow :newline}
      :footer {:text "ft 1", :align :left }
      :width  5}
     {:header {:text "hd 2", :align :center }
      :body   {:align :center, :overflow :newline}
      :footer {:text "ft 2", :align :center }
      :width  5}
     {:header {:text "hd 3", :align :right }
      :body   {:align :right, :overflow :newline}
      :footer {:text "ft 3", :align :right }
      :width  5}] 
     [[1 "1"     "2"    ] 
      [2 "100"   "200"  ] 
      [3 "10000" "20000"]] 
    :double
    2))
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:header {:text "hd 1", :align :left }
      :body   {:align :left, :overflow :newline}
      :footer {:text "ft 1", :align :left }
      :width  5}
     {:header {:text "hd 2", :align :center }
      :body   {:align :center, :overflow :newline}
      :footer {:text "ft 2", :align :center }
      :width  5}
     {:header {:text "hd 3", :align :right }
      :body   {:align :right, :overflow :newline}
      :footer {:text "ft 3", :align :right }
      :width  5}] 
     [[1 "1"     "2"    ] 
      [2 "100"   "200"  ] 
      [3 "10000" "20000"]] 
    :double
    4))
╔═══════╤═══════╤═══════╗
║ hd 1  │  hd 2 │  hd 3 ║
╠═══════╪═══════╪═══════╣
║ 1     │   1   │     2 ║
╟───────┼───────┼───────╢
║ 2     │  100  │   200 ║
╟───────┼───────┼───────╢
║ 3     │ 10000 │ 20000 ║
╠═══════╪═══════╪═══════╣
║ ft 1  │  ft 2 │  ft 3 ║
╚═══════╧═══════╧═══════╝
╔═════════╤═════════╤═════════╗
║  hd 1   │   hd 2  │   hd 3  ║
╠═════════╪═════════╪═════════╣
║  1      │    1    │      2  ║
╟─────────┼─────────┼─────────╢
║  2      │   100   │    200  ║
╟─────────┼─────────┼─────────╢
║  3      │  10000  │  20000  ║
╠═════════╪═════════╪═════════╣
║  ft 1   │   ft 2  │   ft 3  ║
╚═════════╧═════════╧═════════╝
╔═════════════╤═════════════╤═════════════╗
║    hd 1     │     hd 2    │     hd 3    ║
╠═════════════╪═════════════╪═════════════╣
║    1        │      1      │        2    ║
╟─────────────┼─────────────┼─────────────╢
║    2        │     100     │      200    ║
╟─────────────┼─────────────┼─────────────╢
║    3        │    10000    │    20000    ║
╠═════════════╪═════════════╪═════════════╣
║    ft 1     │     ft 2    │     ft 3    ║
╚═════════════╧═════════════╧═════════════╝

Borders

(do
  (load-module :ascii-table)
  
  (defn print-with-border [border]
    (ascii-table/print 
       [{:header {:text "hd 1", :align :left }
         :body   {:align :left, :overflow :newline}
         :width  5}
        {:header {:text "hd 2", :align :center }
         :body   {:align :center, :overflow :newline}
         :width  5}
        {:header {:text "hd 3", :align :right }
         :body   {:align :right, :overflow :newline}
         :width  5}] 
       [[1 "1"   "2"  ] 
        [2 "10"  "20" ] 
        [3 "100" "200"]] 
       border
       1)
     (println)
     (println))
       
   (docoll print-with-border [:none                       
                              :standard                   
                              :standard-no-data           
                              :standard-no-outside        
                              :standard-no-data-no-outside
                              :standard-minimal           
                              :double                     
                              :double-no-data             
                              :bold                       
                              :bold-no-data               
                              :thin                       
                              :thin-no-data               
                              :matrix                     
                              :minimal]))
:none
 hd 1    hd 2   hd 3 
 1        1        2 
 2        10      20 
 3       100     200 
:standard
+-------+-------+-------+
| hd 1  |  hd 2 |  hd 3 |
+-------+-------+-------+
| 1     |   1   |     2 |
+-------+-------+-------+
| 2     |   10  |    20 |
+-------+-------+-------+
| 3     |  100  |   200 |
+-------+-------+-------+
:standard-no-data
+-------+-------+-------+
| hd 1  |  hd 2 |  hd 3 |
+-------+-------+-------+
| 1     |   1   |     2 |
| 2     |   10  |    20 |
| 3     |  100  |   200 |
+-------+-------+-------+
:standard-no-outside
 hd 1  |  hd 2 |  hd 3 
-------+-------+-------
 1     |   1   |     2 
-------+-------+-------
 2     |   10  |    20 
-------+-------+-------
 3     |  100  |   200 
:standard-no-data-no-outside
 hd 1  |  hd 2 |  hd 3 
-------+-------+-------
 1     |   1   |     2 
 2     |   10  |    20 
 3     |  100  |   200 
:standard-minimal
---------------------
 hd 1    hd 2   hd 3 
---------------------
 1        1        2 
 2        10      20 
 3       100     200 
---------------------
:double
╔═══════╤═══════╤═══════╗
║ hd 1  │  hd 2 │  hd 3 ║
╠═══════╪═══════╪═══════╣
║ 1     │   1   │     2 ║
╟───────┼───────┼───────╢
║ 2     │   10  │    20 ║
╟───────┼───────┼───────╢
║ 3     │  100  │   200 ║
╚═══════╧═══════╧═══════╝
:double-no-data
╔═══════╤═══════╤═══════╗
║ hd 1  │  hd 2 │  hd 3 ║
╠═══════╪═══════╪═══════╣
║ 1     │   1   │     2 ║
║ 2     │   10  │    20 ║
║ 3     │  100  │   200 ║
╚═══════╧═══════╧═══════╝
:bold
┏━━━━━━━┯━━━━━━━┯━━━━━━━┓
┃ hd 1  │  hd 2 │  hd 3 ┃
┣━━━━━━━┿━━━━━━━┿━━━━━━━┫
┃ 1     │   1   │     2 ┃
┠───────┼───────┼───────┨
┃ 2     │   10  │    20 ┃
┠───────┼───────┼───────┨
┃ 3     │  100  │   200 ┃
┗━━━━━━━┷━━━━━━━┷━━━━━━━┛
:bold-no-data
┏━━━━━━━┯━━━━━━━┯━━━━━━━┓
┃ hd 1  │  hd 2 │  hd 3 ┃
┣━━━━━━━┿━━━━━━━┿━━━━━━━┫
┃ 1     │   1   │     2 ┃
┃ 2     │   10  │    20 ┃
┃ 3     │  100  │   200 ┃
┗━━━━━━━┷━━━━━━━┷━━━━━━━┛
:thin
┌───────┬───────┬───────┐
│ hd 1  │  hd 2 │  hd 3 │
├───────┼───────┼───────┤
│ 1     │   1   │     2 │
├───────┼───────┼───────┤
│ 2     │   10  │    20 │
├───────┼───────┼───────┤
│ 3     │  100  │   200 │
└───────┴───────┴───────┘
:thin-no-data
┌───────┬───────┬───────┐
│ hd 1  │  hd 2 │  hd 3 │
├───────┼───────┼───────┤
│ 1     │   1   │     2 │
│ 2     │   10  │    20 │
│ 3     │  100  │   200 │
└───────┴───────┴───────┘
:thin-round
╭───────┬───────┬───────╮
│ hd 1  │  hd 2 │  hd 3 │
├───────┼───────┼───────┤
│ 1     │   1   │     2 │
├───────┼───────┼───────┤
│ 2     │   10  │    20 │
├───────┼───────┼───────┤
│ 3     │  100  │   200 │
╰───────┴───────┴───────╯
:thin-round-no-data
╭───────┬───────┬───────╮
│ hd 1  │  hd 2 │  hd 3 │
├───────┼───────┼───────┤
│ 1     │   1   │     2 │
│ 2     │   10  │    20 │
│ 3     │  100  │   200 │
╰───────┴───────┴───────╯
:matrix
│     1       1       2 │
│     2      10      20 │
│     3     100     200 │
:minimal
─────────────────────
 hd 1    hd 2   hd 3 
─────────────────────
 1        1        2 
 2        10      20 
 3       100     200 
─────────────────────

Cell overflow

(do
  (load-module :ascii-table)
  (ascii-table/print 
     [{:header {:text ":newline"}
       :body   {:align :left, :overflow :newline}
       :width  20}
      {:header {:text ":clip-right"}
       :body   {:align :center, :overflow :clip-right}
       :width  20}
      {:header {:text ":clip-left"}
       :body   {:align :center, :overflow :clip-left}
       :width  20}
      {:header {:text ":ellipsis-right"}
       :body   {:align :center, :overflow :ellipsis-right}
       :width  20}
      {:header {:text ":ellipsis-left"}
       :body   {:align :center, :overflow :ellipsis-left}
       :width  20}] 
     [[(str/lorem-ipsum :chars 60)
       (str/lorem-ipsum :chars 60)
       (str/lorem-ipsum :chars 60) 
       (str/lorem-ipsum :chars 60)
       (str/lorem-ipsum :chars 60)]] 
     :double
     1))
╔══════════════════════╤══════════════════════╤══════════════════════╤══════════════════════╤══════════════════════╗
║ :newline             │ :clip-right          │ :clip-left           │ :ellipsis-right      │ :ellipsis-left       ║
╠══════════════════════╪══════════════════════╪══════════════════════╪══════════════════════╪══════════════════════╣
║ Lorem ipsum dolor    │ Lorem ipsum dolor si │ adipiscing elit. Pra │ Lorem ipsum dolor s… │ …dipiscing elit. Pra ║
║ sit amet, consectetu │                      │                      │                      │                      ║
║ r adipiscing elit.   │                      │                      │                      │                      ║
║ Pra                  │                      │                      │                      │                      ║
╚══════════════════════╧══════════════════════╧══════════════════════╧══════════════════════╧══════════════════════╝

Multi-column text

Border Without Border
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:body  {:align :left, :overflow :newline}
      :width 25}
     {:body  {:align :left, :overflow :newline}
      :width 25}] 
     [[(str/lorem-ipsum :chars 150) 
       (str/lorem-ipsum :chars 120)]] 
     :thin
     1))
(do
  (load-module :ascii-table)
  (ascii-table/print 
    [{:body  {:align :left, :overflow :newline}
      :width 25}
     {:body  {:align :left, :overflow :newline}
      :width 25}] 
     [[(str/lorem-ipsum :chars 150) 
       (str/lorem-ipsum :chars 120)]] 
     :none
     1))
┌───────────────────────────┬───────────────────────────┐
│ Lorem ipsum dolor sit     │ Lorem ipsum dolor sit     │
│ amet, consectetur         │ amet, consectetur         │
│ adipiscing elit. Praesent │ adipiscing elit. Praesent │
│ ac iaculis turpis. Duis   │ ac iaculis turpis. Duis   │
│ dictum id sem et          │ dictum id sem et          │
│ consectetur. Nullam       │ consectetur.              │
│ lobortis, libero non co   │                           │
└───────────────────────────┴───────────────────────────┘
 Lorem ipsum dolor sit      Lorem ipsum dolor sit     
 amet, consectetur          amet, consectetur         
 adipiscing elit. Praesent  adipiscing elit. Praesent 
 ac iaculis turpis. Duis    ac iaculis turpis. Duis   
 dictum id sem et           dictum id sem et          
 consectetur. Nullam        consectetur.              
 lobortis, libero non co