Skip to content

Commit

Permalink
Check for IOContext[:limit] in show(), fixes #56
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Nov 23, 2017
1 parent 1159859 commit a80c4ba
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: julia
julia:
- 0.5
- 0.6
before_install:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.5
julia 0.6
Combinatorics 0.2.1
DataStructures 0.4.4
Compat 0.18
17 changes: 17 additions & 0 deletions src/indexedtables.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using IndexedTables

import IndexedTables.IndexedTable

function IndexedTable(n::NamedArray)
L = length(n) # elements in array
cols = Dict{Symbol, Array}()
factor = 1
for d in 1:ndims(n)
nlevels = size(n, d)
nrep = L ÷ (nlevels * factor)
data = repmat(vcat([fill(x, factor) for x in names(n, d)]...), nrep)
cols[Symbol(dimnames(n, d))] = data
factor *= nlevels
end
return IndexedTable(Columns(;cols...), array(n)[:])
end
4 changes: 2 additions & 2 deletions src/namedarraytypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using Compat
## The inner constructor checks for consistency, the values must all be 1:d
if !isdefined(:NamedArray)

type NamedArray{T,N,AT,DT} <: AbstractArray{T,N}
mutable struct NamedArray{T,N,AT,DT} <: AbstractArray{T,N}
array::AT
dicts::DT
dimnames::NTuple{N, Any}
Expand All @@ -28,7 +28,7 @@ end


## a type that negates any index
immutable Not{T}
struct Not{T}
index::T
end

Expand Down
36 changes: 28 additions & 8 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ Base.show(io::IO, ::MIME"text/plain", n::NamedArray) = show(io, n)
function show(io::IO, n::NamedArray)
print(io, summary(n))
s = size(n)
limit = get(io, :limit, true)
if ndims(n) == 0
println(io)
show(io, n.array[1])
elseif ndims(n) == 2
maxnrow = displaysize(io)[1] - 5 # summary, header, dots, + 2 empty lines...
println(io)
show(io, n, min(maxnrow, s[1]))
if limit
maxnrow = displaysize(io)[1] - 5 # summary, header, dots, + 2 empty lines...
show(io, n, min(maxnrow, s[1]))
else
show(io, n, s[1])
end
else
nlinesneeded = prod(s[3:end]) * (s[1] + 3) + 1
if nlinesneeded > displaysize(io)[1]
if limit && nlinesneeded > displaysize(io)[1]
maxnrow = clamp((displaysize(io)[1] - 3) ÷ (prod(s[3:end])) - 3, 3, s[1])
else
maxnrow = s[1]
Expand All @@ -60,9 +65,14 @@ end

function show(io::IO, v::NamedVector)
println(io, summary(v))
limit = get(io, :limit, true)
if size(v) != (0,)
maxnrow = displaysize(io)[1] - 5
show(io, v, min(maxnrow, length(v)))
if limit
maxnrow = displaysize(io)[1] - 5
show(io, v, min(maxnrow, length(v)))
else
show(io, v, length(v))
end
end
end

Expand Down Expand Up @@ -93,14 +103,19 @@ end
function show(io::IO, n::NamedMatrix, maxnrow::Int)
@assert ndims(n)==2
nrow, ncol = size(n)
limit = get(io, :limit, true)
## rows
rowrange, totrowrange = compute_range(maxnrow, nrow)
s = [sprint(showcompact, n.array[i,j]) for i=totrowrange, j=1:ncol]
rowname, colname = strnames(n)
strlen(x) = length(string(x))
colwidth = max(maximum(map(length, s)), maximum(map(strlen, colname)))
rownamewidth = max(maximum(map(strlen, rowname)), sum(map(length, strdimnames(n)))+3)
maxncol = div(displaysize(io)[2] - rownamewidth - 4, colwidth+2) # dots, spaces between
if limit
maxncol = div(displaysize(io)[2] - rownamewidth - 4, colwidth+2) # dots, spaces between
else
maxncol = ncol
end
## columns
colrange, totcorange = compute_range(maxncol, ncol)
## header
Expand Down Expand Up @@ -138,8 +153,13 @@ function show{T1,T2}(io::IO, n::NamedArray{T1,2,SparseMatrixCSC{T1,T2}})
if nnz(S) != 0
print(io, S.m, "×", S.n, " Named sparse matrix with ", nnz(S), " ", eltype(S), " nonzero entries", nnz(S) == 0 ? "" : ":")
end
maxnrow = displaysize(io)[1]
half_screen_rows = div(maxnrow - 5, 2)
limit = get(io, :limit, true)
if limit
maxnrow = displaysize(io)[1]
half_screen_rows = div(maxnrow - 5, 2)
else
half_screen_rows = typemax(Int)
end
rownames, colnames = strnames(n)
rowpad = maximum([length(s) for s in rownames])
colpad = maximum([length(s) for s in colnames])
Expand Down
16 changes: 13 additions & 3 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ println("show,")

include("init-namedarrays.jl")

function showlines(x...)
function showlines(x...; kwargs...)
buf = IOBuffer()
show(buf, x...)
show(IOContext(buf; kwargs...), x...)
return split(String(buf), "\n")
end

Expand Down Expand Up @@ -95,9 +95,19 @@ lines = showlines(NamedArray(sprand(1000,1000, 1e-4), (nms, nms)))
@test endswith(lines[1], "Float64 nonzero entries:")
@test sum([contains(line, "") for line in lines]) == 1

# array with Nullable names
## array with Nullable names
lines = showlines(NamedArray(rand(2, 2), (Nullable["a", Nullable()], Nullable["c", "d"])))
@test lines[1] == "2×2 Named Array{Float64,2}"
@test split(lines[2]) == ["A", "", "B", "", "\"c\"", "\"d\""]
@test startswith(lines[4], "\"a\"")
@test startswith(lines[5], "#NULL")

## no limits
for dims in [(1000,), (1000, 2)]
lines = showlines(NamedArray(rand(dims...)), limit=false)
@test length(lines) == 1003
@test startswith(lines[end-500], "500 ")
end
lines = showlines(NamedArray(rand(10, 1000)), limit=false)
@test length(split(lines[2])) == 1004 ## "A" "╲" "B" "│" ...
@test length(split(lines[end])) == 1002 # "10" "│" ...

0 comments on commit a80c4ba

Please sign in to comment.