Skip to content

Commit

Permalink
Merge 409b8b4 into 9a292ff
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Oct 11, 2020
2 parents 9a292ff + 409b8b4 commit 8db775b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
44 changes: 12 additions & 32 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)
function ourshow(io::IO, x::T, truncstring::Int; styled::Bool=false) where T
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 Categorical here
if startswith(sT, "Categorical")
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

0 comments on commit 8db775b

Please sign in to comment.