Skip to content

Commit 840ba46

Browse files
committed
Format files using DocumentFormat
1 parent 33379ae commit 840ba46

File tree

3 files changed

+75
-79
lines changed

3 files changed

+75
-79
lines changed

docs/make.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
using Documenter, TableShowUtils
22

3-
makedocs(
4-
modules = [TableShowUtils],
3+
makedocs(modules = [TableShowUtils],
54
sitename = "TableShowUtils.jl",
6-
analytics="UA-132838790-1",
5+
analytics = "UA-132838790-1",
76
pages = [
87
"Introduction" => "index.md"
9-
]
10-
)
8+
])
119

12-
deploydocs(
13-
repo = "github.com/queryverse/TableShowUtils.jl.git"
14-
)
10+
deploydocs(repo = "github.com/queryverse/TableShowUtils.jl.git")

src/TableShowUtils.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ module TableShowUtils
33
import JSON, DataValues
44
import Markdown, Dates, Unicode
55

6-
function printtable(io::IO, source, typename::AbstractString; force_unknown_rows=false)
6+
function printtable(io::IO, source, typename::AbstractString; force_unknown_rows = false)
77
T = eltype(source)
88

99
if force_unknown_rows
1010
rows = nothing
1111
data = Iterators.take(source, 10) |> collect
12-
elseif Base.IteratorSize(source) isa Union{Base.HasLength, Base.HasShape{1}}
12+
elseif Base.IteratorSize(source) isa Union{Base.HasLength,Base.HasShape{1}}
1313
rows = length(source)
1414
data = Iterators.take(source, 10) |> collect
1515
else
1616
data_plus_one = Iterators.take(source, 11) |> collect
17-
if length(data_plus_one)<11
17+
if length(data_plus_one) < 11
1818
rows = length(data_plus_one)
1919
data = data_plus_one
2020
else
@@ -25,18 +25,18 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
2525

2626
cols = length(fieldnames(T))
2727

28-
println(io, "$(rows===nothing ? "?" : rows)x$(cols) $typename")
28+
println(io, "$(rows === nothing ? "?" : rows)x$(cols) $typename")
2929

3030
colnames = String.(fieldnames(eltype(source)))
3131

32-
NAvalues = [r==0 ? false : DataValues.isna(data[r][c]) for r in 0:length(data), c in 1:cols]
32+
NAvalues = [r == 0 ? false : DataValues.isna(data[r][c]) for r in 0:length(data), c in 1:cols]
3333

34-
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]
34+
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]
3535

3636
maxwidth = [maximum(Unicode.textwidth.(data[:,c])) for c in 1:cols]
3737

3838
available_heigth, available_width = displaysize(io)
39-
available_width -=1
39+
available_width -= 1
4040

4141
shortened_rows = Set{Int}()
4242

@@ -47,76 +47,76 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
4747
return string(s, ' '^m)
4848
end
4949

50-
while sum(maxwidth) + (size(data,2)-1) * 3 > available_width
51-
if size(data,2)==1
52-
for r in 1:size(data,1)
53-
if length(data[r,1])>available_width
54-
data[r,1] = data[r,1][1:nextind(data[r,1], 0, available_width-2)] * "\""
50+
while sum(maxwidth) + (size(data, 2) - 1) * 3 > available_width
51+
if size(data, 2) == 1
52+
for r in 1:size(data, 1)
53+
if length(data[r,1]) > available_width
54+
data[r,1] = data[r,1][1:nextind(data[r,1], 0, available_width - 2)] * "\""
5555
push!(shortened_rows, r)
5656
end
5757
end
5858
maxwidth[1] = available_width
5959
break
6060
else
61-
data = data[:,1:end-1]
61+
data = data[:,1:end - 1]
6262

63-
maxwidth = [maximum(length.(data[:,c])) for c in 1:size(data,2)]
63+
maxwidth = [maximum(length.(data[:,c])) for c in 1:size(data, 2)]
6464
end
6565
end
6666

67-
for c in 1:size(data,2)
67+
for c in 1:size(data, 2)
6868
print(io, rpad(colnames[c], maxwidth[c]))
69-
if c<size(data,2)
69+
if c < size(data, 2)
7070
print(io, "")
7171
end
7272
end
7373
println(io)
74-
for c in 1:size(data,2)
74+
for c in 1:size(data, 2)
7575
print(io, repeat("", maxwidth[c]))
76-
if c<size(data,2)
76+
if c < size(data, 2)
7777
print(io, "─┼─")
7878
end
7979
end
80-
for r in 2:size(data,1)
80+
for r in 2:size(data, 1)
8181
println(io)
82-
for c in 1:size(data,2)
82+
for c in 1:size(data, 2)
8383

8484
if r in shortened_rows
85-
print(io, data[r,c],)
85+
print(io, data[r,c], )
8686
print(io, "")
8787
else
8888
if NAvalues[r,c]
89-
printstyled(io, rpad(data[r,c], maxwidth[c]), color=:light_black)
89+
printstyled(io, rpad(data[r,c], maxwidth[c]), color = :light_black)
9090
else
9191
print(io, textwidth_based_rpad(data[r,c], maxwidth[c]))
9292
end
9393
end
94-
if c<size(data,2)
94+
if c < size(data, 2)
9595
print(io, "")
9696
end
9797
end
9898
end
9999

