Skip to content

Commit

Permalink
Revise show method for timearray (#399)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jjstickel authored and iblislin committed Mar 15, 2019
1 parent 3473522 commit 5cfe95d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/timearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 #################

Expand Down
37 changes: 37 additions & 0 deletions test/timearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 │
├────────────┼────────┤
Expand All @@ -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 │
├────────────┼────────┼────────┼────────┼────────┤
Expand All @@ -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 │
├────────────┼────────┼────────┼────────┼────────┼───────────┼─────────────┤
Expand Down Expand Up @@ -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 │
├────────────┼────────┼────────┼────────┼────────┤
Expand All @@ -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 │
├────────────┼────────┤
Expand Down

0 comments on commit 5cfe95d

Please sign in to comment.