From d92f3e05c3b82e606a1428e740d79df9dc16ff75 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 22 Aug 2020 07:47:16 +0000 Subject: [PATCH] Format files using DocumentFormat --- docs/make.jl | 8 ++-- src/TableShowUtils.jl | 102 +++++++++++++++++++++--------------------- test/runtests.jl | 34 +++++++------- 3 files changed, 72 insertions(+), 72 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index aedd2dc..24c7b6f 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,14 +1,14 @@ using Documenter, TableShowUtils makedocs( - modules = [TableShowUtils], - sitename = "TableShowUtils.jl", + modules=[TableShowUtils], + sitename="TableShowUtils.jl", analytics="UA-132838790-1", - pages = [ + pages=[ "Introduction" => "index.md" ] ) deploydocs( - repo = "github.com/queryverse/TableShowUtils.jl.git" + repo="github.com/queryverse/TableShowUtils.jl.git" ) diff --git a/src/TableShowUtils.jl b/src/TableShowUtils.jl index e2c3d9d..7b8196e 100644 --- a/src/TableShowUtils.jl +++ b/src/TableShowUtils.jl @@ -9,12 +9,12 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows if force_unknown_rows rows = nothing data = Iterators.take(source, 10) |> collect - elseif Base.IteratorSize(source) isa Union{Base.HasLength, Base.HasShape{1}} + elseif Base.IteratorSize(source) isa Union{Base.HasLength,Base.HasShape{1}} rows = length(source) data = Iterators.take(source, 10) |> collect else data_plus_one = Iterators.take(source, 11) |> collect - if length(data_plus_one)<11 + if length(data_plus_one) < 11 rows = length(data_plus_one) data = data_plus_one else @@ -25,18 +25,18 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows cols = length(fieldnames(T)) - println(io, "$(rows===nothing ? "?" : rows)x$(cols) $typename") + println(io, "$(rows === nothing ? "?" : rows)x$(cols) $typename") colnames = String.(fieldnames(eltype(source))) - NAvalues = [r==0 ? false : DataValues.isna(data[r][c]) for r in 0:length(data), c in 1:cols] + NAvalues = [r == 0 ? false : DataValues.isna(data[r][c]) for r in 0:length(data), c in 1:cols] - data = [r==0 ? colnames[c] : isa(data[r][c], AbstractString) ? data[r][c] : sprint(io->show(IOContext(io, :compact => true), data[r][c])) for r in 0:length(data), c in 1:cols] + data = [r == 0 ? colnames[c] : isa(data[r][c], AbstractString) ? data[r][c] : sprint(io -> show(IOContext(io, :compact => true), data[r][c])) for r in 0:length(data), c in 1:cols] maxwidth = [maximum(Unicode.textwidth.(data[:,c])) for c in 1:cols] available_heigth, available_width = displaysize(io) - available_width -=1 + available_width -= 1 shortened_rows = Set{Int}() @@ -47,42 +47,42 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows return string(s, ' '^m) end - while sum(maxwidth) + (size(data,2)-1) * 3 > available_width - if size(data,2)==1 - for r in 1:size(data,1) - if length(data[r,1])>available_width - data[r,1] = data[r,1][1:nextind(data[r,1], 0, available_width-2)] * "\"" + while sum(maxwidth) + (size(data, 2) - 1) * 3 > available_width + if size(data, 2) == 1 + for r in 1:size(data, 1) + if length(data[r,1]) > available_width + data[r,1] = data[r,1][1:nextind(data[r,1], 0, available_width - 2)] * "\"" push!(shortened_rows, r) end end maxwidth[1] = available_width break else - data = data[:,1:end-1] + data = data[:,1:end - 1] - maxwidth = [maximum(length.(data[:,c])) for c in 1:size(data,2)] + maxwidth = [maximum(length.(data[:,c])) for c in 1:size(data, 2)] end end - for c in 1:size(data,2) + for c in 1:size(data, 2) print(io, rpad(colnames[c], maxwidth[c])) - if c size(data,1)-1 + elseif rows > size(data, 1) - 1 extra_rows = rows - 10 - row_post_text = "$extra_rows more $(extra_rows==1 ? "row" : "rows")" + row_post_text = "$extra_rows more $(extra_rows == 1 ? "row" : "rows")" else row_post_text = "" end - if size(data,2)!=cols - extra_cols = cols-size(data,2) - col_post_text = "$extra_cols more $(extra_cols==1 ? "column" : "columns"): " - col_post_text *= Base.join([colnames[cols-extra_cols+1:end]...], ", ") + if size(data, 2) != cols + extra_cols = cols - size(data, 2) + col_post_text = "$extra_cols more $(extra_cols == 1 ? "column" : "columns"): " + col_post_text *= Base.join([colnames[cols - extra_cols + 1:end]...], ", ") else col_post_text = "" end if !isempty(row_post_text) || !isempty(col_post_text) println(io) - print(io,"... with ") + print(io, "... with ") if !isempty(row_post_text) print(io, row_post_text) end @@ -136,11 +136,11 @@ function printHTMLtable(io, source; force_unknown_rows=false) if force_unknown_rows rows = nothing - elseif Base.IteratorSize(source) isa Union{Base.HasLength, Base.HasShape{1}} + elseif Base.IteratorSize(source) isa Union{Base.HasLength,Base.HasShape{1}} rows = length(source) else - count_needed_plus_one = Iterators.count(i->true, Iterators.take(source, max_elements+1)) - rows = count_needed_plus_one true, Iterators.take(source, max_elements + 1)) + rows = count_needed_plus_one < max_elements + 1 ? count_needed_plus_one : nothing end haslimit = get(io, :limit, true) @@ -165,17 +165,17 @@ function printHTMLtable(io, source; force_unknown_rows=false) print(io, "") for c in values(r) print(io, "") - Markdown.htmlesc(io, sprint(io->show(IOContext(io, :compact => true),c))) + Markdown.htmlesc(io, sprint(io -> show(IOContext(io, :compact => true), c))) print(io, "") end print(io, "") end - if rows==nothing + if rows == nothing row_post_text = "... with more rows." elseif rows > max_elements extra_rows = rows - max_elements - row_post_text = "... with $extra_rows more $(extra_rows==1 ? "row" : "rows")." + row_post_text = "... with $extra_rows more $(extra_rows == 1 ? "row" : "rows")." else row_post_text = "" end @@ -202,45 +202,45 @@ end Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.dataresource+json")}) = true julia_type_to_schema_type(::Type{T}) where {T} = "string" -julia_type_to_schema_type(::Type{T}) where {T<:AbstractFloat} = "number" -julia_type_to_schema_type(::Type{T}) where {T<:Integer} = "integer" -julia_type_to_schema_type(::Type{T}) where {T<:Bool} = "boolean" -julia_type_to_schema_type(::Type{T}) where {T<:Dates.Time} = "time" -julia_type_to_schema_type(::Type{T}) where {T<:Dates.Date} = "date" -julia_type_to_schema_type(::Type{T}) where {T<:Dates.DateTime} = "datetime" -julia_type_to_schema_type(::Type{T}) where {T<:AbstractString} = "string" -julia_type_to_schema_type(::Type{T}) where {S, T<:DataValues.DataValue{S}} = julia_type_to_schema_type(S) +julia_type_to_schema_type(::Type{T}) where {T <: AbstractFloat} = "number" +julia_type_to_schema_type(::Type{T}) where {T <: Integer} = "integer" +julia_type_to_schema_type(::Type{T}) where {T <: Bool} = "boolean" +julia_type_to_schema_type(::Type{T}) where {T <: Dates.Time} = "time" +julia_type_to_schema_type(::Type{T}) where {T <: Dates.Date} = "date" +julia_type_to_schema_type(::Type{T}) where {T <: Dates.DateTime} = "datetime" +julia_type_to_schema_type(::Type{T}) where {T <: AbstractString} = "string" +julia_type_to_schema_type(::Type{T}) where {S,T <: DataValues.DataValue{S}} = julia_type_to_schema_type(S) own_json_formatter(io, x) = JSON.print(io, x) -own_json_formatter(io, x::DataValues.DataValue) = DataValues.isna(x) ? JSON.print(io,nothing) : own_json_formatter(io, x[]) +own_json_formatter(io, x::DataValues.DataValue) = DataValues.isna(x) ? JSON.print(io, nothing) : own_json_formatter(io, x[]) function printdataresource(io::IO, source) if Base.IteratorEltype(source) isa Base.EltypeUnknown first_el = first(source) col_names = String.(propertynames(first_el)) - col_types = [fieldtype(typeof(first_el), i) for i=1:length(col_names)] + col_types = [fieldtype(typeof(first_el), i) for i = 1:length(col_names)] else col_names = String.(fieldnames(eltype(source))) - col_types = [fieldtype(eltype(source), i) for i=1:length(col_names)] + col_types = [fieldtype(eltype(source), i) for i = 1:length(col_names)] end - schema = Dict("fields" => [Dict("name"=>string(i[1]), "type"=>julia_type_to_schema_type(i[2])) for i in zip(col_names, col_types)]) + schema = Dict("fields" => [Dict("name" => string(i[1]), "type" => julia_type_to_schema_type(i[2])) for i in zip(col_names, col_types)]) print(io, "{") JSON.print(io, "schema") print(io, ":") - JSON.print(io,schema) - print(io,",") + JSON.print(io, schema) + print(io, ",") JSON.print(io, "data") print(io, ":[") for (row_i, row) in enumerate(source) - if row_i>1 + if row_i > 1 print(io, ",") end print(io, "{") for col in 1:length(col_names) - if col>1 + if col > 1 print(io, ",") end JSON.print(io, col_names[col]) diff --git a/test/runtests.jl b/test/runtests.jl index 8ecdc5c..3cfba32 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,37 +5,37 @@ using Dates @testset "TableShowUtils" begin -source = [(a=1,b="A"),(a=2,b="B")] + source = [(a = 1, b = "A"),(a = 2, b = "B")] -@test sprint(TableShowUtils.printtable, source, "foo file") == """ + @test sprint(TableShowUtils.printtable, source, "foo file") == """ 2x2 foo file a │ b ──┼── 1 │ A 2 │ B""" -@test sprint(TableShowUtils.printHTMLtable, source) == """ + @test sprint(TableShowUtils.printHTMLtable, source) == """
ab
1"A"
2"B"
""" -@test sprint((stream) -> TableShowUtils.printtable(stream, source, "foo file", force_unknown_rows = true)) == "?x2 foo file\na │ b\n──┼──\n1 │ A\n2 │ B\n... with more rows" + @test sprint((stream) -> TableShowUtils.printtable(stream, source, "foo file", force_unknown_rows=true)) == "?x2 foo file\na │ b\n──┼──\n1 │ A\n2 │ B\n... with more rows" -source_with_many_columns = [(a0=1,b0=1,c0=1,a1=1,b1=1,c1=1,a2=1,b2=1,c2=1,a3=1,b3=1,c3=1,a4=1,b4=1,c4=1,a5=1,b5=1,c5=1,a6=1,b6=1,c6=1,a7=1,b7=1,c7=1,a8=1,b8=1,c8=1,a9=1,b9=1,c9=1,a10=1,b10=1,c10=1)] -@test sprint(TableShowUtils.printtable, source_with_many_columns, "foo file") == "1x33 foo file\na0 │ b0 │ c0 │ a1 │ b1 │ c1 │ a2 │ b2 │ c2 │ a3 │ b3 │ c3 │ a4 │ b4 │ c4 │ a5\n───┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼───\n1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 \n... with 17 more columns: b5, c5, a6, b6, c6, a7, b7, c7, a8, b8, c8, a9, b9, c9, a10, b10, c10" + source_with_many_columns = [(a0 = 1, b0 = 1, c0 = 1, a1 = 1, b1 = 1, c1 = 1, a2 = 1, b2 = 1, c2 = 1, a3 = 1, b3 = 1, c3 = 1, a4 = 1, b4 = 1, c4 = 1, a5 = 1, b5 = 1, c5 = 1, a6 = 1, b6 = 1, c6 = 1, a7 = 1, b7 = 1, c7 = 1, a8 = 1, b8 = 1, c8 = 1, a9 = 1, b9 = 1, c9 = 1, a10 = 1, b10 = 1, c10 = 1)] + @test sprint(TableShowUtils.printtable, source_with_many_columns, "foo file") == "1x33 foo file\na0 │ b0 │ c0 │ a1 │ b1 │ c1 │ a2 │ b2 │ c2 │ a3 │ b3 │ c3 │ a4 │ b4 │ c4 │ a5\n───┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼───\n1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 \n... with 17 more columns: b5, c5, a6, b6, c6, a7, b7, c7, a8, b8, c8, a9, b9, c9, a10, b10, c10" -source_with_NA = [(a=1,b="A"),(a=2,b=NA)] -@test sprint(TableShowUtils.printtable, source_with_NA, "foo file") == "2x2 foo file\na │ b \n──┼────\n1 │ A \n2 │ #NA" + source_with_NA = [(a = 1, b = "A"),(a = 2, b = NA)] + @test sprint(TableShowUtils.printtable, source_with_NA, "foo file") == "2x2 foo file\na │ b \n──┼────\n1 │ A \n2 │ #NA" -@test sprint((stream) -> TableShowUtils.printHTMLtable(stream, source, force_unknown_rows = true)) == "
ab
1"A"
2"B"

... with more rows.

" + @test sprint((stream) -> TableShowUtils.printHTMLtable(stream, source, force_unknown_rows=true)) == "
ab
1"A"
2"B"

... with more rows.

" -@test sprint(TableShowUtils.printdataresource, source) == "{\"schema\":{\"fields\":[{\"name\":\"a\",\"type\":\"integer\"},{\"name\":\"b\",\"type\":\"string\"}]},\"data\":[{\"a\":1,\"b\":\"A\"},{\"a\":2,\"b\":\"B\"}]}" + @test sprint(TableShowUtils.printdataresource, source) == "{\"schema\":{\"fields\":[{\"name\":\"a\",\"type\":\"integer\"},{\"name\":\"b\",\"type\":\"string\"}]},\"data\":[{\"a\":1,\"b\":\"A\"},{\"a\":2,\"b\":\"B\"}]}" -@test sprint(TableShowUtils.printdataresource, source_with_NA) == "{\"schema\":{\"fields\":[{\"name\":\"a\",\"type\":\"string\"},{\"name\":\"b\",\"type\":\"string\"}]},\"data\":[{\"a\":1,\"b\":\"A\"},{\"a\":2,\"b\":null}]}" + @test sprint(TableShowUtils.printdataresource, source_with_NA) == "{\"schema\":{\"fields\":[{\"name\":\"a\",\"type\":\"string\"},{\"name\":\"b\",\"type\":\"string\"}]},\"data\":[{\"a\":1,\"b\":\"A\"},{\"a\":2,\"b\":null}]}" -@test TableShowUtils.julia_type_to_schema_type(AbstractFloat) == "number" -@test TableShowUtils.julia_type_to_schema_type(Bool) == "boolean" -@test TableShowUtils.julia_type_to_schema_type(Dates.Time) == "time" -@test TableShowUtils.julia_type_to_schema_type(Dates.Date) == "date" -@test TableShowUtils.julia_type_to_schema_type(Dates.DateTime) == "datetime" -@test TableShowUtils.julia_type_to_schema_type(DataValues.DataValue{Integer}) == "integer" + @test TableShowUtils.julia_type_to_schema_type(AbstractFloat) == "number" + @test TableShowUtils.julia_type_to_schema_type(Bool) == "boolean" + @test TableShowUtils.julia_type_to_schema_type(Dates.Time) == "time" + @test TableShowUtils.julia_type_to_schema_type(Dates.Date) == "date" + @test TableShowUtils.julia_type_to_schema_type(Dates.DateTime) == "datetime" + @test TableShowUtils.julia_type_to_schema_type(DataValues.DataValue{Integer}) == "integer" end