100-
if rows===nothing
100+
if rows === nothing
101101
row_post_text = "more rows"
102-
elseif rows > size(data,1)-1
102+
elseif rows > size(data, 1) - 1
103103
extra_rows = rows - 10
104-
row_post_text = "$extra_rows more $(extra_rows==1 ? "row" : "rows")"
104+
row_post_text = "$extra_rows more $(extra_rows == 1 ? "row" : "rows")"
105105
else
106106
row_post_text = ""
107107
end
108108

109-
if size(data,2)!=cols
110-
extra_cols = cols-size(data,2)
111-
col_post_text = "$extra_cols more $(extra_cols==1 ? "column" : "columns"): "
112-
col_post_text *= Base.join([colnames[cols-extra_cols+1:end]...], ", ")
109+
if size(data, 2) != cols
110+
extra_cols = cols - size(data, 2)
111+
col_post_text = "$extra_cols more $(extra_cols == 1 ? "column" : "columns"): "
112+
col_post_text *= Base.join([colnames[cols - extra_cols + 1:end]...], ", ")
113113
else
114114
col_post_text = ""
115115
end
116116

117117
if !isempty(row_post_text) || !isempty(col_post_text)
118118
println(io)
119-
print(io,"... with ")
119+
print(io, "... with ")
120120
if !isempty(row_post_text)
121121
print(io, row_post_text)
122122
end
@@ -129,18 +129,18 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
129129
end
130130
end
131131

132-
function printHTMLtable(io, source; force_unknown_rows=false)
132+
function printHTMLtable(io, source; force_unknown_rows = false)
133133
colnames = String.(fieldnames(eltype(source)))
134134

135135
max_elements = 10
136136

137137
if force_unknown_rows
138138
rows = nothing
139-
elseif Base.IteratorSize(source) isa Union{Base.HasLength, Base.HasShape{1}}
139+
elseif Base.IteratorSize(source) isa Union{Base.HasLength,Base.HasShape{1}}
140140
rows = length(source)
141141
else
142-
count_needed_plus_one = Iterators.count(i->true, Iterators.take(source, max_elements+1))
143-
rows = count_needed_plus_one<max_elements+1 ? count_needed_plus_one : nothing
142+
count_needed_plus_one = Iterators.count(i->true, Iterators.take(source, max_elements + 1))
143+
rows = count_needed_plus_one < max_elements + 1 ? count_needed_plus_one : nothing
144144
end
145145

146146
haslimit = get(io, :limit, true)
@@ -165,17 +165,17 @@ function printHTMLtable(io, source; force_unknown_rows=false)
165165
print(io, "<tr>")
166166
for c in values(r)
167167
print(io, "<td>")
168-
Markdown.htmlesc(io, sprint(io->show(IOContext(io, :compact => true),c)))
168+
Markdown.htmlesc(io, sprint(io->show(IOContext(io, :compact => true), c)))
169169
print(io, "</td>")
170170
end
171171
print(io, "</tr>")
172172
end
173173

174-
if rows==nothing
174+
if rows == nothing
175175
row_post_text = "... with more rows."
176176
elseif rows > max_elements
177177
extra_rows = rows - max_elements
178-
row_post_text = "... with $extra_rows more $(extra_rows==1 ? "row" : "rows")."
178+
row_post_text = "... with $extra_rows more $(extra_rows == 1 ? "row" : "rows")."
179179
else
180180
row_post_text = ""
181181
end
@@ -202,45 +202,45 @@ end
202202
Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.dataresource+json")}) = true
203203

204204
julia_type_to_schema_type(::Type{T}) where {T} = "string"
205-
julia_type_to_schema_type(::Type{T}) where {T<:AbstractFloat} = "number"
206-
julia_type_to_schema_type(::Type{T}) where {T<:Integer} = "integer"
207-
julia_type_to_schema_type(::Type{T}) where {T<:Bool} = "boolean"
208-
julia_type_to_schema_type(::Type{T}) where {T<:Dates.Time} = "time"
209-
julia_type_to_schema_type(::Type{T}) where {T<:Dates.Date} = "date"
210-
julia_type_to_schema_type(::Type{T}) where {T<:Dates.DateTime} = "datetime"
211-
julia_type_to_schema_type(::Type{T}) where {T<:AbstractString} = "string"
212-
julia_type_to_schema_type(::Type{T}) where {S, T<:DataValues.DataValue{S}} = julia_type_to_schema_type(S)
205+
julia_type_to_schema_type(::Type{T}) where {T <: AbstractFloat} = "number"
206+
julia_type_to_schema_type(::Type{T}) where {T <: Integer} = "integer"
207+
julia_type_to_schema_type(::Type{T}) where {T <: Bool} = "boolean"
208+
julia_type_to_schema_type(::Type{T}) where {T <: Dates.Time} = "time"
209+
julia_type_to_schema_type(::Type{T}) where {T <: Dates.Date} = "date"
210+
julia_type_to_schema_type(::Type{T}) where {T <: Dates.DateTime} = "datetime"
211+
julia_type_to_schema_type(::Type{T}) where {T <: AbstractString} = "string"
212+
julia_type_to_schema_type(::Type{T}) where {S,T <: DataValues.DataValue{S}} = julia_type_to_schema_type(S)
213213

