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

Make CoefTable width adjustable #794

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/statmodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,57 @@ function show(io::IO, ct::CoefTable)
if length(rownms) == 0
rownms = [lpad("[$i]",floor(Integer, log10(nr))+3) for i in 1:nr]
end

if !(get(io, :limit, false)::Bool)
screenheight = screenwidth = typemax(Int)
else
sz = displaysize(io)::Tuple{Int,Int}
screenheight, screenwidth = sz[1] - 4, sz[2]
end

sepsize = 3
hdots = " … "
mat = [j == 1 ? NoQuote(rownms[i]) :
j-1 == ct.pvalcol ? NoQuote(sprint(show, PValue(cols[j-1][i]))) :
j-1 in ct.teststatcol ? TestStat(cols[j-1][i]) :
cols[j-1][i] isa AbstractString ? NoQuote(cols[j-1][i]) : cols[j-1][i]
for i in 1:nr, j in 1:nc+1]

# Code inspired by print_matrix in Base
io = IOContext(io, :compact=>true, :limit=>false)
A = Base.alignment(io, mat, 1:size(mat, 1), 1:size(mat, 2),
typemax(Int), typemax(Int), 3)
screenwidth, screenwidth, sepsize)
nmswidths = pushfirst!(length.(colnms), 0)
A = [nmswidths[i] > sum(A[i]) ? (A[i][1]+nmswidths[i]-sum(A[i]), A[i][2]) : A[i]
for i in 1:length(A)]
totwidth = sum(sum.(A)) + 2 * (length(A) - 1)

# remove columns that do not fit on the screen
maxcols = length(A)
if maxcols < nc+1
ncols = min(nc, maxcols)
mat = mat[:, 1:ncols]
mat = hcat(mat, [hdots for i in 1:nr])
colnms = colnms[1:ncols]
sepsize = textwidth(hdots)+1
else
sepsize = 0
end

# print table
totwidth = sum(sum, A) + 2 * (maxcols - 1) + sepsize

println(io, repeat('─', totwidth))
print(io, repeat(' ', sum(A[1])))
for j in 1:length(colnms)
for j in 1:maxcols-1
print(io, " ", lpad(colnms[j], sum(A[j+1])))
end
maxcols < nc+1 && print(io, lpad(hdots, sepsize))
println(io, '\n', repeat('─', totwidth))
for i in 1:size(mat, 1)
Base.print_matrix_row(io, mat, A, i, 1:size(mat, 2), " ")
i != size(mat, 1) && println(io)
m, n = size(mat)
for i in 1:m
Base.print_matrix_row(io, mat, A, i, 1:n, " ")
maxcols < nc+1 && (i+4)%5 == 0 && print(io, lpad(hdots, sepsize))
i != m && println(io)
end
print(io, '\n', repeat('─', totwidth))
nothing
Expand Down
8 changes: 7 additions & 1 deletion test/statmodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4)
df = 0.2422083248151139, p = 0.4530583319523316)
]

ct = CoefTable(rand(15,100), ["c$i" for i in 1:100], [], 4)
sct = sprint(show, ct, context=:limit=>false)
@test textwidth(first(split(sct, '\n'))) > 900
sct = sprint(show, ct, context=:limit=>true)
@test textwidth(first(split(sct, '\n'))) <= displaysize()[2]
Comment on lines +85 to +89
Copy link
Member

Choose a reason for hiding this comment

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

Haven't you found a way to test the exact output as discussed above?

Copy link
Contributor Author

@wildart wildart Oct 9, 2022

Choose a reason for hiding this comment

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

The displaysize() returns current terminal dimensions, what ever they may be in the CI environment. I would assume standard 80x25.

Copy link
Member

Choose a reason for hiding this comment

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

Yes I think it's same to assume that, and check the string output for these dimensions.


@test sprint(show, PValue(1.0)) == "1.0000"
@test sprint(show, PValue(1e-1)) == "0.1000"
if VERSION > v"1.6.0-DEV"
Expand Down Expand Up @@ -190,4 +196,4 @@ end
# Defined but not reexported
@test StatsBase.params isa Function
@test StatsBase.params! isa Function
end
end