Skip to content

Commit

Permalink
🔧 Allow columns_width to be an integer
Browse files Browse the repository at this point in the history
If `columns_width` is an integer, then it will be the size of all
columns.

This addresses part of issue #21.
  • Loading branch information
ronisbr committed Oct 28, 2019
1 parent 930ffff commit 64ec502
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/src/man/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ In all cases, the following keywords are available:
* `columns_width`: A set of integers specifying the width of each column. If the
width is equal or lower than 0, then it will be automatically
computed to fit the large cell in the column. If it is
`nothing`, then all the columns will have their size
automatically computed. (**Default** = `nothing`)
a single integer, then this number will be used as the size
of all columns. (**Default** = 0)
* `crop`: Select the printing behavior when the data is bigger than the
available screen size (see `screen_size`). It can be `:both` to crop
on vertical and horizontal direction, `:horizontal` to crop only on
Expand Down
8 changes: 4 additions & 4 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ omitted, then it defaults to `stdout`.
* `columns_width`: A set of integers specifying the width of each column. If the
width is equal or lower than 0, then it will be automatically
computed to fit the large cell in the column. If it is
`nothing`, then all the columns will have their size
automatically computed. (**Default** = `nothing`)
a single integer, then this number will be used as the size
of all columns. (**Default** = 0)
* `crop`: Select the printing behavior when the data is bigger than the
available screen size (see `screen_size`). It can be `:both` to crop
on vertical and horizontal direction, `:horizontal` to crop only on
Expand Down Expand Up @@ -338,7 +338,7 @@ function _pretty_table(io, data, header, tf::PrettyTableFormat = unicode;
alignment::Union{Symbol,Vector{Symbol}} = :r,
cell_alignment::Dict{Tuple{Int,Int},Symbol} = Dict{Tuple{Int,Int},Symbol}(),
crop::Symbol = :both,
columns_width::Union{Nothing,AbstractVector{Int}} = nothing,
columns_width::Union{Integer,AbstractVector{Int}} = 0,
filters_row::Union{Nothing,Tuple} = nothing,
filters_col::Union{Nothing,Tuple} = nothing,
formatter::Dict = Dict(),
Expand Down Expand Up @@ -531,7 +531,7 @@ function _pretty_table(io, data, header, tf::PrettyTableFormat = unicode;
num_lines_in_row = ones(Int, num_printed_rows)

# Check which columns must have fixed sizes.
columns_width == nothing && (columns_width = zeros(Int, num_cols))
typeof(columns_width) <: Integer && (columns_width = ones(Int, num_cols)*columns_width)
length(columns_width) != num_cols && error("The length of `columns_width` must be the same as the number of columns.")
fixed_col_width = map(w->w > 0, columns_width)

Expand Down
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,26 @@ end
alignment = [:l, :l, :r, :c, :r, :c, :l, :l, :c, :r]),
model)

expected = """
┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐
│ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │ Col. 5 │ Col. 6 │ Col. 7 │ Col. 8 │ Col. 9 │ Col. … │
├────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
│ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │ 1.123… │
└────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘
"""

result = sprint((io,data)->pretty_table(io, data,
columns_width = 6,
alignment = [:l, :l, :r, :c, :r, :c, :l, :l, :c, :r]),
model)

@test result == expected
end

Expand Down

0 comments on commit 64ec502

Please sign in to comment.