Skip to content

Commit

Permalink
Changes to PrettyTables printing (#520)
Browse files Browse the repository at this point in the history
closes #514
closes #428
  • Loading branch information
ValentinKaisermayer authored Nov 21, 2023
1 parent 6db8220 commit c286db1
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 274 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
fail-fast: false
matrix:
version:
- "1.0" # LTS
- "1.5"
- "1.6.7" # LTS
- "1.6"
- "1" # Latest Release
os:
- ubuntu-latest
Expand All @@ -25,7 +25,7 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
Expand Down Expand Up @@ -54,9 +54,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@latest
with:
version: '1.5'
version: '1'
- run: |
git config --global user.name name
git config --global user.email email
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.23.2"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Expand All @@ -20,7 +21,8 @@ RecipesBase = "0.5, 0.7, 0.8, 1.0"
Reexport = "1"
Statistics = "1"
Tables = "1"
julia = "1"
julia = "1.6"
PrettyTables = "2"

[extras]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e"

[compat]
Documenter = "~0.24"
Documenter = "1"
15 changes: 8 additions & 7 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ using TimeSeries


makedocs(
format = Documenter.HTML(),
sitename = "TimeSeries.jl",
modules = [TimeSeries],
pages = [
format=Documenter.HTML(; prettyurls=(get(ENV, "CI", nothing) == "true")),
sitename="TimeSeries.jl",
modules=[TimeSeries],
warnonly=true, # some docstrings are not in the manual
pages=[
"index.md",
"getting_started.md",
"timearray.md",
Expand All @@ -24,7 +25,7 @@ makedocs(
)

deploydocs(
repo = "github.com/JuliaStats/TimeSeries.jl.git",
devbranch = "master",
push_preview = true,
repo="github.com/JuliaStats/TimeSeries.jl.git",
devbranch="master",
push_preview=true,
)
1 change: 1 addition & 0 deletions src/TimeSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using DocStringExtensions: SIGNATURES
using RecipesBase
using Reexport
using Tables
using PrettyTables: pretty_table

export TimeArray, AbstractTimeSeries,
when, from, to, findwhen, timestamp, values, colnames, meta, head, tail,
Expand Down
189 changes: 26 additions & 163 deletions src/timearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,186 +186,49 @@ Base.eltype(::AbstractTimeSeries{T,1,D}) where {T,D} = Tuple{D,T}
Base.eltype(::AbstractTimeSeries{T,2,D}) where {T,D} = Tuple{D,Vector{T}}

###### show #####################
Base.summary(io::IO, ta::TimeArray) = show(io, ta)

@inline _showval(v::Any) = repr(v)
@inline _showval(v::Number) = string(v)
@inline _showval(v::AbstractFloat) =
ifelse(isnan(v), MISSING, string(round(v, digits=DECIMALS)))

"""
calculate the paging
```
> using MarketData
> AAPL # this function will return `UnitRange{Int64}[1:9, 10:12]`
```
"""
@inline function _showpages(dcol::Int, timewidth::Int, colwidth::Array{Int})
ret = UnitRange{Int}[]
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 - 5 - w
last_i = i
elseif i == length(colwidth)
push!(ret, last_i:i)
else
c -= w
end
end
ret
end

function print_time_array(io::IO, ta::TimeArray{T}, short=false, allcols=false) where T
# summary line
function Base.show(io::IO, ta::TimeArray)
nrow = size(values(ta), 1)
ncol = size(values(ta), 2)

print(io, "$(nrow)×$(ncol) $(typeof(ta))")
if nrow != 0
print(io, " $(timestamp(ta)[1]) to $(timestamp(ta)[end])")
else # e.g. TimeArray(Date[], [])
return
end

short && return
println(io)

# calculate column widths
drow, dcol = displaysize(io)
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

if nrow > (drow - res_row)
tophalf = 1:(half_row + add_row)
bothalf = (nrow - half_row + 1):nrow
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

colwidth = maximum(
[textwidth.(string.(colnames(ta)))'; textwidth.(strs); fill(5, ncol)'],
dims = 1)

# paging
spacetime = textwidth(string(ts[1]))
pages = _showpages(dcol, spacetime, colwidth)

# 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
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
end
end
function Base.show(io::IO, ::MIME"text/plain", ta::TimeArray; allrows = !get(io, :limit, false), allcols = !get(io, :limit, false))
nrow = size(values(ta), 1)
ncol = size(values(ta), 2)

"""
_print_page
show(io, ta) # summary line

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)
nrow == 0 && return

## e.g. ├───────┼───────┼───────┼────────┤
print(io, "", ""^(spacetime + 2))
for w in colwidth[p]
print(io, "", ""^(w + 2))
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

# 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
println(io)

if allcols && allrows
crop = :none
elseif allcols
crop = :vertical
elseif allrows
crop = :horizontal
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
crop = :both
end
end

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))

data = hcat(timestamp(ta), values(ta))
header = vcat("", string.(colnames(ta)))
pretty_table(io, data;
header=header,
newline_at_end = false,
reserved_display_lines = 2,
row_label_alignment = :r,
header_alignment = :l,
crop=crop,
)
end

###### getindex #################

Expand Down
Loading

0 comments on commit c286db1

Please sign in to comment.