diff --git a/src/GMT.jl b/src/GMT.jl index 0cad7f3ca..434ceafdb 100644 --- a/src/GMT.jl +++ b/src/GMT.jl @@ -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 diff --git a/src/PrettyTables.jl b/src/PrettyTables.jl index bf7a869e4..73769822f 100644 --- a/src/PrettyTables.jl +++ b/src/PrettyTables.jl @@ -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 @@ -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 =================================================================== @@ -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 @@ -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, @@ -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) @@ -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, @@ -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 diff --git a/src/psxy.jl b/src/psxy.jl index 4c46c93ac..9ff5be629 100644 --- a/src/psxy.jl +++ b/src/psxy.jl @@ -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 " @@ -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? @@ -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")) @@ -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")) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index adfa82ab5..64e1d3080 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") diff --git a/test/test_PT_default.jl b/test/test_PT_default.jl index d63d9df44..e7c5ce11b 100644 --- a/test/test_PT_default.jl +++ b/test/test_PT_default.jl @@ -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 @@ -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 diff --git a/test/test_PT_line_breaks.jl b/test/test_PT_line_breaks.jl new file mode 100644 index 000000000..b93617535 --- /dev/null +++ b/test/test_PT_line_breaks.jl @@ -0,0 +1,271 @@ +## Description ############################################################################# +# +# Tests of line breaks. +# +############################################################################################ + +@testset "Line breaks inside cells" begin + data = ["This line contains\nthe velocity [m/s]" 10.0; + "This line contains\nthe acceleration [m/s^2]" 1.0; + "This line contains\nthe time from the\nbeginning of the simulation" 10;] + + header = ["Information", "Value"] + + expected = """ +┌─────────────────────────────┬───────┐ +│ Information │ Value │ +├─────────────────────────────┼───────┤ +│ This line contains │ 10.0 │ +│ the velocity [m/s] │ │ +│ This line contains │ 1.0 │ +│ the acceleration [m/s^2] │ │ +│ This line contains │ 10 │ +│ the time from the │ │ +│ beginning of the simulation │ │ +└─────────────────────────────┴───────┘ +""" + + result = pretty_table(String, data; header = header, linebreaks = true) + @test result == expected + + expected = """ +┌─────────────────────────────┬───────┐ +│ Information │ Value │ +├─────────────────────────────┼───────┤ +│ This line contains │ 10.0 │ +│ the velocity [m/s] │ │ +│ This line contains │ 1.0 │ +│ the acceleration [m/s^2] │ │ +│ This line contains │ 10 │ +│ the time from the │ │ +│ beginning of the simulation │ │ +└─────────────────────────────┴───────┘ +""" + + result = pretty_table( String, data; header = header, alignment = :c, linebreaks = true) + @test result == expected + + expected = """ +┌─────────────────────────────┬───────┐ +│ Information │ Value │ +├─────────────────────────────┼───────┤ +│ This line contains │ 10.0 │ +│ the velocity [m/s] │ │ +│ This line contains │ 1.0 │ +│ the acceleration [m/s^2] │ │ +│ This line contains │ 10 │ +│ the time from the │ │ +│ beginning of the simulation │ │ +└─────────────────────────────┴───────┘ +""" + + result = pretty_table( String, data; header = header, alignment = :l, linebreaks = true) + @test result == expected + + expected = """ +┌────────────────────────────────────────────────────────────────────┬───────┐ +│ Information │ Value │ +├────────────────────────────────────────────────────────────────────┼───────┤ +│ This line contains\\nthe velocity [m/s] │ 10.0 │ +│ This line contains\\nthe acceleration [m/s^2] │ 1.0 │ +│ This line contains\\nthe time from the\\nbeginning of the simulation │ 10 │ +└────────────────────────────────────────────────────────────────────┴───────┘ +""" + + result = pretty_table(String, data; header = header) + @test result == expected + + # == Show Only the First Line ========================================================== + + expected = """ +┌────────────────────┬───────┐ +│ Information │ Value │ +├────────────────────┼───────┤ +│ This line contains │ 10.0 │ +│ This line contains │ 1.0 │ +│ This line contains │ 10 │ +└────────────────────┴───────┘ +""" + + result = pretty_table( String, data; header = header, cell_first_line_only = true) + @test result == expected +end + +@testset "Auto wrapping" begin + table = [1 """Ouviram do Ipiranga as margens plácidas + De um povo heróico o brado retumbante, + E o sol da Liberdade, em raios fúlgidos, + Brilhou no céu da Pátria nesse instante."""; + 2 """Se o penhor dessa igualdade + Conseguimos conquistar com braço forte, + Em teu seio, ó Liberdade, + Desafia o nosso peito a própria morte!"""; + 3 """Ó Pátria amada, Idolatrada, Salve! Salve! + Brasil, um sonho intenso, um raio vívido + De amor e de esperança à terra desce, + Se em teu formoso céu, risonho e límpido,"""] + + header = ["Verse number", "Verse"] + + expected = """ +┌──────────────┬────────────────────────────────┐ +│ Verse number │ Verse │ +├──────────────┼────────────────────────────────┤ +│ 1 │ Ouviram do Ipiranga as │ +│ │ margens plácidas │ +│ │ De um povo heróico o brado │ +│ │ retumbante, │ +│ │ E o sol da Liberdade, em │ +│ │ raios fúlgidos, │ +│ │ Brilhou no céu da Pátria │ +│ │ nesse instante. │ +├──────────────┼────────────────────────────────┤ +│ 2 │ Se o penhor dessa igualdade │ +│ │ Conseguimos conquistar com │ +│ │ braço forte, │ +│ │ Em teu seio, ó Liberdade, │ +│ │ Desafia o nosso peito a │ +│ │ própria morte! │ +├──────────────┼────────────────────────────────┤ +│ 3 │ Ó Pátria amada, Idolatrada, │ +│ │ Salve! Salve! │ +│ │ Brasil, um sonho intenso, um │ +│ │ raio vívido │ +│ │ De amor e de esperança à │ +│ │ terra desce, │ +│ │ Se em teu formoso céu, │ +│ │ risonho e límpido, │ +└──────────────┴────────────────────────────────┘ +""" + + result = pretty_table( String, table; header = header, autowrap = true, linebreaks = true, body_hlines = [1, 2], columns_width = [-1, 30]) + + @test result == expected + + expected = """ +┌──────────────┬────────────────────────────────┐ +│ Verse number │ Verse │ +├──────────────┼────────────────────────────────┤ +│ 1 │ Ouviram do Ipiranga as │ +│ │ margens plácidas │ +│ │ De um povo heróico o brado │ +│ │ retumbante, │ +│ │ E o sol da Liberdade, em │ +│ │ raios fúlgidos, │ +│ │ Brilhou no céu da Pátria │ +│ │ nesse instante. │ +├──────────────┼────────────────────────────────┤ +│ 2 │ Se o penhor dessa igualdade │ +│ │ Conseguimos conquistar com │ +│ │ braço forte, │ +│ │ Em teu seio, ó Liberdade, │ +│ │ Desafia o nosso peito a │ +│ │ própria morte! │ +├──────────────┼────────────────────────────────┤ +│ 3 │ Ó Pátria amada, Idolatrada, │ +│ │ Salve! Salve! │ +│ │ Brasil, um sonho intenso, um │ +│ │ raio vívido │ +│ │ De amor e de esperança à │ +│ │ terra desce, │ +│ │ Se em teu formoso céu, │ +│ │ risonho e límpido, │ +└──────────────┴────────────────────────────────┘ +""" + + result = pretty_table( String, table; header=header, alignment=:c, autowrap=true, linebreaks=true, body_hlines=[1, 2], columns_width=[-1, 30]) + + @test result == expected + + expected = """ +┌──────────────┬────────────────────────────────┐ +│ Verse number │ Verse │ +├──────────────┼────────────────────────────────┤ +│ 1 │ Ouviram do Ipiranga as │ +│ │ margens plácidas │ +│ │ De um povo heróico o brado │ +│ │ retumbante, │ +│ │ E o sol da Liberdade, em │ +│ │ raios fúlgidos, │ +│ │ Brilhou no céu da Pátria │ +│ │ nesse instante. │ +├──────────────┼────────────────────────────────┤ +│ 2 │ Se o penhor dessa igualdade │ +│ │ Conseguimos conquistar com │ +│ │ braço forte, │ +│ │ Em teu seio, ó Liberdade, │ +│ │ Desafia o nosso peito a │ +│ │ própria morte! │ +├──────────────┼────────────────────────────────┤ +│ 3 │ Ó Pátria amada, Idolatrada, │ +│ │ Salve! Salve! │ +│ │ Brasil, um sonho intenso, um │ +│ │ raio vívido │ +│ │ De amor e de esperança à │ +│ │ terra desce, │ +│ │ Se em teu formoso céu, │ +│ │ risonho e límpido, │ +└──────────────┴────────────────────────────────┘ +""" + + result = pretty_table( + String, + table; + header = header, + alignment = :l, + autowrap = true, + linebreaks = true, + body_hlines = [1, 2], + columns_width = [-1, 30] + ) + + @test result == expected + + # -- Test with additional rows --------------------------------------------------------- + + expected = """ +┌─────┬──────────┬──────────────┬────────────────────────────────┐ +│ Row │ Verso │ Verse number │ Verse │ +├─────┼──────────┼──────────────┼────────────────────────────────┤ +│ 1 │ Primeiro │ 1 │ Ouviram do Ipiranga as │ +│ │ │ │ margens plácidas │ +│ │ │ │ De um povo heróico o brado │ +│ │ │ │ retumbante, │ +│ │ │ │ E o sol da Liberdade, em │ +│ │ │ │ raios fúlgidos, │ +│ │ │ │ Brilhou no céu da Pátria │ +│ │ │ │ nesse instante. │ +├─────┼──────────┼──────────────┼────────────────────────────────┤ +│ 2 │ Segundo │ 2 │ Se o penhor dessa igualdade │ +│ │ │ │ Conseguimos conquistar com │ +│ │ │ │ braço forte, │ +│ │ │ │ Em teu seio, ó Liberdade, │ +│ │ │ │ Desafia o nosso peito a │ +│ │ │ │ própria morte! │ +├─────┼──────────┼──────────────┼────────────────────────────────┤ +│ 3 │ Terceiro │ 3 │ Ó Pátria amada, Idolatrada, │ +│ │ │ │ Salve! Salve! │ +│ │ │ │ Brasil, um sonho intenso, um │ +│ │ │ │ raio vívido │ +│ │ │ │ De amor e de esperança à │ +│ │ │ │ terra desce, │ +│ │ │ │ Se em teu formoso céu, │ +│ │ │ │ risonho e límpido, │ +└─────┴──────────┴──────────────┴────────────────────────────────┘ +""" + + result = pretty_table( + String, table; + header = header, + alignment = :l, + autowrap = true, + linebreaks = true, + body_hlines = [1, 2], + columns_width = [-1, 30], + show_row_number = true, + row_labels = ["Primeiro", "Segundo", "Terceiro"], + row_label_column_title = "Verso" + ) + + @test result == expected +end diff --git a/test/test_PT_row_numbers.jl b/test/test_PT_row_numbers.jl new file mode 100644 index 000000000..4e6d4a70f --- /dev/null +++ b/test/test_PT_row_numbers.jl @@ -0,0 +1,73 @@ +## Description ############################################################################# +# +# Tests of row number column. +# +############################################################################################ + +@testset "Show Row Number" begin + +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 ;] + + expected = """ +┌─────┬────────┬────────┬────────┬────────┐ +│ Row │ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │ +├─────┼────────┼────────┼────────┼────────┤ +│ 1 │ 1 │ false │ 1.0 │ 1 │ +│ 2 │ 2 │ true │ 2.0 │ 2 │ +│ 3 │ 3 │ false │ 3.0 │ 3 │ +│ 4 │ 4 │ true │ 4.0 │ 4 │ +│ 5 │ 5 │ false │ 5.0 │ 5 │ +│ 6 │ 6 │ true │ 6.0 │ 6 │ +└─────┴────────┴────────┴────────┴────────┘ +""" + result = pretty_table( + String, + data; + alignment = [:l, :r, :c, :r], + show_row_number = true + ) + @test result == expected + + expected = """ +┌─────┬────────┬────────┬────────┬────────┐ +│ Row │ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │ +├─────┼────────┼────────┼────────┼────────┤ +│ 1 │ 1 │ false │ 1.0 │ 1 │ +│ 2 │ 2 │ true │ 2.0 │ 2 │ +│ 3 │ 3 │ false │ 3.0 │ 3 │ +│ 4 │ 4 │ true │ 4.0 │ 4 │ +│ 5 │ 5 │ false │ 5.0 │ 5 │ +│ 6 │ 6 │ true │ 6.0 │ 6 │ +└─────┴────────┴────────┴────────┴────────┘ +""" + + result = pretty_table( + String, + data; + alignment = [:l, :r, :c, :r], + row_number_alignment = :c, + show_row_number = true + ) + @test result == expected + + expected = """ +┌───┬────────┬────────┬────────┬────────┐ +│ # │ Col. 1 │ Col. 2 │ Col. 3 │ Col. 4 │ +├───┼────────┼────────┼────────┼────────┤ +│ 1 │ 1 │ false │ 1.0 │ 1 │ +│ 2 │ 2 │ true │ 2.0 │ 2 │ +│ 3 │ 3 │ false │ 3.0 │ 3 │ +│ 4 │ 4 │ true │ 4.0 │ 4 │ +│ 5 │ 5 │ false │ 5.0 │ 5 │ +│ 6 │ 6 │ true │ 6.0 │ 6 │ +└───┴────────┴────────┴────────┴────────┘ +""" + + result = pretty_table( String, data; alignment=[:l, :r, :c, :r], row_number_alignment=:c, row_number_column_title="#", show_row_number=true) + @test result == expected +end diff --git a/test/test_PT_titles.jl b/test/test_PT_titles.jl new file mode 100644 index 000000000..2bb8f805d --- /dev/null +++ b/test/test_PT_titles.jl @@ -0,0 +1,183 @@ +## Description ############################################################################# +# +# Tests of table titles. +# +############################################################################################ + +@testset "Titles" begin + +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 ;] + + title = "This is a very very long title that will be displayed above the table." + + expected = """ +This is a very very long title that will be displayed above the table. +┌────────┬────────┬────────┬────────┐ +│ 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 │ +└────────┴────────┴────────┴────────┘ +""" + result = pretty_table(String, data, title = title) + @test result == expected + + expected = """ +This is a very very long title that … +┌────────┬────────┬────────┬────────┐ +│ 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 │ +└────────┴────────┴────────┴────────┘ +""" + result = pretty_table( + String, + data; + title = title, + title_same_width_as_table = true + ) + @test result == expected + + expected = """ +This is a very very long title that +will be displayed above the table. +┌────────┬────────┬────────┬────────┐ +│ 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 │ +└────────┴────────┴────────┴────────┘ +""" + result = pretty_table( + String, + data; + title = title, + title_autowrap = true, + title_same_width_as_table = true + ) + @test result == expected + + expected = """ + This is a very very long title that + will be displayed above the table. +┌────────┬────────┬────────┬────────┐ +│ 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 │ +└────────┴────────┴────────┴────────┘ +""" + result = pretty_table( + String, + data; + title = title, + title_alignment = :c, + title_autowrap = true, + title_same_width_as_table = true + ) + @test result == expected + + expected = """ + This is a very very long title that + will be displayed above the table. +┌────────┬────────┬────────┬────────┐ +│ 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 │ +└────────┴────────┴────────┴────────┘ +""" + result = pretty_table( + String, + data; + title = title, + title_alignment = :r, + title_autowrap = true, + title_same_width_as_table = true + ) + @test result == expected +end + +@testset "Title With Table Cropping" begin + +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 ;] + + + # == Cropping ========================================================================== + + expected = """ +This is a long long long long long… +That has two lines. +┌────────┬────────┬────────┬─────── +│ Col. 1 │ Col. 2 │ Col. 3 │ Col. ⋯ +├────────┼────────┼────────┼─────── +│ 1 │ false │ 1.0 │ ⋯ +│ 2 │ true │ 2.0 │ ⋯ +│ 3 │ false │ 3.0 │ ⋯ +│ ⋮ │ ⋮ │ ⋮ │ ⋮ ⋱ +└────────┴────────┴────────┴─────── + 1 column and 3 rows omitted +""" + result = pretty_table( + String, + data; + crop = :both, + display_size = (13, 35), + title = "This is a long long long long long long title\nThat has two lines." + ) + @test result == expected + + expected = """ +This is a long long long long long +long title +That has two lines. +┌────────┬────────┬────────┬─────── +│ Col. 1 │ Col. 2 │ Col. 3 │ Col. ⋯ +├────────┼────────┼────────┼─────── +│ 1 │ false │ 1.0 │ ⋯ +│ 2 │ true │ 2.0 │ ⋯ +│ ⋮ │ ⋮ │ ⋮ │ ⋮ ⋱ +└────────┴────────┴────────┴─────── + 1 column and 4 rows omitted +""" + result = pretty_table( + String, + data; + crop = :both, + display_size = (13, 35), + title = "This is a long long long long long long title\nThat has two lines.", + title_autowrap = true + ) + @test result == expected +end