Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to display PNG for arbitrary types #76

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ 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
7 changes: 7 additions & 0 deletions test/tst_display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
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
Loading