Text

Text back-end

The following options are available when the text backend is used. Those can be passed as keywords when calling the function pretty_table:

The keywords header_crayon and subheaders_crayon can be a Crayon or a Vector{Crayon}. In the first case, the Crayon will be applied to all the elements. In the second, each element can have its own crayon, but the length of the vector must be equal to the number of columns in the data.

Crayons

A Crayon is an object that handles a style for text printed on terminals. It is defined in the package Crayons.jl. There are many options available to customize the style, such as foreground color, background color, bold text, etc.

A Crayon can be created in two different ways:

julia> Crayon(foreground = :blue, background = :black, bold = :true)

julia> crayon"blue bg:black bold"

For more information, see the Crayon.jl documentation.

Info

The Crayon.jl package is re-exported by PrettyTables.jl. Hence, you do not need using Crayons to create a Crayon.

Cropping

By default, the data will be cropped to fit the screen. This behavior can be changed by using the keyword crop.

julia> data = Any[1    false      1.0     0x01 ;
                  2     true      2.0     0x02 ;
                  3    false      3.0     0x03 ;
                  4     true      4.0     0x04 ;
                  5    false      5.0     0x05 ;
                  6     true      6.0     0x06 ;];

julia> pretty_table(data, screen_size = (10,30))
┌────────┬────────┬────────┬ ⋯
│ Col. 1 │ Col. 2 │ Col. 3 │ ⋯
├────────┼────────┼────────┼ ⋯
│      1 │  false │    1.0 │ ⋯
│      2 │   true │    2.0 │ ⋯
│   ⋮    │   ⋮    │   ⋮    │ ⋯
└────────┴────────┴────────┴ ⋯

julia> pretty_table(data, screen_size = (10,30), crop = :none)
┌────────┬────────┬────────┬────────┐
│ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │
├────────┼────────┼────────┼────────┤
│      1 │  false │    1.0 │      1 │
│      2 │   true │    2.0 │      2 │
│      3 │  false │    3.0 │      3 │
│      4 │   true │    4.0 │      4 │
│      5 │  false │    5.0 │      5 │
│      6 │   true │    6.0 │      6 │
└────────┴────────┴────────┴────────┘

If the keyword screen_size is not specified (or is nothing), then the screen size will be obtained automatically. For files, screen_size = (-1,-1), meaning that no limit exits in both vertical and horizontal direction.

Note

In vertical cropping, the header and the first table row is always printed.

Note

The highlighters will work even in partially printed data.

If the user selects a fixed size for the columns (using the keyword columns_width), enables line breaks (using the keyword linebreaks), and sets autowrap = true, then the algorithm wraps the text on spaces to automatically fit the space.

julia> data = ["One very very very big long long line"; "Another very very very big big long long line"];

julia> pretty_table(data, columns_width = 10, autowrap = true, linebreaks = true, show_row_number = true)
┌─────┬────────────┐
│ Row │     Col. 1 │
├─────┼────────────┤
│   1 │   One very │
│     │  very very │
│     │   big long │
│     │  long line │
│   2 │    Another │
│     │  very very │
│     │   very big │
│     │   big long │
│     │  long line │
└─────┴────────────┘

Text highlighters

A highlighter of the text backend is an instance of the structure Highlighter that contains information about which elements a highlight style should be applied when using the text backend. The structure contains three fields:

The function f must have the following signature:

f(data, i, j)

in which data is a reference to the data that is being printed, i and j are the element coordinates that are being tested. If this function returns true, then the highlight style will be applied to the (i,j) element. Otherwise, the default style will be used.

A set of highlighters can be passed as a Tuple to the highlighter keyword. Notice that if multiple highlighters are valid for the element (i,j), then the applied style will be equal to the first match considering the order in the Tuple highlighters.

If only a single highlighter is wanted, then it can be passed directly to the keyword highlighter of pretty_table without being inside a Tuple.

Note

If the highlighters are used together with Formatter, then the change in the format will not affect that parameter data passed to the highlighter function f. It will always receive the original, unformatted value.

There are a set of pre-defined highlighters (with names hl_*) to make the usage simpler. They are defined in the file ./src/backends/text/predefined_highlighters.jl.

To make the syntax less cumbersome, the following helper function is available:

function Highlighter(f; kwargs...)

It creates a Highlighter with the function f and pass all the keyword arguments kwargs to the Crayon. Hence, the following code:

julia> Highlighter((data,i,j)->isodd(i), Crayon(bold = true, background = :dark_gray))

can be replaced by:

julia> Highlighter((data,i,j)->isodd(i); bold = true, background = :dark_gray)

Text table formats

The following table formats are available when using the text back-end:

unicode (Default)

┌────────┬────────┬────────┬────────┐
│ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │
├────────┼────────┼────────┼────────┤
│      1 │  false │    1.0 │      1 │
│      2 │   true │    2.0 │      2 │
│      3 │  false │    3.0 │      3 │
└────────┴────────┴────────┴────────┘

ascii_dots

.....................................
: Col. 1 : Col. 2 : Col. 3 : Col. 4 :
:........:........:........:........:
:      1 :  false :    1.0 :      1 :
:      2 :   true :    2.0 :      2 :
:      3 :  false :    3.0 :      3 :
:........:........:........:........:

