Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AUTO] Format files using DocumentFormat #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -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"
)
90 changes: 45 additions & 45 deletions src/TableShowUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}()

Expand All @@ -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,2)
if c < size(data, 2)
print(io, " │ ")
end
end
println(io)
for c in 1:size(data,2)
for c in 1:size(data, 2)
print(io, repeat("─", maxwidth[c]))
if c<size(data,2)
if c < size(data, 2)
print(io, "─┼─")
end
end
for r in 2:size(data,1)
for r in 2:size(data, 1)
println(io)
for c in 1:size(data,2)
for c in 1:size(data, 2)

if r in shortened_rows
print(io, data[r,c],)
print(io, data[r, c],)
print(io, "…")
else
if NAvalues[r,c]
printstyled(io, rpad(data[r,c], maxwidth[c]), color=:light_black)
if NAvalues[r, c]
printstyled(io, rpad(data[r, c], maxwidth[c]), color=:light_black)
else
print(io, textwidth_based_rpad(data[r,c], maxwidth[c]))
print(io, textwidth_based_rpad(data[r, c], maxwidth[c]))
end
end
if c<size(data,2)
if c < size(data, 2)
print(io, " │ ")
end
end
end

if rows===nothing
if rows === nothing
row_post_text = "more rows"
elseif rows > 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
Expand All @@ -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
Expand All @@ -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<max_elements+1 ? count_needed_plus_one : nothing
count_needed_plus_one = Iterators.count(i -> 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)
Expand All @@ -166,18 +166,18 @@ function printHTMLtable(io, source; force_unknown_rows=false)
for c in values(r)
print(io, "<td>")
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, "</td>")
end
print(io, "</tr>")
end

if rows==nothing
if rows == nothing
row_post_text = "... with more rows."
elseif rows > max_elements
extra_rows = rows - max_elements
Expand Down Expand Up @@ -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])
Expand Down
14 changes: 7 additions & 7 deletions test/test_tableshowutils.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,15 +13,15 @@
@test sprint(TableShowUtils.printHTMLtable, source) == """
<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>&quot;A&quot;</td></tr><tr><td>2</td><td>&quot;B&quot;</td></tr></tbody></table>"""

@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)) == "<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>&quot;A&quot;</td></tr><tr><td>2</td><td>&quot;B&quot;</td></tr><tr><td>&vellip;</td><td>&vellip;</td></tr></tbody></table><p>... with more rows.</p>"
@test sprint((stream) -> TableShowUtils.printHTMLtable(stream, source, force_unknown_rows=true)) == "<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>&quot;A&quot;</td></tr><tr><td>2</td><td>&quot;B&quot;</td></tr><tr><td>&vellip;</td><td>&vellip;</td></tr></tbody></table><p>... with more rows.</p>"

@test sprint(TableShowUtils.printdataresource, source) == "{\"schema\":{\"fields\":[{\"name\":\"a\",\"type\":\"integer\"},{\"name\":\"b\",\"type\":\"string\"}]},\"data\":[{\"a\":1,\"b\":\"A\"},{\"a\":2,\"b\":\"B\"}]}"

Expand All @@ -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) == """
<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2010-05-06T00:00:00</td></tr><tr><td>2</td><td>2011-05-06T00:00:00</td></tr></tbody></table>"""

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) == """
<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2010-05-06T00:00:00</td></tr><tr><td>2</td><td>#NA</td></tr></tbody></table>"""
Expand Down