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

Modified show, added omitted columns message #489

Merged
merged 6 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
149 changes: 99 additions & 50 deletions src/timearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ calculate the paging
"""
@inline function _showpages(dcol::Int, timewidth::Int, colwidth::Array{Int})
ret = UnitRange{Int}[]
c = dcol - timewidth - 4
c = dcol - timewidth - 5
last_i = 1
for i in eachindex(colwidth)
w = colwidth[i] + 3
if c - w < 0
push!(ret, last_i:i-1)
# next page
c = dcol - timewidth - 4 - w
c = dcol - timewidth - 5 - w
last_i = i
elseif i == length(colwidth)
push!(ret, last_i:i)
Expand All @@ -220,7 +220,7 @@ calculate the paging
ret
end

function print_time_array(io::IO, ta::TimeArray{T}, short=false) where T
function print_time_array(io::IO, ta::TimeArray{T}, short=false, allcols=false) where T
# summary line
nrow = size(values(ta), 1)
ncol = size(values(ta), 2)
Expand All @@ -237,7 +237,7 @@ function print_time_array(io::IO, ta::TimeArray{T}, short=false) where T

# calculate column withs
drow, dcol = displaysize(io)
res_row = 7 # number of reserved rows: summary line, lable line ... etc
res_row = 9 # number of reserved rows: summary line, label line ... etc
half_row = floor(Int, (drow - res_row) / 2)
add_row = (drow - res_row) % 2

Expand All @@ -247,6 +247,8 @@ function print_time_array(io::IO, ta::TimeArray{T}, short=false) where T
strs = _showval.(@view values(ta)[[tophalf; bothalf], :])
ts = @view timestamp(ta)[[tophalf; bothalf]]
else
tophalf = 0
bothalf = 0
strs = _showval.(values(ta))
ts = timestamp(ta)
end
Expand All @@ -259,63 +261,110 @@ function print_time_array(io::IO, ta::TimeArray{T}, short=false) where T
spacetime = textwidth(string(ts[1]))
pages = _showpages(dcol, spacetime, colwidth)

for p ∈ pages
# row label line
## e.g. | Open | High | Low | Close |
print(io, "│", " "^(spacetime + 2))
for (name, w) in zip(colnames(ta)[p], colwidth[p])
print(io, "│ ", _rpad(name, w + 1))
# print all columns?
if allcols
for p in pages
islastpage = p == last(pages)
_print_page(io, p, ta,
spacetime, colwidth, nrow, drow, res_row, tophalf, bothalf,
islastpage ? 1 : 2)
if length(pages) > 1 && !islastpage
print(io,"\n\n")
end
end
println(io, "│")
## e.g. ├───────┼───────┼───────┼────────┤
print(io, "├", "─"^(spacetime + 2))
for w in colwidth[p]
print(io, "┼", "─"^(w + 2))
else
# print first page and omitted columns message
_print_page(io, pages[1], ta,
spacetime, colwidth, nrow, drow, res_row, tophalf, bothalf,
length(pages))

if length(pages) > 1
pndtcols = last(last(pages)) - first(pages[2]) + 1
println(io)
printstyled(io, lpad("$pndtcols columns omitted", dcol), color=:cyan)
end
print(io, "┤")

# timestamp and values line
if nrow > (drow - res_row)
for i in tophalf
println(io)
print(io, "│ ", ts[i], " ")
for j in p
print(io, "│ ", _rpad(strs[i, j], colwidth[j] + 1))
end
print(io, "│")
end
end
end

"""
_print_page

