From 60954a4aa7df9d0105e0383d4c7c885afa768a13 Mon Sep 17 00:00:00 2001 From: davidanthoff Date: Fri, 29 Dec 2023 03:46:36 +0000 Subject: [PATCH] Format files using DocumentFormat --- docs/make.jl | 10 ++--- src/TableShowUtils.jl | 90 ++++++++++++++++++------------------- test/test_tableshowutils.jl | 14 +++--- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index aedd2dc..0853774 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,14 +1,14 @@ using Documenter, TableShowUtils makedocs( - modules = [TableShowUtils], - sitename = "TableShowUtils.jl", - analytics="UA-132838790-1", - pages = [ + modules=[TableShowUtils], + sitename="TableShowUtils.jl", + analytics="UA-132838790-1", + 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 50a7339..57dec53 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 @@ -29,14 +29,14 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows 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] + 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,67 +47,67 @@ 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")" else row_post_text = "" end - if size(data,2)!=cols - extra_cols = cols-size(data,2) + 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 @@ -116,7 +116,7 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows 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) @@ -166,18 +166,18 @@ function printHTMLtable(io, source; force_unknown_rows=false) for c in values(r) print(io, "") if c isa Dates.AbstractTime - Markdown.htmlesc(io, sprint(io->print(IOContext(io, :compact => true),c))) + Markdown.htmlesc(io, sprint(io -> print(IOContext(io, :compact => true), c))) elseif c isa DataValues.DataValue && DataValues.hasvalue(c) && c[] isa Dates.AbstractDateTime - Markdown.htmlesc(io, sprint(io->print(IOContext(io, :compact => true),c[]))) + Markdown.htmlesc(io, sprint(io -> print(IOContext(io, :compact => true), c[]))) else - Markdown.htmlesc(io, sprint(io->show(IOContext(io, :compact => true),c))) + Markdown.htmlesc(io, sprint(io -> show(IOContext(io, :compact => true), c))) end 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 @@ -215,38 +215,38 @@ 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 {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/test_tableshowutils.jl b/test/test_tableshowutils.jl index f9f7ed8..9d0a6eb 100644 --- a/test/test_tableshowutils.jl +++ b/test/test_tableshowutils.jl @@ -1,7 +1,7 @@ @testitem "TableShowUtils" begin using DataValues, Dates - 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") == """ 2x2 foo file @@ -13,15 +13,15 @@ @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)] + 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)] + 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\"}]}" @@ -38,12 +38,12 @@ end @testitem "DateTime" begin using Dates, DataValues - source = [(a=1,b=DateTime(2010, 5, 6)),(a=2,b=DateTime(2011, 5, 6))] + source = [(a=1, b=DateTime(2010, 5, 6)), (a=2, b=DateTime(2011, 5, 6))] @test sprint(TableShowUtils.printHTMLtable, source) == """
ab
12010-05-06T00:00:00
22011-05-06T00:00:00
""" - source2 = [(a=1,b=DataValue(DateTime(2010, 5, 6))),(a=2,b=NA)] + source2 = [(a=1, b=DataValue(DateTime(2010, 5, 6))), (a=2, b=NA)] @test sprint(TableShowUtils.printHTMLtable, source2) == """
ab
12010-05-06T00:00:00
2#NA
"""