Skip to content

Commit

Permalink
✨ Add option autowrap
Browse files Browse the repository at this point in the history
This option can be used to wrap text on spaces when line breaks are
permitted and the column size is fixed.

Closes #21
  • Loading branch information
ronisbr committed Nov 8, 2019
1 parent 4a8f9f0 commit 362673f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ omitted, then it defaults to `stdout`.
numbers.
* `text_crayon`: Crayon to print default text.
* `alignment`: Select the alignment of the columns (see the section `Alignment`).
* `autowrap`: If `true`, then the text will be wrapped on spaces to fit the
column. Notice that this function requires `linebreaks = true` and
the column must have a fixed size (see `columns_width`).
* `cell_alignment`: A dictionary of type `(i,j) => a` that overrides that
alignment of the cell `(i,j)` to `a` regardless of the
columns alignment selected. `a` must be a symbol like
Expand Down Expand Up @@ -310,6 +313,7 @@ function _pretty_table(io, data, header, tf::PrettyTableFormat = unicode;
rownum_header_crayon::Crayon = Crayon(bold = true),
text_crayon::Crayon = Crayon(),
alignment::Union{Symbol,Vector{Symbol}} = :r,
autowrap::Bool = false,
cell_alignment::Dict{Tuple{Int,Int},Symbol} = Dict{Tuple{Int,Int},Symbol}(),
crop::Symbol = :both,
columns_width::Union{Integer,AbstractVector{Int}} = 0,
Expand Down Expand Up @@ -564,8 +568,9 @@ function _pretty_table(io, data, header, tf::PrettyTableFormat = unicode;
end

if linebreaks
# Get the tokens for each line.
tokens = escape_string.(split(data_str_ij, '\n'))
tokens = _str_line_breaks(data_str_ij,
autowrap && fixed_col_width[ic],
columns_width[i])
data_str[j,i] = tokens
num_lines_ij = length(tokens)

Expand Down
60 changes: 60 additions & 0 deletions src/private.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,66 @@ function _str_aligned(data::AbstractString, alignment::Symbol,
end
end

"""
function _str_line_breaks(str::AbstractString, autowrap::Bool = false, width::Int = 0)
Split the string `str` into substring, each one meaning one new line. If
`autowrap` is `true`, then the text will be wrapped so that it fits the column
with the width `width`.
"""
function _str_line_breaks(str::AbstractString, autowrap::Bool = false, width::Int = 0)
# Check for errors.
autowrap && (width <= 0) &&
error("If `autowrap` is true, then the width must not be positive.")

# Get the tokens for each line.
tokens_raw = escape_string.(split(str, '\n'))

# If the user wants to auto wrap the text, then we must check if
# the tokens must be modified.
if autowrap
tokens = String[]

for token in tokens_raw
sub_tokens = String[]
length_tok = length(token)

if length_tok > width
# First, let's analyze from the beginning of the token up to the
# field width.
k₀ = 1
k₁ = k₀ + width - 1

while k₀ <= length_tok
# If we have a space, then we crop in this space.
aux = findlast(" ", token[k₀:k₁])

if aux == nothing
push!(sub_tokens, token[k₀:k₁])
else
k₁ = k₀ + aux[1] - 1

# Here, we subtract 1 because we want to remove the
# space.
push!(sub_tokens, token[k₀:k₁-1])
end

k₀ = k₁+1
k₁ = clamp(k₀ + width - 1, 0, length_tok)
end
push!(tokens, sub_tokens...)
else
push!(tokens, token)
end
end

return tokens
else
return tokens_raw
end
end

################################################################################
# Printing Functions
################################################################################
Expand Down

0 comments on commit 362673f

Please sign in to comment.