@@ -3,18 +3,18 @@ module TableShowUtils
3
3
import JSON, DataValues
4
4
import Markdown, Dates, Unicode
5
5
6
- function printtable (io:: IO , source, typename:: AbstractString ; force_unknown_rows= false )
6
+ function printtable (io:: IO , source, typename:: AbstractString ; force_unknown_rows = false )
7
7
T = eltype (source)
8
8
9
9
if force_unknown_rows
10
10
rows = nothing
11
11
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 }}
13
13
rows = length (source)
14
14
data = Iterators. take (source, 10 ) |> collect
15
15
else
16
16
data_plus_one = Iterators. take (source, 11 ) |> collect
17
- if length (data_plus_one)< 11
17
+ if length (data_plus_one) < 11
18
18
rows = length (data_plus_one)
19
19
data = data_plus_one
20
20
else
@@ -25,18 +25,18 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
25
25
26
26
cols = length (fieldnames (T))
27
27
28
- println (io, " $(rows=== nothing ? " ?" : rows) x$(cols) $typename " )
28
+ println (io, " $(rows === nothing ? " ?" : rows) x$(cols) $typename " )
29
29
30
30
colnames = String .(fieldnames (eltype (source)))
31
31
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]
33
33
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]
35
35
36
36
maxwidth = [maximum (Unicode. textwidth .(data[:,c])) for c in 1 : cols]
37
37
38
38
available_heigth, available_width = displaysize (io)
39
- available_width -= 1
39
+ available_width -= 1
40
40
41
41
shortened_rows = Set {Int} ()
42
42
@@ -47,76 +47,76 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
47
47
return string (s, ' ' ^ m)
48
48
end
49
49
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 )] * " \" "
55
55
push! (shortened_rows, r)
56
56
end
57
57
end
58
58
maxwidth[1 ] = available_width
59
59
break
60
60
else
61
- data = data[:,1 : end - 1 ]
61
+ data = data[:,1 : end - 1 ]
62
62
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 )]
64
64
end
65
65
end
66
66
67
- for c in 1 : size (data,2 )
67
+ for c in 1 : size (data, 2 )
68
68
print (io, rpad (colnames[c], maxwidth[c]))
69
- if c< size (data,2 )
69
+ if c < size (data, 2 )
70
70
print (io, " │ " )
71
71
end
72
72
end
73
73
println (io)
74
- for c in 1 : size (data,2 )
74
+ for c in 1 : size (data, 2 )
75
75
print (io, repeat (" ─" , maxwidth[c]))
76
- if c< size (data,2 )
76
+ if c < size (data, 2 )
77
77
print (io, " ─┼─" )
78
78
end
79
79
end
80
- for r in 2 : size (data,1 )
80
+ for r in 2 : size (data, 1 )
81
81
println (io)
82
- for c in 1 : size (data,2 )
82
+ for c in 1 : size (data, 2 )
83
83
84
84
if r in shortened_rows
85
- print (io, data[r,c],)
85
+ print (io, data[r,c], )
86
86
print (io, " …" )
87
87
else
88
88
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 )
90
90
else
91
91
print (io, textwidth_based_rpad (data[r,c], maxwidth[c]))
92
92
end
93
93
end
94
- if c< size (data,2 )
94
+ if c < size (data, 2 )
95
95
print (io, " │ " )
96
96
end
97
97
end
98
98
end
99
99
100
- if rows=== nothing
100
+ if rows === nothing
101
101
row_post_text = " more rows"
102
- elseif rows > size (data,1 ) - 1
102
+ elseif rows > size (data, 1 ) - 1
103
103
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" ) "
105
105
else
106
106
row_post_text = " "
107
107
end
108
108
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 ]. .. ], " , " )
113
113
else
114
114
col_post_text = " "
115
115
end
116
116
117
117
if ! isempty (row_post_text) || ! isempty (col_post_text)
118
118
println (io)
119
- print (io," ... with " )
119
+ print (io, " ... with " )
120
120
if ! isempty (row_post_text)
121
121
print (io, row_post_text)
122
122
end
@@ -129,18 +129,18 @@ function printtable(io::IO, source, typename::AbstractString; force_unknown_rows
129
129
end
130
130
end
131
131
132
- function printHTMLtable (io, source; force_unknown_rows= false )
132
+ function printHTMLtable (io, source; force_unknown_rows = false )
133
133
colnames = String .(fieldnames (eltype (source)))
134
134
135
135
max_elements = 10
136
136
137
137
if force_unknown_rows
138
138
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 }}
140
140
rows = length (source)
141
141
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
144
144
end
145
145
146
146
haslimit = get (io, :limit , true )
@@ -165,17 +165,17 @@ function printHTMLtable(io, source; force_unknown_rows=false)
165
165
print (io, " <tr>" )
166
166
for c in values (r)
167
167
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)))
169
169
print (io, " </td>" )
170
170
end
171
171
print (io, " </tr>" )
172
172
end
173
173
174
- if rows== nothing
174
+ if rows == nothing
175
175
row_post_text = " ... with more rows."
176
176
elseif rows > max_elements
177
177
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" ) ."
179
179
else
180
180
row_post_text = " "
181
181
end
@@ -202,45 +202,45 @@ end
202
202
Base. Multimedia. istextmime (:: MIME{Symbol("application/vnd.dataresource+json")} ) = true
203
203
204
204
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)
213
213
214
214
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[])
216
216
217
217
function printdataresource (io:: IO , source)
218
218
if Base. IteratorEltype (source) isa Base. EltypeUnknown
219
219
first_el = first (source)
220
220
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)]
222
222
else
223
223
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)]
225
225
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)])
227
227
228
228
print (io, " {" )
229
229
JSON. print (io, " schema" )
230
230
print (io, " :" )
231
- JSON. print (io,schema)
232
- print (io," ," )
231
+ JSON. print (io, schema)
232
+ print (io, " ," )
233
233
JSON. print (io, " data" )
234
234
print (io, " :[" )
235
235
236
236
for (row_i, row) in enumerate (source)
237
- if row_i> 1
237
+ if row_i > 1
238
238
print (io, " ," )
239
239
end
240
240
241
241
print (io, " {" )
242
242
for col in 1 : length (col_names)
243
- if col> 1
243
+ if col > 1
244
244
print (io, " ," )
245
245
end
246
246
JSON. print (io, col_names[col])
0 commit comments