214214
own_json_formatter(io, x) = JSON.print(io, x)
215-
own_json_formatter(io, x::DataValues.DataValue) = DataValues.isna(x) ? JSON.print(io,nothing) : own_json_formatter(io, x[])
215+
own_json_formatter(io, x::DataValues.DataValue) = DataValues.isna(x) ? JSON.print(io, nothing) : own_json_formatter(io, x[])
216216

217217
function printdataresource(io::IO, source)
218218
if Base.IteratorEltype(source) isa Base.EltypeUnknown
219219
first_el = first(source)
220220
col_names = String.(propertynames(first_el))
221-
col_types = [fieldtype(typeof(first_el), i) for i=1:length(col_names)]
221+
col_types = [fieldtype(typeof(first_el), i) for i = 1:length(col_names)]
222222
else
223223
col_names = String.(fieldnames(eltype(source)))
224-
col_types = [fieldtype(eltype(source), i) for i=1:length(col_names)]
224+
col_types = [fieldtype(eltype(source), i) for i = 1:length(col_names)]
225225
end
226-
schema = Dict("fields" => [Dict("name"=>string(i[1]), "type"=>julia_type_to_schema_type(i[2])) for i in zip(col_names, col_types)])
226+
schema = Dict("fields" => [Dict("name" => string(i[1]), "type" => julia_type_to_schema_type(i[2])) for i in zip(col_names, col_types)])
227227

228228
print(io, "{")
229229
JSON.print(io, "schema")
230230
print(io, ":")
231-
JSON.print(io,schema)
232-
print(io,",")
231+
JSON.print(io, schema)
232+
print(io, ",")
233233
JSON.print(io, "data")
234234
print(io, ":[")
235235

236236
for (row_i, row) in enumerate(source)
237-
if row_i>1
237+
if row_i > 1
238238
print(io, ",")
239239
end
240240

241241
print(io, "{")
242242
for col in 1:length(col_names)
243-
if col>1
243+
if col > 1
244244
print(io, ",")
245245
end
246246
JSON.print(io, col_names[col])

test/runtests.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@ using Dates
55

66
@testset "TableShowUtils" begin
77

8-
source = [(a=1,b="A"),(a=2,b="B")]
8+
source = [(a = 1, b = "A"),(a = 2, b = "B")]
99

10-
@test sprint(TableShowUtils.printtable, source, "foo file") == """
10+
@test sprint(TableShowUtils.printtable, source, "foo file") == """
1111
2x2 foo file
1212
a │ b
1313
──┼──
1414
1 │ A
1515
2 │ B"""
1616

17-
@test sprint(TableShowUtils.printHTMLtable, source) == """
17+
@test sprint(TableShowUtils.printHTMLtable, source) == """
1818
<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>"""
1919

20-
@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"
20+
@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"
2121

22-
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)]
23-
@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"
22+
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)]
23+
@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"
2424

25-
source_with_NA = [(a=1,b="A"),(a=2,b=NA)]
26-
@test sprint(TableShowUtils.printtable, source_with_NA, "foo file") == "2x2 foo file\na │ b \n──┼────\n1 │ A \n2 │ #NA"
25+
source_with_NA = [(a = 1, b = "A"),(a = 2, b = NA)]
26+
@test sprint(TableShowUtils.printtable, source_with_NA, "foo file") == "2x2 foo file\na │ b \n──┼────\n1 │ A \n2 │ #NA"
2727

28-
@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>"
28+
@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>"
2929

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

32-
@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}]}"
32+
@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}]}"
3333

34-
@test TableShowUtils.julia_type_to_schema_type(AbstractFloat) == "number"
35-
@test TableShowUtils.julia_type_to_schema_type(Bool) == "boolean"
36-
@test TableShowUtils.julia_type_to_schema_type(Dates.Time) == "time"
37-
@test TableShowUtils.julia_type_to_schema_type(Dates.Date) == "date"
38-
@test TableShowUtils.julia_type_to_schema_type(Dates.DateTime) == "datetime"
39-
@test TableShowUtils.julia_type_to_schema_type(DataValues.DataValue{Integer}) == "integer"
34+
@test TableShowUtils.julia_type_to_schema_type(AbstractFloat) == "number"
35+
@test TableShowUtils.julia_type_to_schema_type(Bool) == "boolean"
36+
@test TableShowUtils.julia_type_to_schema_type(Dates.Time) == "time"
37+
@test TableShowUtils.julia_type_to_schema_type(Dates.Date) == "date"
38+
@test TableShowUtils.julia_type_to_schema_type(Dates.DateTime) == "datetime"
39+
@test TableShowUtils.julia_type_to_schema_type(DataValues.DataValue{Integer}) == "integer"
4040

4141
end

0 commit comments

Comments
 (0)