Skip to content

Commit

Permalink
Use (set|with)context instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jan 24, 2022
1 parent 4d8c443 commit 7480f97
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
46 changes: 35 additions & 11 deletions src/DisplayAs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,55 @@ 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

"""
IOContextCarrier(x, kv::Pair...)
DisplayAs.setcontext(obj, kvs::Pair...)
Bundle arguments for `IOContext` with an object `x`.
Bundle arguments for `IOContext` with the object `x`.
# Examples
```julia-repl
julia> using DisplayAs
julia> import DisplayAs
julia> data = rand(2, 2)
2×2 Array{Float64,2}:
0.786992 0.576265
0.321868 0.791263
julia> DisplayAs.IOContextCarrier(data, :compact => false)
julia> DisplayAs.setcontext(data, :compact => false)
2×2 Array{Float64,2}:
0.7869920812675713 0.5762653628115182
0.32186846202784314 0.791263230914472
```
See also [`DisplayAs.withcontext`](@ref).
"""
struct IOContextCarrier
content
context::Dict{Symbol,Any}
end
IOContextCarrier(obj, kv::Pair...) = IOContextCarrier(obj, Dict{Symbol,Any}(kv...))
setcontext(obj, kvs::Pair...) = IOContextCarrier(obj, Dict{Symbol,Any}(kvs...))

"""
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).
"""
withcontext(kvs::Pair...) = obj -> setcontext(obj, kvs...)

Base.showable(::MIME{mime}, x::IOContextCarrier) where {mime} =
hasmethod(show, Tuple{IO, MIME{mime}, typeof(x)}) &&
Expand Down Expand Up @@ -182,8 +206,8 @@ julia> data |> DisplayAs.Unlimited
```
"""
function Unlimited(x)
IOContextCarrier(x, :compact => false, :limit => false,
:displaysize => (typemax(Int), typemax(Int)))
setcontext(x, :compact => false, :limit => false,
:displaysize => (typemax(Int), typemax(Int)))
end

end # module
10 changes: 7 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ end
@test showable("text/plain", text_png)
@test showable("image/png", text_png)
@test !showable("image/html", text_png)
# IOContextCarrier
# (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.IOContextCarrier(PrintContext()))
show(ioc, MIME"text/plain"(), DisplayAs.setcontext(PrintContext()))
@test String(take!(iob)) == "true/true/(24, 80)"
show(ioc, MIME"text/plain"(), DisplayAs.IOContextCarrier(PrintContext(), :limit=>false))
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()))
Expand Down

0 comments on commit 7480f97

Please sign in to comment.