/
prettytables.jl
92 lines (80 loc) · 3.24 KB
/
prettytables.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
##############################################################################
##
## Functions related to the interface with PrettyTables.jl.
##
##############################################################################
# Default DataFrames highlighter for text backend.
#
# This highlighter changes the text color to gray in cells with `nothing`,
# `missing`, `#undef`, and types related to DataFrames.jl.
function _pretty_tables_highlighter_func(data, i::Integer, j::Integer)
try
cell = data[i, j]
return ismissing(cell) ||
cell === nothing ||
cell isa Union{AbstractDataFrame, GroupedDataFrame,
DataFrameRow, DataFrameRows,
DataFrameColumns}
catch e
if isa(e, UndefRefError)
return true
else
rethrow(e)
end
end
end
const _PRETTY_TABLES_HIGHLIGHTER = Highlighter(_pretty_tables_highlighter_func,
Crayon(foreground = :dark_gray))
# Default DataFrames formatter for text backend.
#
# This formatter changes how the following types are presented when rendering
# the data frame:
# - missing;
# - nothing;
# - Cells with types related to DataFrames.jl.
function _pretty_tables_general_formatter(v, i::Integer, j::Integer)
if typeof(v) <: Union{AbstractDataFrame, GroupedDataFrame, DataFrameRow,
DataFrameRows, DataFrameColumns}
# Here, we must not use `print` or `show`. Otherwise, we will call
# `_pretty_table` to render the current table leading to a stack
# overflow.
return sprint(summary, v)
elseif ismissing(v)
return "missing"
elseif v === nothing
return ""
else
return v
end
end
# Formatter to align the floating points as in Julia array printing.
#
# - `float_cols` contains the IDs of the columns that must be formatted.
# - `indices` is a vector of vectors containing the indices of each elements
# in the data frame.
# - `padding` is a vector of vectors containing the padding of each element for
# each row.
# - `compact_printing` must be a boolean indicating if we should enable the
# `:compact` option of `io` when converting the number to string.
function _pretty_tables_float_formatter(v, i::Integer, j::Integer,
float_cols::Vector{Int},
indices::Vector{Vector{Int}},
padding::Vector{Vector{Int}},
compact_printing::Bool)
isempty(float_cols) && return v
# We apply this formatting only to the columns that contains only floats.
ind_col = findfirst(==(j), float_cols)
if ind_col !== nothing
ind_row = findfirst(==(i), indices[ind_col])
if ind_row !== nothing
pad = padding[ind_col][ind_row]
# Return the formatted number.
str = sprint(print, v, context = :compact => compact_printing)
return " "^pad * str
end
end
# The formatter is applied to all tables' cells. Hence, we must return the
# input value `v` unchanged if this cell is not part of a column that has
# floating point numbers.
return v
end