Skip to content

Commit 75c3b98

Browse files
committed
make CoefTable width adjustable
1 parent 1815e1e commit 75c3b98

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/statmodels.jl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,54 @@ function show(io::IO, ct::CoefTable)
120120
if length(rownms) == 0
121121
rownms = [lpad("[$i]",floor(Integer, log10(nr))+3) for i in 1:nr]
122122
end
123+
124+
if !(get(io, :limit, false)::Bool)
125+
screenheight = screenwidth = typemax(Int)
126+
else
127+
sz = displaysize(io)::Tuple{Int,Int}
128+
screenheight, screenwidth = sz[1] - 4, sz[2]
129+
end
130+
131+
sepsize = 3
132+
hdots = " \u2026 "
123133
mat = [j == 1 ? NoQuote(rownms[i]) :
124134
j-1 == ct.pvalcol ? NoQuote(sprint(show, PValue(cols[j-1][i]))) :
125135
j-1 in ct.teststatcol ? TestStat(cols[j-1][i]) :
126136
cols[j-1][i] isa AbstractString ? NoQuote(cols[j-1][i]) : cols[j-1][i]
127137
for i in 1:nr, j in 1:nc+1]
138+
128139
# Code inspired by print_matrix in Base
129140
io = IOContext(io, :compact=>true, :limit=>false)
130141
A = Base.alignment(io, mat, 1:size(mat, 1), 1:size(mat, 2),
131-
typemax(Int), typemax(Int), 3)
142+
screenwidth, screenwidth, sepsize)
132143
nmswidths = pushfirst!(length.(colnms), 0)
133144
A = [nmswidths[i] > sum(A[i]) ? (A[i][1]+nmswidths[i]-sum(A[i]), A[i][2]) : A[i]
134145
for i in 1:length(A)]
135-
totwidth = sum(sum.(A)) + 2 * (length(A) - 1)
146+
147+
# remove columns that do not fit on the screen
148+
maxcols = length(A)
149+
totwidth = sum(sum.(A)) + 2 * (length(A) - 1)+sepsize
150+
if maxcols < nc+1
151+
ncols = min(nc, maxcols)
152+
mat = mat[:, 1:ncols]
153+
colnms = colnms[1:ncols]
154+
sepsize = length(hdots)+1
155+
else
156+
sepsize = 0
157+
end
158+
159+
# print table
160+
totwidth = sum(sum.(A)) + 2 * (length(A) - 1)+sepsize
136161
println(io, repeat('', totwidth))
137162
print(io, repeat(' ', sum(A[1])))
138-
for j in 1:length(colnms)
163+
for j in 1:maxcols-1
139164
print(io, " ", lpad(colnms[j], sum(A[j+1])))
140165
end
166+
maxcols < nc+1 && print(io, lpad(hdots, sepsize))
141167
println(io, '\n', repeat('', totwidth))
142168
for i in 1:size(mat, 1)
143169
Base.print_matrix_row(io, mat, A, i, 1:size(mat, 2), " ")
170+
maxcols < nc+1 && print(io, lpad(hdots, sepsize))
144171
i != size(mat, 1) && println(io)
145172
end
146173
print(io, '\n', repeat('', totwidth))

test/statmodels.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4)
8282
df = 0.2422083248151139, p = 0.4530583319523316)
8383
]
8484

85+
ct = CoefTable(rand(1,100), ["c$i" for i in 1:100], [], 4)
86+
sct = sprint(show, ct, context=:limit=>false)
87+
@test length(first(split(sct, \'n'))) > 1000
88+
sct = sprint(show, ct, context=:limit=>true)
89+
@test length(first(split(sct, \'n'))) < 200
90+
8591
@test sprint(show, PValue(1.0)) == "1.0000"
8692
@test sprint(show, PValue(1e-1)) == "0.1000"
8793
if VERSION > v"1.6.0-DEV"
@@ -190,4 +196,4 @@ end
190196
# Defined but not reexported
191197
@test StatsBase.params isa Function
192198
@test StatsBase.params! isa Function
193-
end
199+
end

0 commit comments

Comments
 (0)