Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ using GMT.Laszip
#arrows([0 8.2 0 6], limits=(-2,4,0,9), arrow=(len=2,stop=1,shape=0.5,fill=:red), axis=:a, pen="6p");
theme()
#rescale(mat2img(rand(UInt16, 16,16,3)))
plot(rand(5,2))
GMT.finish_PS_nested(Dict{Symbol, Any}(), ["psbasemap -Rd -JX15c/0 -Baf -BWSen"])
plot(rand(5,2))
resetGMT()
end

Expand Down
90 changes: 63 additions & 27 deletions src/PrettyTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -829,15 +829,8 @@ function _print_table_with_text_back_end(

# -- Column Alignment Regex ------------------------------------------------------------

_apply_alignment_anchor_regex!(ptable, table_str, actual_columns_width,
# Configurations.
alignment_anchor_fallback,
alignment_anchor_fallback_override,
alignment_anchor_regex,
columns_width,
maximum_columns_width,
minimum_columns_width
)
_apply_alignment_anchor_regex!(ptable, table_str, actual_columns_width, alignment_anchor_fallback,
alignment_anchor_fallback_override, alignment_anchor_regex, columns_width, maximum_columns_width, minimum_columns_width)

# If the user wants all the columns with the same size, select the larger.
if equal_columns_width
Expand All @@ -863,8 +856,7 @@ function _print_table_with_text_back_end(
# -- Process the Title -----------------------------------------------------------------

title_tokens = _tokenize_title(title, display.size[2], table_width,
title_alignment, title_autowrap, title_same_width_as_table
)
title_alignment, title_autowrap, title_same_width_as_table)

# == Print the Table ===================================================================

Expand Down Expand Up @@ -1223,6 +1215,61 @@ function _print_title!(display::Display, title_tokens::Vector{String})
return nothing
end

function _str_autowrap(tokens_raw::Vector{String}, width::Int = 0)
width <= 0 && error("If `autowrap` is true, then the width must not be positive.")
tokens = String[]

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

# Get the list of valid indices to handle UTF-8 strings. In this case, the n-th
# character of the string can be accessed by `token[tok_ids[n]]`.
tok_ids = collect(eachindex(token))

if length_tok > width
# First, let's analyze from the beginning of the token up to the field width.
#
# k₀ is the character that will start the sub-token.
# k₁ is the character that will end the sub-token.
k₀ = 1
k₁ = k₀ + width - 1

while k₀ <= length_tok
# Check if the remaining string fit in the available space.
if k₁ == length_tok
push!(sub_tokens, token[tok_ids[k₀:k₁]])

else
# If the remaining string does not fit into the available space, then we
# search for spaces to crop.
Δ = 0
for k = k₁:-1:k₀
if token[tok_ids[k]] == ' '
# If a space is found, then select `k₁` as this character and
# use `Δ` to remove it when printing, so that we hide the space.
k₁ = k
Δ = 1
break
end
end

push!(sub_tokens, token[tok_ids[k₀:k₁-Δ]])
end

# Move to the next analysis window.
k₀ = k₁+1
k₁ = clamp(k₀ + width - 1, 0, length_tok)
end
push!(tokens, sub_tokens...)
else
push!(tokens, token)
end
end

return tokens
end

# Split the table title into tokens considering the line break character.
function _tokenize_title(title::AbstractString, display_width::Int, table_width::Int,
# Configurations
Expand Down Expand Up @@ -2833,9 +2880,7 @@ function ProcessedTable(data::Any, header::Any;
end
end

if max_num_of_columns > 0
max_num_of_columns = min(max_num_of_columns, num_data_columns)
end
(max_num_of_columns > 0) && (max_num_of_columns = min(max_num_of_columns, num_data_columns))

return ProcessedTable(
data = data,
Expand All @@ -2860,15 +2905,11 @@ function _add_column!(ptable::ProcessedTable, new_column::AbstractVector, new_he
# data.
num_rows, ~ = _data_size(ptable)

if num_rows != length(new_column)
error("The size of the new column does not match the size of the table.")
end
(num_rows != length(new_column)) && error("The size of the new column does not match the size of the table.")

# The symbol cannot be `:__ORIGINAL_DATA__` because it is used to identified
# if a column is part of the original data.
if id == :__ORIGINAL_DATA__
error("The new column identification symbol cannot be `:__ORIGINAL_DATA__`.")
end
(id == :__ORIGINAL_DATA__) && error("The new column identification symbol cannot be `:__ORIGINAL_DATA__`.")

push!(ptable._additional_column_id, id)
push!(ptable._additional_data_columns, new_column)
Expand Down Expand Up @@ -3022,9 +3063,7 @@ end

# Parse the table `cell` of type `T` and return a vector of `String` with the parsed cell
# text, one component per line.
function _text_parse_cell(
@nospecialize(io::IOContext),
cell::Any;
function _text_parse_cell(@nospecialize(io::IOContext), cell::Any;
autowrap::Bool = true,
cell_data_type::DataType = Nothing,
cell_first_line_only::Bool = false,
Expand Down Expand Up @@ -3223,10 +3262,7 @@ function _get_column_alignment(ptable::ProcessedTable, j::Int)

# The apparently unnecessary conversion to `Symbol` avoids type instability.
ptable_data_alignment = ptable._data_alignment

alignment = ptable_data_alignment isa Symbol ?
Symbol(ptable_data_alignment) :
Symbol(ptable_data_alignment[jr])
alignment = ptable_data_alignment isa Symbol ? Symbol(ptable_data_alignment) : Symbol(ptable_data_alignment[jr])

return alignment
else
Expand Down
16 changes: 8 additions & 8 deletions src/psxy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool,

arg2, arg3, arg4 = nothing, nothing, nothing
N_args::Int = (arg1 === nothing) ? 0 : 1
is_ternary = (caller == "ternary") ? true : false
is_ternary = (caller == "ternary")
if (is3D) gmt_proggy = (IamModern[1]) ? "plot3d " : "psxyz "
elseif (is_ternary) gmt_proggy = (IamModern[1]) ? "ternary " : "psternary "
else gmt_proggy = (IamModern[1]) ? "plot " : "psxy "
Expand All @@ -32,7 +32,7 @@ function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool,
cmd, isFV, caller, sub_module, gmt_proggy, opt_A, g_bar_fill, arg1 = parse_plot_callers(d, gmt_proggy, caller, is3D, O, arg1)

# --------------------- Check the grid2tri cases --------------------
cmd, is_gridtri, arg1 = parse_grid2tri_case(d, cmd, caller, is3D, isFV, O, arg1)
cmd, is_gridtri, arg1 = parse_grid2tri_case(d, cmd, caller, is3D, isFV, O, arg1) # FORCES RECOMPILE

isa(arg1, GMTdataset) && (arg1 = with_xyvar(d, arg1)) # See if we have a column request based on column names
if ((val = hlp_desnany_int(d, [:decimate])) !== -999) # Asked for a clever data decimation?
Expand Down Expand Up @@ -63,7 +63,7 @@ function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool,
(isa(arg1, GMTdataset) && size(arg1,2) > 1 && !isempty(arg1.colnames)) && (CTRL.XYlabels[1] = arg1.colnames[1]; CTRL.XYlabels[2] = arg1.colnames[2])
isa(arg1, Vector{<:GMTdataset}) && !isempty(arg1[1].colnames) && (CTRL.XYlabels[1] = arg1[1].colnames[1]; CTRL.XYlabels[2] = arg1[1].colnames[2])
if (is_ternary) cmd, opt_J = parse_J(d, cmd, default=def_J)
else cmd, opt_B, opt_J, opt_R = parse_BJR(d, cmd, caller, O, def_J)
else cmd, opt_B, opt_J, opt_R = parse_BJR(d, cmd, caller, O, def_J) # FORCES RECOMPILE
end
# Current parse_B does not add a default -Baz when 3D AND -J has a projection. More or less fix that.
if (is3D && opt_B != "" && !contains(opt_B, " -Bz"))
Expand All @@ -75,14 +75,14 @@ function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool,

axis_equal = is_axis_equal(d) # See if the user asked for an equal aspect ratio
cmd, opt_JZ = parse_JZ(d, cmd; O=O, is3D=is3D)
cmd, = parse_common_opts(d, cmd, [:a :e :f :g :t :w :margin :params]; first=first)
cmd, _ = parse_common_opts(d, cmd, [:a :e :f :g :t :w :margin :params]; first=first) # FORCES RECOMPILE
cmd, opt_l = parse_l(d, cmd) # Parse this one (legend) aside so we can use it in classic mode
cmd, opt_f = parse_f(d, cmd) # Parse this one (-f) aside so we can check against D.attrib
cmd = parse_these_opts(cmd, d, [[:D :shift :offset], [:I :intens], [:N :no_clip :noclip], [:T]])
parse_ls_code!(d::Dict) # Check for linestyle codes (must be before the GMTsyntax_opt() call)
cmd = GMTsyntax_opt(d, cmd)[1] # See if an hardcore GMT syntax string has been passed by mk_styled_line!
(is_ternary) && (cmd = add_opt(d, cmd, "M", [:M :dump]))
opt_UVXY = parse_UVXY(d, "") # Need it separate to not risk to double include it.
opt_UVXY = parse_UVXY(d, "") # FORCES RECOMPILE # Need it separate to not risk to double include it.
cmd, opt_c = parse_c(d, cmd) # Need opt_c because we may need to remove it from double calls

if (isa(arg1, GDtype) && !contains(opt_f, "T") && !contains(opt_f, "t") && !contains(opt_R, "T") && !contains(opt_R, "t"))
Expand Down Expand Up @@ -632,9 +632,9 @@ function with_xyvar(d::Dict, arg1::GMTdataset, no_x::Bool=false)::Union{GMTdatas

function getcolvar(d::Dict, var::VMs)::Int
((_val = find_in_dict(d::Dict, var)[1]) === nothing) && return 0
!(isa(_val, Integer) || isa(_val, String) || isa(_val, Symbol)) && error("$(var) can only be an Int, a String or a Symbol but was a $(typeof(_val))")
c = isa(_val, Integer) ? _val : ((_ind = findfirst(string(_val)::String .== arg1.colnames)) !== nothing ? _ind : 0)
(c < 1 || c > size(arg1,2)) && error("$(var) Col name not found in GMTdataset col names or exceed col count.")
!(isa(_val, Int) || isa(_val, StrSymb)) && error("$(_val) can only be an Int, a String or a Symbol but was a $(typeof(_val))")
c::Int = isa(_val, Int) ? _val : ((_ind = findfirst(string(_val)::String .== arg1.colnames)) !== nothing ? _ind : 0)
(c < 1 || c > size(arg1,2)) && error("$(_val) Col name not found in GMTdataset col names or exceed col count.")
return c
end

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ using InteractiveUtils
include("test_PT_column_width.jl")
include("test_PT_default.jl")
include("test_PT_table_lines.jl")
include("test_PT_line_breaks.jl")
include("test_PT_row_numbers.jl")
include("test_PT_titles.jl")

include("test_avatars.jl")
include("test_misc.jl")
Expand Down
24 changes: 15 additions & 9 deletions test/test_PT_default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,10 @@ end
└────────────┘
"""

result = pretty_table(
String,
vec;
header = (["Header"], ["Sub-header"])
)
result = pretty_table(String, vec; header = (["Header"], ["Sub-header"]))
@test result == expected

@test_throws Exception pretty_table(
vec;
header = ["1", "1"]
)
@test_throws Exception pretty_table( vec; header = ["1", "1"])
end

@testset "Print missing, nothing, and #undef" begin
Expand Down Expand Up @@ -360,4 +353,17 @@ data = Any[1 false 1.0 0x01 ;
io_result = String(take!(io))

@test io_result == ("\e[1F\e[2K"^(num_lines) * result)

GMT._next_string_state('a', :escape_state_begin)
GMT._next_string_state('a', :escape_state_opening)
GMT._next_string_state('a', :escape_state_1)
GMT._next_string_state('a', :escape_state_2)
GMT._next_string_state('a', :escape_state_3)
GMT._next_string_state('a', :escape_hyperlink_opening)
GMT._next_string_state('a', :escape_hyperlink_1)
GMT._next_string_state('a', :escape_hyperlink_2)
GMT._next_string_state('a', :escape_hyperlink_1)
GMT._next_string_state('a', :escape_hyperlink_3)
GMT._next_string_state('a', :escape_hyperlink_end)
GMT._next_string_state('a', :escape_state_end)
end
Loading
Loading