Helper function to print a single page of the `TimeArray`
"""
function _print_page(io::IO, p::UnitRange{Int}, ta::TimeArray, spacetime,
colwidth, nrow, drow, res_row, tophalf, bothalf, pages)

strs = _showval.(values(ta))
ts = timestamp(ta)
last = pages > 1 ? "│\u22EF" : "│"

# row label line
## e.g. | Open | High | Low | Close |
print(io, "│", " "^(spacetime + 2))
for (name, w) in zip(colnames(ta)[p], colwidth[p])
print(io, "│ ", _rpad(name, w + 1))
end
println(io, last)

print(io, "\n \u22EE")
## e.g. ├───────┼───────┼───────┼────────┤
print(io, "├", "─"^(spacetime + 2))
for w in colwidth[p]
print(io, "┼", "─"^(w + 2))
end
print(io, "┤")

for i in (length(bothalf) - 1):-1:0
i = size(strs, 1) - i
println(io)
print(io, "│ ", ts[i], " ")
for j in p
print(io, "│ ", _rpad(strs[i, j], colwidth[j] + 1))
end
print(io, "│")
# timestamp and values line
if nrow > (drow - res_row)

# print bottom part
for i in tophalf
println(io)
print(io, "│ ", ts[i], " ")
for j in p
print(io, "│ ", _rpad(strs[i, j], colwidth[j] + 1))
end
print(io, i % 3 == 0 ? last : "│")
end

else
for i in 1:nrow
println(io)
print(io, "│ ", ts[i], " ")
for j in p
print(io, "│ ", _rpad(strs[i, j], colwidth[j] + 1))
end
print(io, "│")
# print vdots part
println(io)
print(io, "│ ", _rpad("\u22EE", spacetime + 1))
for j in p
print(io, "│ ", _rpad("\u22EE", colwidth[j] + 1))
end
# print(io, pages > 1 ? "│\u22F1" : "│")
print(io, "│")

# print bottom part
for i in (length(bothalf) - 1):-1:0
i = size(strs, 1) - i
println(io)
print(io, "│ ", ts[i], " ")
for j in p
print(io, "│ ", _rpad(strs[i, j], colwidth[j] + 1))
end
print(io, i % 3 == 0 ? last : "│")
end

if length(pages) > 1 && p != pages[end]
print(io, "\n\n")
else
# print all rows
for i in 1:nrow
println(io)
print(io, "│ ", ts[i], " ")
for j in p
print(io, "│ ", _rpad(strs[i, j], colwidth[j] + 1))
end
print(io, i % 3 == 0 ? last : "│")
end
end # for p ∈ pages
end
end
Base.show(io::IO, ta::TimeArray) = print_time_array(io, ta, true)
Base.show(io::IO, ::MIME"text/plain", ta::TimeArray) =
print_time_array(io, ta, false)

Base.summary(io::IO, ta::TimeArray) = print_time_array(io, ta, true)

Base.show(io::IO, ta::TimeArray) =
print_time_array(io, ta, false, get(io, :limit, true))
Base.show(io::IO, ::MIME"text/plain", ta::TimeArray) =
print_time_array(io, ta, false, !get(io, :limit, false))


###### getindex #################
Expand Down
63 changes: 41 additions & 22 deletions test/timearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ end

@testset "show methods don't throw errors" begin
io = IOBuffer()
let str = sprint(show, cl)
let str = sprint(summary, cl)
out = "500×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2001-12-31"
Copy link
Contributor Author

@r2cp r2cp Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that tests are failing in 1.6 because Array{Float64,1} is now shown as Vector{Float64} and Array{Float64,2} as Matrix{Float64}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in test/timearray.jl we can add a string to interpolate in the output depending on the value of VERSION. Something like:

dispvec = VERSION < v"1.6" ? "Array{Float64,1}" : "Vector{Float64}"

Or maybe even typed:

dispvec(T) = VERSION < v"1.6" ? "Array{$(repr(T)),1}" : "Vector{$(repr(T))}"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, fine. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that this TimeArray{Float64,1,Date,Array{Float64,1}} becomes TimeArray{Float64, 1, Date, Vector{Float64}} in 1.6.

@test str == out
end
Expand All @@ -497,9 +497,7 @@ end
│ 2000-01-10 │ 97.75 │
│ 2000-01-11 │ 92.75 │
│ 2000-01-12 │ 87.19 │
│ 2000-01-13 │ 96.75 │
│ 2001-12-19 │ 21.62 │
│ ⋮ │ ⋮ │
│ 2001-12-20 │ 20.67 │
│ 2001-12-21 │ 21.0 │
│ 2001-12-24 │ 21.36 │
Expand All @@ -511,7 +509,7 @@ end
end

# my edits above seem to work -- now need to do for the rest, JJS 2/22/19
let str = sprint(show, ohlc)
let str = sprint(summary, ohlc)
out = "500×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2001-12-31"
@test str == out
end
Expand All @@ -528,9 +526,7 @@ end
│ 2000-01-10 │ 102.0 │ 102.25 │ 94.75 │ 97.75 │
│ 2000-01-11 │ 95.94 │ 99.38 │ 90.5 │ 92.75 │
│ 2000-01-12 │ 95.0 │ 95.5 │ 86.5 │ 87.19 │
│ 2000-01-13 │ 94.48 │ 98.75 │ 92.5 │ 96.75 │
│ 2001-12-19 │ 20.58 │ 21.68 │ 20.47 │ 21.62 │
│ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │
│ 2001-12-20 │ 21.4 │ 21.47 │ 20.62 │ 20.67 │
│ 2001-12-21 │ 21.01 │ 21.54 │ 20.8 │ 21.0 │
│ 2001-12-24 │ 20.9 │ 21.45 │ 20.9 │ 21.36 │
Expand All @@ -541,31 +537,53 @@ end
@test str == out
end

let str = sprint(show, AAPL)
let str = sprint(summary, AAPL)
out = "8336×12 TimeArray{Float64,2,Date,Array{Float64,2}} 1980-12-12 to 2013-12-31"
@test str == out
end
show(IOContext(io, :limit => false), AAPL)
let str = String(take!(io))
out = """8336×12 TimeArray{Float64,2,Date,Array{Float64,2}} 1980-12-12 to 2013-12-31
│ │ Open │ High │ Low │ Close │ Volume │ ExDividend │⋯
├────────────┼────────┼────────┼────────┼────────┼───────────┼────────────┤
│ 1980-12-12 │ 28.75 │ 28.88 │ 28.75 │ 28.75 │ 2.0939e6 │ 0.0 │
│ 1980-12-15 │ 27.38 │ 27.38 │ 27.25 │ 27.25 │ 785200.0 │ 0.0 │
│ 1980-12-16 │ 25.38 │ 25.38 │ 25.25 │ 25.25 │ 472000.0 │ 0.0 │⋯
│ 1980-12-17 │ 25.88 │ 26.0 │ 25.88 │ 25.88 │ 385900.0 │ 0.0 │
│ 1980-12-18 │ 26.62 │ 26.75 │ 26.62 │ 26.62 │ 327900.0 │ 0.0 │
│ 1980-12-19 │ 28.25 │ 28.38 │ 28.25 │ 28.25 │ 217100.0 │ 0.0 │⋯
│ 1980-12-22 │ 29.62 │ 29.75 │ 29.62 │ 29.62 │ 166800.0 │ 0.0 │
│ 1980-12-23 │ 30.88 │ 31.0 │ 30.88 │ 30.88 │ 209600.0 │ 0.0 │
│ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │
│ 2013-12-20 │ 545.43 │ 551.61 │ 544.82 │ 549.02 │ 1.55862e7 │ 0.0 │
│ 2013-12-23 │ 568.0 │ 570.72 │ 562.76 │ 570.09 │ 1.79038e7 │ 0.0 │⋯
│ 2013-12-24 │ 569.89 │ 571.88 │ 566.03 │ 567.67 │ 5.9841e6 │ 0.0 │
│ 2013-12-26 │ 568.1 │ 569.5 │ 563.38 │ 563.9 │ 7.286e6 │ 0.0 │
│ 2013-12-27 │ 563.82 │ 564.41 │ 559.5 │ 560.09 │ 8.0673e6 │ 0.0 │⋯
│ 2013-12-30 │ 557.46 │ 560.09 │ 552.32 │ 554.52 │ 9.0582e6 │ 0.0 │
│ 2013-12-31 │ 554.17 │ 561.28 │ 554.0 │ 561.02 │ 7.9673e6 │ 0.0 │
5 columns omitted"""
@test str == out
end
show(io, "text/plain", AAPL)
let str = String(take!(io))
out = """8336×12 TimeArray{Float64,2,Date,Array{Float64,2}} 1980-12-12 to 2013-12-31
│ │ Open │ High │ Low │ Close │ Volume │ ExDividend │
│ │ Open │ High │ Low │ Close │ Volume │ ExDividend │
├────────────┼────────┼────────┼────────┼────────┼───────────┼────────────┤
│ 1980-12-12 │ 28.75 │ 28.88 │ 28.75 │ 28.75 │ 2.0939e6 │ 0.0 │
│ 1980-12-15 │ 27.38 │ 27.38 │ 27.25 │ 27.25 │ 785200.0 │ 0.0 │
│ 1980-12-16 │ 25.38 │ 25.38 │ 25.25 │ 25.25 │ 472000.0 │ 0.0 │
│ 1980-12-16 │ 25.38 │ 25.38 │ 25.25 │ 25.25 │ 472000.0 │ 0.0 │
│ 1980-12-17 │ 25.88 │ 26.0 │ 25.88 │ 25.88 │ 385900.0 │ 0.0 │
│ 1980-12-18 │ 26.62 │ 26.75 │ 26.62 │ 26.62 │ 327900.0 │ 0.0 │
│ 1980-12-19 │ 28.25 │ 28.38 │ 28.25 │ 28.25 │ 217100.0 │ 0.0 │
│ 1980-12-19 │ 28.25 │ 28.38 │ 28.25 │ 28.25 │ 217100.0 │ 0.0 │
│ 1980-12-22 │ 29.62 │ 29.75 │ 29.62 │ 29.62 │ 166800.0 │ 0.0 │
│ 1980-12-23 │ 30.88 │ 31.0 │ 30.88 │ 30.88 │ 209600.0 │ 0.0 │
│ 1980-12-24 │ 32.5 │ 32.62 │ 32.5 │ 32.5 │ 214300.0 │ 0.0 │
│ 2013-12-19 │ 549.5 │ 550.0 │ 543.73 │ 544.46 │ 1.14396e7 │ 0.0 │
│ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │
│ 2013-12-20 │ 545.43 │ 551.61 │ 544.82 │ 549.02 │ 1.55862e7 │ 0.0 │
│ 2013-12-23 │ 568.0 │ 570.72 │ 562.76 │ 570.09 │ 1.79038e7 │ 0.0 │
│ 2013-12-23 │ 568.0 │ 570.72 │ 562.76 │ 570.09 │ 1.79038e7 │ 0.0 │
│ 2013-12-24 │ 569.89 │ 571.88 │ 566.03 │ 567.67 │ 5.9841e6 │ 0.0 │
│ 2013-12-26 │ 568.1 │ 569.5 │ 563.38 │ 563.9 │ 7.286e6 │ 0.0 │
│ 2013-12-27 │ 563.82 │ 564.41 │ 559.5 │ 560.09 │ 8.0673e6 │ 0.0 │
│ 2013-12-27 │ 563.82 │ 564.41 │ 559.5 │ 560.09 │ 8.0673e6 │ 0.0 │
│ 2013-12-30 │ 557.46 │ 560.09 │ 552.32 │ 554.52 │ 9.0582e6 │ 0.0 │
│ 2013-12-31 │ 554.17 │ 561.28 │ 554.0 │ 561.02 │ 7.9673e6 │ 0.0 │

