Skip to content

Commit 7cf8538

Browse files
committed
Implement AnnotatedDisplay for ANSI rendering
Remove invalidating methods, and use the write-barrier introduced by Base.
1 parent 97f7c26 commit 7cf8538

File tree

1 file changed

+9
-32
lines changed

1 file changed

+9
-32
lines changed

src/io.jl

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ function termstyle(io::IO, face::Face, lastface::Face=getface())
225225
ANSI_STYLE_CODES.end_reverse))
226226
end
227227

228-
function _ansi_writer(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}},
229-
string_writer::F) where {F <: Function}
228+
function _ansi_writer(string_writer::F, io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) where {F <: Function}
230229
# We need to make sure that the customisations are loaded
231230
# before we start outputting any styled content.
232231
load_customisations!()
@@ -255,22 +254,13 @@ function _ansi_writer(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedS
255254
end
256255
end
257256

258-
Base.write(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
259-
_ansi_writer(io, s, write)::Int
257+
# ------------
258+
# Hook into the AnnotatedDisplay invalidation barrier
260259

261-
Base.print(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
262-
(_ansi_writer(io, s, print); nothing)
260+
Base.AnnotatedDisplay.ansi_write(f::F, io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) where {F <: Function} =
261+
_ansi_writer(f, io, s)
263262

264-
# We need to make sure that printing to an `AnnotatedIOBuffer` calls `write` not `print`
265-
# so we get the specialised handling that `_ansi_writer` doesn't provide.
266-
Base.print(io::AnnotatedIOBuffer, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
267-
(write(io, s); nothing)
268-
269-
Base.escape_string(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}},
270-
esc = ""; keep = (), ascii::Bool=false, fullhex::Bool=false) =
271-
(_ansi_writer(io, s, (io, s) -> escape_string(io, s, esc; keep, ascii, fullhex)); nothing)
272-
273-
function Base.write(io::IO, c::AnnotatedChar)
263+
function Base.AnnotatedDisplay.ansi_write(::typeof(write), io::IO, c::AnnotatedChar)
274264
if get(io, :color, false) == true
275265
termstyle(io, getface(c), getface())
276266
bytes = write(io, c.char)
@@ -281,9 +271,7 @@ function Base.write(io::IO, c::AnnotatedChar)
281271
end
282272
end
283273

284-
Base.print(io::IO, c::AnnotatedChar) = (write(io, c); nothing)
285-
286-
function Base.show(io::IO, c::AnnotatedChar)
274+
function Base.AnnotatedDisplay.show_annot(io::IO, c::AnnotatedChar)
287275
if get(io, :color, false) == true
288276
out = IOBuffer()
289277
show(out, c.char)
@@ -296,19 +284,8 @@ function Base.show(io::IO, c::AnnotatedChar)
296284
end
297285
end
298286

299-
function Base.write(io::IO, aio::AnnotatedIOBuffer)
300-
if get(io, :color, false) == true
301-
# This does introduce an overhead that technically
302-
# could be avoided, but I'm not sure that it's currently
303-
# worth the effort to implement an efficient version of
304-
# writing from a AnnotatedIOBuffer with style.
305-
# In the meantime, by converting to an `AnnotatedString` we can just
306-
# reuse all the work done to make that work.
307-
write(io, read(aio, AnnotatedString))
308-
else
309-
write(io, aio.io)
310-
end
311-
end
287+
# End AnnotatedDisplay hooks
288+
# ------------
312289

313290
"""
314291
A mapping between ANSI named colors and 8-bit colors for use in HTML

0 commit comments

Comments
 (0)