From 5cfe95d8c6405eac4c085f0411dbca6dcc159b52 Mon Sep 17 00:00:00 2001 From: Jonathan Stickel Date: Thu, 14 Mar 2019 19:36:26 -0600 Subject: [PATCH] Revise show method for timearray (#399) * Revise show method for timearray Now a one-line short version will be output (when no MIME type is specified). The previous long version will be output for ::MIME"text/plain", e.g., when printing in the REPL. This commit does not include changes to the indenting in order to show only the functional changes. --- src/timearray.jl | 11 +++++++++-- test/timearray.jl | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/timearray.jl b/src/timearray.jl index e74e59fd..edef03be 100644 --- a/src/timearray.jl +++ b/src/timearray.jl @@ -208,18 +208,21 @@ calculate the paging ret end -function show(io::IO, ta::TimeArray{T}) where T +function print_time_array(io::IO, ta::TimeArray{T}, short=false) where T # summary line nrow = size(values(ta), 1) ncol = size(values(ta), 2) print(io, "$(nrow)×$(ncol) $(typeof(ta))") if nrow != 0 - println(io, " $(timestamp(ta)[1]) to $(timestamp(ta)[end])") + print(io, " $(timestamp(ta)[1]) to $(timestamp(ta)[end])") else # e.g. TimeArray(Date[], []) return end + short && return + println(io) + # calculate column withs drow, dcol = displaysize(io) res_row = 7 # number of reserved rows: summary line, lable line ... etc @@ -298,6 +301,10 @@ function show(io::IO, ta::TimeArray{T}) where T end end # for p ∈ pages 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) + ###### getindex ################# diff --git a/test/timearray.jl b/test/timearray.jl index 4a337e1c..743aaf3b 100644 --- a/test/timearray.jl +++ b/test/timearray.jl @@ -428,7 +428,13 @@ end @testset "show methods don't throw errors" begin + io = IOBuffer() let str = sprint(show, cl) + out = "500×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2001-12-31" + @test str == out + end + show(io, "text/plain", cl) + let str = String(take!(io)) out = """500×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2001-12-31 │ │ Close │ ├────────────┼────────┤ @@ -453,7 +459,13 @@ end @test str == out end + # my edits above seem to work -- now need to do for the rest, JJS 2/22/19 let str = sprint(show, ohlc) + out = "500×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2001-12-31" + @test str == out + end + show(io, "text/plain", ohlc) + let str = String(take!(io)) out = """500×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2001-12-31 │ │ Open │ High │ Low │ Close │ ├────────────┼────────┼────────┼────────┼────────┤ @@ -479,6 +491,11 @@ end end let str = sprint(show, AAPL) + out = "8336×12 TimeArray{Float64,2,Date,Array{Float64,2}} 1980-12-12 to 2013-12-31" + @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 │ Ex-Dividend │ ├────────────┼────────┼────────┼────────┼────────┼───────────┼─────────────┤ @@ -525,6 +542,11 @@ end end let str = sprint(show, ohlc[1:4]) + out = "4×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-06" + @test str == out + end + show(io, "text/plain", ohlc[1:4]) + let str = String(take!(io)) out = """4×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-06 │ │ Open │ High │ Low │ Close │ ├────────────┼────────┼────────┼────────┼────────┤ @@ -535,15 +557,30 @@ end @test str == out end + let str = sprint(show, 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}}" + end + let str = sprint(show, TimeArray(Date[], [])) @test str == "0×1 TimeArray{Any,1,Date,Array{Any,1}}" end + show(io, "text/plain", TimeArray(Date[], [])) + let str = String(take!(io)) + @test str == "0×1 TimeArray{Any,1,Date,Array{Any,1}}" + end let str = sprint(show, 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 + show(io, "text/plain", lag(cl[1:2], padding=true)) + let str = String(take!(io)) out = """2×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2000-01-04 │ │ Close │ ├────────────┼────────┤