Skip to content

Commit

Permalink
Merge pull request #11 from devmotion/dw/imageio
Browse files Browse the repository at this point in the history
Switch to ImageIO
  • Loading branch information
eschnett committed Sep 6, 2023
2 parents c0c6c37 + 3741ef9 commit 535dfc3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name = "SixelTerm"
uuid = "65edfddc-f399-4499-8369-a1bd38eee2ea"
version = "1.1.1"
version = "1.2.0"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"

[compat]
FileIO = "1.4.1"
ImageMagick = "1.2"
ImageIO = "0.6.7"
julia = "1.6"
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ Here is an example using Plots with the default GR backend:

```julia
ENV["GKSwstype"] = "nul" # needed for the GR backend on headless servers
using Plots
using SixelTerm
using Plots
scatter(rand(100))
```
Note that when using it with Plots, you have to do `using SixelTerm` after `using Plots`.
For some reason, Plots.jl adds its own display to the stack, so we need the SixelTerm
display added last.

This is how things look in iTerm2:
<img src="https://raw.githubusercontent.com/eschnett/SixelTerm.jl/master/demo.png" width=900px></img>

Here is an example using Makie:
```julia
using CairoMakie
using SixelTerm
using CairoMakie
scatter(rand(100))
```

Expand All @@ -52,6 +49,4 @@ This package was written by [Tom Short](https://github.com/tshort) and
is now maintained by [Erik Schnetter](https://github.com/eschnett).

[TerminalGraphics](https://github.com/m-j-w/TerminalGraphics.jl) is another package that
provides similar functionality. The main difference is that TerminalGraphics includes an
interface to libsixel, while this package relies on ImageMagick for conversion to the Sixel
format.
provides similar functionality.
42 changes: 25 additions & 17 deletions src/SixelTerm.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
module SixelTerm

using FileIO
using ImageMagick
using ImageIO

struct SixelDisplay <: AbstractDisplay end

function Base.display(d::SixelDisplay, m::MIME{Symbol("image/png")}, x)
a = repr("image/png", x)
im = ImageMagick.load_(a)
io = IOBuffer()
s = Stream{format"six"}(io, "t.six")
ImageMagick.save(s, im)
write(stdout, take!(io))
return nothing
const MIMES = ("image/png", "image/tiff", "image/jpeg")
const FORMATS = ("PNG", "TIFF", "JPEG")

for (mime, fmt) in zip(MIMES, FORMATS)
@eval begin
function Base.display(::SixelDisplay, m::MIME{Symbol($mime)}, @nospecialize(x))
io = IOBuffer()
show(io, m, x)
seekstart(io)
im = load(Stream{DataFormat{Symbol($fmt)}}(io))
save(Stream{format"SIXEL"}(stdout), im)
return nothing
end
Base.displayable(::SixelDisplay, ::MIME{Symbol($mime)}) = true
end
end

function Base.display(d::SixelDisplay, x)
if showable("image/png", x)
display(d, "image/png", x)
else
# fall through to the Displays lower in the display stack
throw(MethodError(display, "nope"))
Base.displayable(::SixelDisplay, ::MIME) = false

function Base.display(d::SixelDisplay, @nospecialize(x))
for mime in MIMES
if showable(mime, x)
return display(d, mime, x)
end
end
return nothing
throw(MethodError(display, (d, x)))
end

function __init__()
Base.Multimedia.pushdisplay(SixelDisplay())
pushdisplay(SixelDisplay())
return nothing
end

Expand Down
4 changes: 2 additions & 2 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
CairoMakie = "0.8.13"
julia = "1.5"
CairoMakie = "0.10"
julia = "1.6"

0 comments on commit 535dfc3

Please sign in to comment.