Skip to content

Commit

Permalink
some maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Mar 5, 2021
1 parent cca8d5e commit 31decdc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Reexport = "^1"
Suppressor = "^0.2.0"
julia = "^1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
28 changes: 14 additions & 14 deletions src/DisplayTricks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

export as_mime

struct AsMIME
mime::MIME
struct AsMIME{M <: MIME}
mime::M
x::Any
end

Base.showable(m::MIME, am::AsMIME) = m == am.mime
Base.show(io::IO, m::MIME, am::AsMIME) = Base.show(io, m, am.x)
Base.show(io::IO, m::M, am::AsMIME{M}) where M <: MIME = Base.show(io, m, am.x)

function as_mime(m::MIME)
x -> as_mime(m, x)
Expand Down Expand Up @@ -76,13 +75,12 @@ repr(MIME"text/latex"(), s) == "\\\\frac{hello}{world}"
```
"""
struct Show
mime::MIME
struct Show{M <: MIME}
mime::M
data
end

Base.showable(m::MIME, x::Show) = x.mime == m
Base.show(io::IO, ::MIME, x::Show) = write(io, x.data)
Base.show(io::IO, ::M, x::Show{M}) where M <: MIME = write(io, x.data)


"""
Expand Down Expand Up @@ -116,18 +114,20 @@ large_df = DataFrame(rand(100,100))
WithIOContext(large_df, :displaysize => (9999,9999))
```
"""
struct WithIOContext
x
context_properties
WithIOContext(x, props::Pair...; moreprops...) = new(x, [props..., moreprops...])
struct WithIOContext{T}
x::T
context::IOContext{Base.DevNull}
WithIOContext(x::T, props::Pair...; moreprops...) where T = new{T}(x, IOContext(devnull, props..., moreprops...))
end

Base.:(==)(w1::WithIOContext, w2::WithIOContext) = w1.x == w2.x && w1.context == w2.context

function Base.show(io::IO, m::MIME, w::WithIOContext)
Base.show(IOContext(io, w.context_properties...), m, w.x)
Base.show(IOContext(io, w.context), m, w.x)
end
# we need to add a more specific method for text/plain because Julia base has a fallback method here
function Base.show(io::IO, m::MIME"text/plain", w::WithIOContext)
Base.show(IOContext(io, w.context_properties...), m, w.x)
Base.show(IOContext(io, w.context), m, w.x)
end

Base.showable(m::MIME, w::WithIOContext) = Base.showable(m, w.x)
68 changes: 68 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using PlutoUI
using Test


@testset "DisplayTricks" begin

x = "asdf"

# Show

@test repr(MIME"derp1"(), Show(MIME"derp1"(), x)) == codeunits(x)

Base.istextmime(::MIME"derp2") = true

@test repr(MIME"derp2"(), Show(MIME"derp2"(), codeunits(x))) == x


s = Show(MIME"cool1"(), x)


@test Base.showable(MIME"text/plain"(), x)
@test !Base.showable(MIME"cool1"(), x)
# everything is plaintext showable
@test Base.showable(MIME"text/plain"(), s)
@test Base.showable(MIME"cool1"(), s)


# AsMIME

mimer = as_mime(MIME"cool2"())
am = mimer(s)

@test Base.showable(MIME"cool1"(), s)
@test !Base.showable(MIME"cool2"(), s)
@test Base.showable(MIME"text/plain"(), s)

@test !Base.showable(MIME"cool1"(), am)
@test Base.showable(MIME"cool2"(), am)
@test Base.showable(MIME"text/plain"(), am)


# WithIOContext

@test WithIOContext(s, :a => 1, :b => 2) ==
WithIOContext(s, :a => 1; b = 2) ==
WithIOContext(s, a = 1; b = 2) ==
WithIOContext(s; a = 1, b = 2)


@test repr(MIME"text/plain"(), WithIOContext(1.345234523452, compact=true)) == "1.34523"
@test repr(MIME"text/plain"(), WithIOContext(1.345234523452, compact=false)) == "1.345234523452"


struct Uhm end
m = MIME"hello"()
Base.istextmime(::MIME"hello") = true

Base.show(io::IO, ::MIME"hello", ::Uhm) = print(io, get(io, :prop, missing))

u = Uhm()

@test repr(m, WithIOContext(u, prop=1)) == "1"
end





0 comments on commit 31decdc

Please sign in to comment.