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 set of highlighters can be passed as a Tuple to the highlighters keyword. Each highlighter is an instance of the structure Highlighter that contains three fields:

The function f has the following signature:

f(data, i, j)

in which data is a reference to the data that is being printed, and i and j are the element coordinates that are being tested. If this function returns true, then the cell (i,j) will be highlighted.

If the function f returns true, then the function fd(h,data,i,j) will be called and must return a Crayon that will be applied to the cell.

A highlighter can be constructed using three helpers:

Highlighter(f::Function; kwargs...)

where it will construct a Crayon using the keywords in kwargs and apply it to the highlighted cell,

Highlighter(f::Function, crayon::Crayon)

where it will apply the crayon to the highlighted cell, and

Highlighter(f::Function, fd::Function)

where it will apply the Crayon returned by the function fd to the highlighted cell.

Info

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

Note

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.

Note

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

julia> h1 = Highlighter( f      = (data,i,j) -> (data[i,j] < 0.5),
                         crayon = crayon"red bold" )

julia> h2 = Highlighter( (data,i,j) -> (data[i,j] > 0.5),
                         bold       = true,
                         foreground = :blue )

julia> h3 = Highlighter( f      = (data,i,j) -> (data[i,j] == 0.5),
                         crayon = Crayon(bold = true, foreground = :yellow) )

julia> pretty_table(data, highlighters = (h1, h2, h3))

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

julia> hl_odd = Highlighter( f      = (data,i,j) -> i % 2 == 0,
                             crayon = Crayon(background = :light_blue))

julia> pretty_table(data, highlighters = hl_odd, formatters = ft_printf("%10.5f"))

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.

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 |

matrix

┌                     ┐
│ 1   false   1.0   1 │
│ 2    true   2.0   2 │
│ 3   false   3.0   3 │
└                     ┘
Info

In this case, the table format matrix was printed with the option noheader = true.

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.