Skip to content

Instantly share code, notes, and snippets.

@fjebaker
Last active April 23, 2024 11:30
Show Gist options
  • Save fjebaker/b0fda23ed4ed3232f59c54039e33ec5c to your computer and use it in GitHub Desktop.
Save fjebaker/b0fda23ed4ed3232f59c54039e33ec5c to your computer and use it in GitHub Desktop.
Base.@kwdef struct CustomDisplay <: AbstractDisplay
image_dir::String = "figs"
end
image_directory(c::CustomDisplay) = c.image_dir
_mime_extension(::MIME"image/png") = "png"
_mime_extension(::MIME"image/svg") = "svg"
function Base.display(c::CustomDisplay, m::MIME, @nospecialize(x))
name = repr(MIME"text/plain"(), Dates.now())
if !isdir(image_directory(c))
mkdir(image_directory(c))
end
filename = joinpath(image_directory(c), "$name.$(_mime_extension(m))")
# write to a file so we don't lose it if anything goes wrong
write(filename, repr(m, x))
# here's where the magic happens
run(`/home/lilith/Developer/printing/star-photo.py $filename`, wait=false)
return nothing
end
function Base.display(d::CustomDisplay, x)
supported = (MIME"image/png"(), MIME"image/svg"())
for m in supported
if showable(m, x)
Base.display(d, m, x)
return
end
end
throw(MethodError(display, (d, x)))
end
function fix_displays()
for d in reverse(Base.Multimedia.displays)
if d isa CustomDisplay
popdisplay(d)
end
end
pushdisplay(CustomDisplay())
end
atreplinit(repl -> begin
fix_displays()
println("Custom display hook active")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment