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

remove dependency on CategoricalArrays.jl in legacy show #2427

Merged
merged 8 commits into from
Oct 13, 2020
Merged
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
42 changes: 11 additions & 31 deletions src/abstractdataframe/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,25 @@ end
"""
DataFrames.ourshow(io::IO, x::Any, truncstring::Int)

Render a value to an `IO` object compactly and omitting type information, by
calling 3-argument `show`, or 2-argument `show` if the former contains line breaks.
Unlike `show`, render strings without surrounding quote marks.
Render a value to an `IO` object compactly using print.
`truncstring` indicates the approximate number of text characters width to truncate
the output (if it is a non-positive value then no truncation is applied).
"""
function ourshow(io::IO, x::Any, truncstring::Int; styled::Bool=false)
io_ctx = IOContext(io, :compact=>get(io, :compact, true), :typeinfo=>typeof(x))

# This mirrors the behavior of Base.print_matrix_row
# First try 3-arg show
sx = sprint(show, "text/plain", x, context=io_ctx)

# If the output contains line breaks, try 2-arg show instead.
if occursin('\n', sx)
sx = sprint(show, x, context=io_ctx)
end

# strings should have " stripped here
if x isa AbstractString
@assert sx[1] == sx[end] == '"'
sx = escape_string(chop(sx, head=1, tail=1), "")
end

sx = sprint(print, x, context=io_ctx)
sx = escape_string(sx, ()) # do not escape "
sx = truncatestring(sx, truncstring)

if styled
printstyled(io_ctx, sx, color=:light_black)
else
print(io_ctx, sx)
end
styled ? printstyled(io_ctx, sx, color=:light_black) : print(io_ctx, sx)
end

const SHOW_TABULAR_TYPES = Union{AbstractDataFrame, DataFrameRow, DataFrameRows,
DataFrameColumns, GroupedDataFrame}

ourshow(io::IO, x::AbstractString, truncstring::Int) =
escape_string(io, truncatestring(x, truncstring), "")
ourshow(io::IO, x::CategoricalValue{<:AbstractString}, truncstring::Int) =
ourshow(io, get(x), truncstring)
ourshow(io::IO, x::Symbol, truncstring::Int) = ourshow(io, string(x), truncstring)
# workaround Julia 1.0 for Char
ourshow(io::IO, x::Char, truncstring::Int; styled::Bool=false) =
ourshow(io, string(x), styled=styled, truncstring)

ourshow(io::IO, x::Nothing, truncstring::Int; styled::Bool=false) =
ourshow(io, "", styled=styled, truncstring)
ourshow(io::IO, x::SHOW_TABULAR_TYPES, truncstring::Int; styled::Bool=false) =
Expand Down Expand Up @@ -113,7 +91,9 @@ function compacttype(T::Type, maxwidth::Int=8, initial::Bool=true)

maxwidth -= 1 # we will add "…" at the end

if T <: CategoricalValue
# This is only type display shortening so we
# are OK with any T whose name starts with CategoricalValue here
if startswith(sT, "CategoricalValue") || startswith(sT, "CategoricalArrays.CategoricalValue")
sT = string(nameof(T))
if textwidth(sT) ≤ maxwidth
return sT * "…" * suffix
Expand Down
8 changes: 4 additions & 4 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ end
│ Row │ A │ B │ C │ D │
│ │ Int64 │ String │ Float32 │ Char │
├─────┼───────┼─────────────┼─────────┼──────┤
│ 1 │ 1 │ x" │ 1.0 │ '\\''
│ 2 │ 2 │ ∀ε>0: x+ε>x │ 2.0 │ '∀'
│ 3 │ 3 │ z\$ │ 3.0 │ '\$'
│ 4 │ 4 │ A\\nC │ 4.0 │ '\\n' │"""
│ 1 │ 1 │ x" │ 1.0 │ '
│ 2 │ 2 │ ∀ε>0: x+ε>x │ 2.0 │
│ 3 │ 3 │ z\$ │ 3.0 │ \$
│ 4 │ 4 │ A\\nC │ 4.0 │ \\n │"""

for allrows in [true, false], allcols in [true, false]
io = IOBuffer()
Expand Down