Usage
The following functions can be used to print data.
function pretty_table([io::IO,] data::AbstractVecOrMat{T1}, header::AbstractVecOrMat{T2}, tf::PrettyTableFormat = unicode; kwargs...) where {T1,T2}
Print to io
the vector or matrix data
with header header
using the format tf
(see Formats). If io
is omitted, then it defaults to stdout
. If header
is empty, then it will be automatically filled with "Col. i" for the i-th column.
The header
can be a Vector
or a Matrix
. If it is a Matrix
, then each row will be a header line. The first line is called header and the others are called sub-headers .
julia> data = [1 2 3; 4 5 6];
julia> pretty_table(data, ["Column 1", "Column 2", "Column 3"])
┌──────────┬──────────┬──────────┐
│ Column 1 │ Column 2 │ Column 3 │
├──────────┼──────────┼──────────┤
│ 1 │ 2 │ 3 │
│ 4 │ 5 │ 6 │
└──────────┴──────────┴──────────┘
julia> pretty_table(data, ["Column 1" "Column 2" "Column 3"; "A" "B" "C"])
┌──────────┬──────────┬──────────┐
│ Column 1 │ Column 2 │ Column 3 │
│ A │ B │ C │
├──────────┼──────────┼──────────┤
│ 1 │ 2 │ 3 │
│ 4 │ 5 │ 6 │
└──────────┴──────────┴──────────┘
function pretty_table([io,] data::AbstractVecOrMat{T}, tf::PrettyTableFormat = unicode; ...) where T
Print to io
the vector or matrix data
using the format tf
(see PrettyTableFormat
). If io
is omitted, then it defaults to stdout
. The header will be automatically filled with "Col. i" for the i-th column.
julia> data = Any[1 2 3; true false true];
julia> pretty_table(data)
┌────────┬────────┬────────┐
│ Col. 1 │ Col. 2 │ Col. 3 │
├────────┼────────┼────────┤
│ 1 │ 2 │ 3 │
│ true │ false │ true │
└────────┴────────┴────────┘
If data
is a vector, then the header
must be a vector. In this case, the first element is considered the header and the others are the sub-headers.
function pretty_table([io::IO,] dict::Dict{K,V}, tf::PrettyTableFormat = unicode; sortkeys = true, ...) where {K,V}
Print to io
the dictionary dict
in a matrix form (one column for the keys and other for the values), using the format tf
(see PrettyTableFormat
). If io
is omitted, then it defaults to stdout
.
In this case, the keyword sortkeys
can be used to select whether or not the user wants to print the dictionary with the keys sorted. If it is false
, then the elements will be printed on the same order returned by the functions keys
and values
. Notice that this assumes that the keys are sortable, if they are not, then an error will be thrown.
julia> dict = Dict(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun");
julia> pretty_table(dict)
┌───────┬────────┐
│ Keys │ Values │
│ Int64 │ String │
├───────┼────────┤
│ 4 │ Apr │
│ 2 │ Feb │
│ 3 │ Mar │
│ 5 │ May │
│ 6 │ Jun │
│ 1 │ Jan │
└───────┴────────┘
julia> pretty_table(dict, sortkeys = true)
┌───────┬────────┐
│ Keys │ Values │
│ Int64 │ String │
├───────┼────────┤
│ 1 │ Jan │
│ 2 │ Feb │
│ 3 │ Mar │
│ 4 │ Apr │
│ 5 │ May │
│ 6 │ Jun │
└───────┴────────┘
function pretty_table([io,] table, tf::PrettyTableFormat = unicode; ...)
Print to io
the table table
using the format tf
(see Formats). In this case, table
must comply with the API of Tables.jl. If io
is omitted, then it defaults to stdout
.
In all cases, the following keywords are available:
border_crayon
: Crayon to print the border.header_crayon
: Crayon to print the header.subheaders_crayon
: Crayon to print sub-headers.rownum_header_crayon
: Crayon for the header of the column with the row numbers.text_crayon
: Crayon to print default text.alignment
: Select the alignment of the columns (see the section Alignment).crop
: Select the printing behavior when the data is bigger than the available screen size (seescreen_size
). It can be:both
to crop on vertical and horizontal direction,:horizontal
to crop only on horizontal direction,:vertical
to crop only on vertical direction, or:none
to do not crop the data at all.filters_row
: Filters for the rows (see the section Filters).filters_col
: Filters for the columns (see the section Filters).formatter
: See the section Formatter.highlighters
: A tuple with a list of highlighters (see the section Highlighters).hlines
: A vector ofInt
indicating row numbers in which an additional horizontal line should be drawn after the row. Notice that numbers lower than 1 and equal or higher than the number of rows will be neglected.linebreaks
: Iftrue
, then\n
will break the line inside the cells. (Default =false
)noheader
: Iftrue
, then the header will not be printed. Notice that all keywords and parameters related to the header and sub-headers will be ignored. (Default =false
)same_column_size
: Iftrue
, then all the columns will have the same size. (Default =false
)screen_size
: A tuple of two integers that defines the screen size (num. of rows, num. of columns) that is available to print the table. It is used to crop the data depending on the value of the keywordcrop
. If it isnothing
, then the size will be obtained automatically. Notice that if a dimension is not positive, then it will be treated as unlimited. (Default =nothing
)show_row_number
: Iftrue
, then a new column will be printed showing the row number. (Default =false
)
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.
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.
In vertical cropping, the header and the first table row is always printed.
The highlighters will work even in partially printed data.