Expand All @@ -579,9 +597,7 @@ end
│ 1980-12-19 │ 1.0 │ 3.3179 │ 3.3331 │ 3.3179 │ 3.3179 │
│ 1980-12-22 │ 1.0 │ 3.4788 │ 3.494 │ 3.4788 │ 3.4788 │
│ 1980-12-23 │ 1.0 │ 3.6267 │ 3.6408 │ 3.6267 │ 3.6267 │
│ 1980-12-24 │ 1.0 │ 3.817 │ 3.8311 │ 3.817 │ 3.817 │
│ 2013-12-19 │ 1.0 │ 546.2492 │ 546.7463 │ 540.5133 │ 541.239 │
│ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │
│ 2013-12-20 │ 1.0 │ 542.2033 │ 548.3467 │ 541.5969 │ 545.7721 │
│ 2013-12-23 │ 1.0 │ 564.6398 │ 567.3437 │ 559.4308 │ 566.7174 │
│ 2013-12-24 │ 1.0 │ 566.5186 │ 568.4968 │ 562.6814 │ 564.3117 │
Expand All @@ -592,7 +608,7 @@ end
@test str == out
end

let str = sprint(show, ohlc[1:4])
let str = sprint(summary, ohlc[1:4])
out = "4×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-06"
@test str == out
end
Expand All @@ -612,6 +628,9 @@ end
let str = sprint(show, ohlc[1:0])
@test str == "0×4 TimeArray{Float64,2,Date,Array{Float64,2}}"
end
let str = sprint(summary, ohlc[1:0])
@test str == "0×4 TimeArray{Float64,2,Date,Array{Float64,2}}"
end
show(io, "text/plain", ohlc[1:0])
let str = String(take!(io))
@test str == "0×4 TimeArray{Float64,2,Date,Array{Float64,2}}"
Expand All @@ -626,7 +645,7 @@ end
@test str == "0×1 TimeArray{Any,1,Date,Array{Any,1}}"
end

let str = sprint(show, lag(cl[1:2], padding=true))
let str = sprint(summary, lag(cl[1:2], padding=true))
out = "2×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2000-01-04"
@test str == out
end
Expand Down