Output Formats
Plain Text
PlainTextPrinter prints a plain text with ANSI escape codes as a plain text (MIME"text/plain"). This may seem useless, but it has two major benefits.
One is the stripping of ANSI escape codes. You can get rid of ANSI escape codes by printing a text to an IO object with :color I/O property false.
The other is the optimization of verbose ANSI escape codes.
Examples
julia> src = IOBuffer();julia> printstyled(IOContext(src, :color => true), "light ", color=:light_cyan);julia> printstyled(IOContext(src, :color => true), "cyan", color=:light_cyan);julia> read(seekstart(src), String) # source text"\e[96mlight \e[39m\e[96mcyan\e[39m"julia> printer = PlainTextPrinter(src);julia> repr("text/plain", printer, context = :color => false) # stripped"light cyan"julia> repr("text/plain", printer, context = :color => true) # optimized"\e[96mlight cyan\e[m"
The initial and final states are implicitly interpreted as being "Normal", i.e. the state with "\e[0m".
HTML
HTMLPrinter prints a plain text with ANSI escape codes as an HTML fragment (MIME"text/html").
See Supported Codes for examples.
The HTMLPrinter constructor supports the callback keyword argument. The callback method will be called just before writing HTML tags. You can rewrite the attributes in your callback methods. You can also prevent the default tag writing by setting the return value of the callback method to something other than nothing.
src = IOBuffer();
print(src, " Normal ", "\e[48;5;246m", " GrayBG ", "\e[0m", " Normal ");
HTMLPrinter(src) # without callback method Normal GrayBG Normal julia> function cb(io::IO, printer::HTMLPrinter, tag::String, attrs::Dict{Symbol, String}) text = String(take!(io)) @show text @show tag, attrs return true # prevent default writing end;julia> dummy = IOBuffer();julia> show(dummy, MIME"text/html"(), HTMLPrinter(src, callback = cb));text = "" (tag, attrs) = ("pre", Dict{Symbol, String}()) text = " Normal " (tag, attrs) = ("span", Dict(:class => "sgr48_5", :style => "background:#949494")) text = " GrayBG " (tag, attrs) = ("/span", Dict{Symbol, String}()) text = " Normal " (tag, attrs) = ("/pre", Dict{Symbol, String}())