Skip to content

Commit

Permalink
Merge 7480f97 into 47e75ea
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jan 24, 2022
2 parents 47e75ea + 7480f97 commit 4e1cede
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 19 deletions.
84 changes: 65 additions & 19 deletions src/DisplayAs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,60 @@ for (name, mime) in _showables
@eval @doc $doc const $name = @mime_str $mime
end

# Internal type to bundle arguments for IOContext
struct IOContextCarrier
content
context::Dict{Symbol,Any}
end

"""
Unlimited
DisplayAs.setcontext(obj, kvs::Pair...)
Unlimit display size. Useful for, e.g., printing all contents of
dataframes in a Jupyter notebook.
Bundle arguments for `IOContext` with the object `x`.
# Examples
```julia-repl
julia> import DisplayAs
julia> data = rand(2, 2)
2×2 Array{Float64,2}:
0.786992 0.576265
0.321868 0.791263
julia> DisplayAs.setcontext(data, :compact => false)
2×2 Array{Float64,2}:
0.7869920812675713 0.5762653628115182
0.32186846202784314 0.791263230914472
```
julia> using DisplayAs, VegaDatasets
julia> data = dataset("cars");
See also [`DisplayAs.withcontext`](@ref).
"""
setcontext(obj, kvs::Pair...) = IOContextCarrier(obj, Dict{Symbol,Any}(kvs...))

julia> data |> DisplayAs.Unlimited
"""
DisplayAs.withcontext(kvs::Pair...)
Convenience method equivalent to `obj -> DisplayAs.setcontext(obj, kvs...)`
useful for "piping".
# Examples
```julia-repl
julia> import DisplayAs
julia> rand(2, 2) |> DisplayAs.withcontext(:compact => false)
2×2 Array{Float64,2}:
0.7869920812675713 0.5762653628115182
0.32186846202784314 0.791263230914472
```
See also [`DisplayAs.withcontext`](@ref).
"""
struct Unlimited
content
end
withcontext(kvs::Pair...) = obj -> setcontext(obj, kvs...)

Base.showable(::MIME{mime}, x::Unlimited) where {mime} =
Base.showable(::MIME{mime}, x::IOContextCarrier) where {mime} =
hasmethod(show, Tuple{IO, MIME{mime}, typeof(x)}) &&
showable(MIME(mime), x.content)

unlimit(io) = IOContext(
io,
:compact => false,
:limit => false,
:displaysize => (typemax(Int), typemax(Int)),
)

_textmimes = [m for (_, m) in _showables if startswith(m, "text/")]
_textmimes = [
_textmimes
Expand All @@ -160,8 +184,30 @@ _textmimes = [

for mime in _textmimes
mimetype = MIME{Symbol(mime)}
@eval Base.show(io::IO, ::$mimetype, obj::Unlimited) =
show(unlimit(io), $mimetype(), obj.content)
@eval function Base.show(io::IO, ::$mimetype, obj::IOContextCarrier)
ioc = IOContext(io, obj.context...)
show(ioc, $mimetype(), obj.content)
end
end

"""
Unlimited
Unlimit display size. Useful for, e.g., printing all contents of
dataframes in a Jupyter notebook.
# Examples
```
julia> using DisplayAs, VegaDatasets
julia> data = dataset("cars");
julia> data |> DisplayAs.Unlimited
```
"""
function Unlimited(x)
setcontext(x, :compact => false, :limit => false,
:displaysize => (typemax(Int), typemax(Int)))
end

end # module
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
using DisplayAs
using Test

struct PrintContext end
function Base.show(io::IO, ::MIME"text/plain", ::PrintContext)
print(io, get(io, :compact, "-"), "/")
print(io, get(io, :limit, "-"), "/")
print(io, get(io, :displaysize, "-"))
end

@testset "DisplayAs.jl" begin
@test showable("text/plain", DisplayAs.Text(nothing))
@test showable("text/html", DisplayAs.HTML(nothing))
Expand All @@ -10,6 +17,22 @@ using Test
@test showable("text/plain", text_png)
@test showable("image/png", text_png)
@test !showable("image/html", text_png)
# (set|with)context
iob = IOBuffer()
ioc = IOContext(iob, :compact=>true, :limit=>true, :displaysize=>(24, 80))
show(ioc, MIME"text/plain"(), PrintContext())
@test String(take!(iob)) == "true/true/(24, 80)"
show(ioc, MIME"text/plain"(), DisplayAs.setcontext(PrintContext()))
@test String(take!(iob)) == "true/true/(24, 80)"
show(ioc, MIME"text/plain"(), DisplayAs.withcontext()(PrintContext()))
@test String(take!(iob)) == "true/true/(24, 80)"
show(ioc, MIME"text/plain"(), DisplayAs.setcontext(PrintContext(), :limit=>false))
@test String(take!(iob)) == "true/false/(24, 80)"
show(ioc, MIME"text/plain"(), DisplayAs.withcontext(:limit=>false)(PrintContext()))
@test String(take!(iob)) == "true/false/(24, 80)"
# Unlimited
show(ioc, MIME"text/plain"(), DisplayAs.Unlimited(PrintContext()))
@test String(take!(iob)) == "false/false/($(typemax(Int)), $(typemax(Int)))"
end

using Aqua
Expand Down

0 comments on commit 4e1cede

Please sign in to comment.