Skip to content

Commit

Permalink
Add support to display PNG for arbitrary types
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed May 20, 2024
1 parent 340ebeb commit 126d2ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ struct TerminalGraphicDisplay{TC<:IO,TS<:IO} <: AbstractDisplay
end
TerminalGraphicDisplay(io::IO) = TerminalGraphicDisplay(io, io)

Base.displayable(::TerminalGraphicDisplay, ::MIME"image/png") = true
Base.displayable(::TerminalGraphicDisplay, ::MIME"image/png", x::Any) = showable("image/png", x)
Base.displayable(::TerminalGraphicDisplay, ::MIME"image/png", ::Vector{UInt8}) = true
Base.displayable(::TerminalGraphicDisplay, ::MIME"image/png", ::AbstractArray{<:Colorant}) = true

function Base.display(d::TerminalGraphicDisplay, ::MIME"image/png", x::Any)
io = IOBuffer()
show(io, "image/png", x)
display(d, MIME("image/png"), FileIO.load(io))
end

function Base.display(d::TerminalGraphicDisplay, ::MIME"image/png", bytes::Vector{UInt8})
# In this case, assume it to be png byte sequences, use FileIO to find a decoder for it.
Expand Down
6 changes: 6 additions & 0 deletions test/tst_display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
dsp = ImageInTerminal.TerminalGraphicDisplay(io)
display(dsp, MIME("image/png"), bytes)
@test length(read(io, String)) > 5_000

struct Foo end
Base.show(io::IO, ::MIME"image/png", ::Foo) = FileIO.save(Stream{format"PNG"}(io), FileIO.load(fn))
dsp = ImageInTerminal.TerminalGraphicDisplay(io)
display(dsp, MIME("image/png"), Foo())
@test length(read(io, String)) > 5_000
end

0 comments on commit 126d2ce

Please sign in to comment.