Created
January 26, 2020 07:36
-
-
Save mkitti/e4f7d2a0ed00f1b7ca9d37838f7011dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Allows for the splitting output among several AbstractDisplays" | |
module Tee | |
import Base.Multimedia.display | |
import Base.TextDisplay | |
import Base: flush, close | |
export TeeDisplay, display, teeDisplay, FlushingTextDisplay, flush, close | |
""" | |
TeeDisplay(displays::Vector{AbstractDisplay}) | |
TeeDisplay(args::AbstractDisplay...) | |
Returns a `TeeDisplay <: AbstractDisplay`, which forwards display calls to | |
an arbitrary number of other AbstractDisplays. An example use is logging | |
output to files. | |
""" | |
struct TeeDisplay <: AbstractDisplay | |
displays::Vector{AbstractDisplay} | |
end | |
TeeDisplay(args::AbstractDisplay...) = TeeDisplay(collect((args...,))) | |
display(d::TeeDisplay,x) = (y->display(y,x)).(d.displays)[1] | |
displayable(d::TeeDisplay, M::MIME) = all(displayable.(d.displays,M)) | |
flush(d::TeeDisplay) = flush.(d.displays) | |
close(d::TeeDisplay) = close.(d.displays) | |
""" | |
teeDisplay(d::AbstractDisplay) | |
This duplicates output to the current AbstractDisplay to a new | |
AbstractDisplay. | |
""" | |
function teeDisplay(d::AbstractDisplay) | |
old_display = Base.Multimedia.popdisplay() | |
tee_display = TeeDisplay(old_display,d) | |
Base.Multimedia.pushdisplay(old_display) | |
Base.Multimedia.pushdisplay(tee_display) | |
end | |
""" | |
FlushingTextDisplay(io:IO) | |
FlushingTextDisplay(filename:AbstractString) | |
Follow each display call with a new line and a call to flush. | |
""" | |
struct FlushingTextDisplay <: AbstractDisplay | |
io::IO | |
end | |
FlushingTextDisplay(filename::AbstractString) = FlushingTextDisplay(open(filename,"w")) | |
TextDisplay(d::FlushingTextDisplay) = TextDisplay(d.io) | |
function display(d::FlushingTextDisplay,x) | |
display(TextDisplay(d),x) | |
println(d.io,"") | |
flush(d.io) | |
end | |
displayable(d::FlushingTextDisplay) = displayable(TextDisplay(d)) | |
convert(::Type{TextDisplay},d::FlushingTextDisplay) = TextDisplay(d) | |
flush(d::FlushingTextDisplay) = flush(d.io) | |
close(d::FlushingTextDisplay) = close(d.io) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment