Skip to content

Commit

Permalink
Format files using DocumentFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Mar 23, 2020
1 parent 16272bc commit 4c6cf8b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Documenter, TableShowUtils
makedocs(
modules = [TableShowUtils],
sitename = "TableShowUtils.jl",
analytics="UA-132838790-1",
analytics = "UA-132838790-1",
pages = [
"Introduction" => "index.md"
]
Expand Down
108 changes: 54 additions & 54 deletions src/TableShowUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ module TableShowUtils
import JSON, DataValues
import Markdown, Dates, Unicode

function printtable(io::IO, source, typename::AbstractString; force_unknown_rows=false)
function printtable(io::IO, source, typename::AbstractString; force_unknown_rows = false)
T = eltype(source)

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 @@ -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}()

Expand All @@ -47,76 +47,76 @@ 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)
printstyled(io, rpad(data[r,c], maxwidth[c]), color = :light_black)
else
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")"
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
Expand All @@ -129,18 +129,18 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
end
end

function printHTMLtable(io, source; force_unknown_rows=false)
function printHTMLtable(io, source; force_unknown_rows = false)
colnames = String.(fieldnames(eltype(source)))

max_elements = 10

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 @@ -165,17 +165,17 @@ function printHTMLtable(io, source; force_unknown_rows=false)
print(io, "<tr>")
for c in values(r)
print(io, "<td>")
Markdown.htmlesc(io, sprint(io->show(IOContext(io, :compact => true),c)))
Markdown.htmlesc(io, sprint(io->show(IOContext(io, :compact => true), c)))
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
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
Expand All @@ -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])
Expand Down
34 changes: 17 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) == """
<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)]
@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)) == "<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\"}]}"
@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

0 comments on commit 4c6cf8b

Please sign in to comment.