ascii_rounded

.--------.--------.--------.--------.
| Col. 1 | Col. 2 | Col. 3 | Col. 4 |
:--------+--------+--------+--------:
|      1 |  false |    1.0 |      1 |
|      2 |   true |    2.0 |      2 |
|      3 |  false |    3.0 |      3 |
'--------'--------'--------'--------'

borderless

  Col. 1   Col. 2   Col. 3   Col. 4

       1    false      1.0        1
       2     true      2.0        2
       3    false      3.0        3

compact

 -------- -------- -------- --------
  Col. 1   Col. 2   Col. 3   Col. 4
 -------- -------- -------- --------
       1    false      1.0        1
       2     true      2.0        2
       3    false      3.0        3
 -------- -------- -------- --------

markdown

| Col. 1 | Col. 2 | Col. 3 | Col. 4 |
|--------|--------|--------|--------|
|      1 |  false |    1.0 |      1 |
|      2 |   true |    2.0 |      2 |
|      3 |  false |    3.0 |      3 |

mysql

+--------+--------+--------+--------+
| Col. 1 | Col. 2 | Col. 3 | Col. 4 |
+--------+--------+--------+--------+
|      1 |  false |    1.0 |      1 |
|      2 |   true |    2.0 |      2 |
|      3 |  false |    3.0 |      3 |
+--------+--------+--------+--------+

simple

========= ======== ======== =========
  Col. 1   Col. 2   Col. 3   Col. 4
========= ======== ======== =========
       1    false      1.0        1
       2     true      2.0        2
       3    false      3.0        3
========= ======== ======== =========

unicode_rounded

╭────────┬────────┬────────┬────────╮
│ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │
├────────┼────────┼────────┼────────┤
│      1 │  false │    1.0 │      1 │
│      2 │   true │    2.0 │      2 │
│      3 │  false │    3.0 │      3 │
╰────────┴────────┴────────┴────────╯
Note

The format unicode_rounded should look awful on your browser, but it should be printed fine on your terminal.

julia> data = Any[ f(a) for a = 0:15:90, f in (sind,cosd,tand)];

julia> pretty_table(data, tf = ascii_dots)
..................................................................
:              Col. 1 :              Col. 2 :             Col. 3 :
:.....................:.....................:....................:
:                 0.0 :                 1.0 :                0.0 :
: 0.25881904510252074 :  0.9659258262890683 : 0.2679491924311227 :
:                 0.5 :  0.8660254037844386 : 0.5773502691896258 :
:  0.7071067811865476 :  0.7071067811865476 :                1.0 :
:  0.8660254037844386 :                 0.5 : 1.7320508075688772 :
:  0.9659258262890683 : 0.25881904510252074 : 3.7320508075688776 :
:                 1.0 :                 0.0 :                Inf :
:.....................:.....................:....................:

julia> pretty_table(data, tf = compact)
 --------------------- --------------------- --------------------
               Col. 1                Col. 2               Col. 3
 --------------------- --------------------- --------------------
                  0.0                   1.0                  0.0
  0.25881904510252074    0.9659258262890683   0.2679491924311227
                  0.5    0.8660254037844386   0.5773502691896258
   0.7071067811865476    0.7071067811865476                  1.0
   0.8660254037844386                   0.5   1.7320508075688772
   0.9659258262890683   0.25881904510252074   3.7320508075688776
                  1.0                   0.0                  Inf
 --------------------- --------------------- --------------------

It is also possible to define you own custom table by creating a new instance of the structure TextFormat. For example, let's say that you want a table like simple that does not print the bottom line:

julia> data = Any[ f(a) for a = 0:15:90, f in (sind,cosd,tand)];

julia> tf = TextFormat(simple, bottom_line = false);

julia> pretty_table(data, tf = tf)
====================== ===================== =====================
               Col. 1                Col. 2               Col. 3
====================== ===================== =====================
                  0.0                   1.0                  0.0
  0.25881904510252074    0.9659258262890683   0.2679491924311227
                  0.5    0.8660254037844386   0.5773502691896258
   0.7071067811865476    0.7071067811865476                  1.0
   0.8660254037844386                   0.5   1.7320508075688772
   0.9659258262890683   0.25881904510252074   3.7320508075688776
                  1.0                   0.0                  Inf

or that does not print the header line:

julia> data = Any[ f(a) for a = 0:15:90, f in (sind,cosd,tand)];

julia> tf = TextFormat(simple, header_line = false);

julia> pretty_table(data, tf = tf)
====================== ===================== =====================
               Col. 1                Col. 2               Col. 3
                  0.0                   1.0                  0.0
  0.25881904510252074    0.9659258262890683   0.2679491924311227
                  0.5    0.8660254037844386   0.5773502691896258
   0.7071067811865476    0.7071067811865476                  1.0
   0.8660254037844386                   0.5   1.7320508075688772
   0.9659258262890683   0.25881904510252074   3.7320508075688776
                  1.0                   0.0                  Inf
====================== ===================== =====================

For more information, see the documentation of the structure TextFormat.