Skip to content

[Speculative] Implement AnnotatedDisplay for ANSI rendering #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@
ANSI_STYLE_CODES.end_reverse))
end

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

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

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

# We need to make sure that printing to an `AnnotatedIOBuffer` calls `write` not `print`
# so we get the specialised handling that `_ansi_writer` doesn't provide.
Base.print(io::AnnotatedIOBuffer, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
(write(io, s); nothing)

Base.escape_string(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}},
esc = ""; keep = (), ascii::Bool=false, fullhex::Bool=false) =
(_ansi_writer(io, s, (io, s) -> escape_string(io, s, esc; keep, ascii, fullhex)); nothing)

function Base.write(io::IO, c::AnnotatedChar)
function Base.AnnotatedDisplay.ansi_write(::typeof(write), io::IO, c::AnnotatedChar)
if get(io, :color, false) == true
termstyle(io, getface(c), getface())
bytes = write(io, c.char)
Expand All @@ -281,9 +271,7 @@
end
end

Base.print(io::IO, c::AnnotatedChar) = (write(io, c); nothing)

function Base.show(io::IO, c::AnnotatedChar)
function Base.AnnotatedDisplay.show_annot(io::IO, c::AnnotatedChar)

Check warning on line 274 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L274

Added line #L274 was not covered by tests
if get(io, :color, false) == true
out = IOBuffer()
show(out, c.char)
Expand All @@ -296,19 +284,11 @@
end
end

function Base.write(io::IO, aio::AnnotatedIOBuffer)
if get(io, :color, false) == true
# This does introduce an overhead that technically
# could be avoided, but I'm not sure that it's currently
# worth the effort to implement an efficient version of
# writing from a AnnotatedIOBuffer with style.
# In the meantime, by converting to an `AnnotatedString` we can just
# reuse all the work done to make that work.
write(io, read(aio, AnnotatedString))
else
write(io, aio.io)
end
end
Base.AnnotatedDisplay.show_annot(io::IO, ::MIME"text/html", s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
show_html(io, s)

# End AnnotatedDisplay hooks
# ------------

"""
A mapping between ANSI named colors and 8-bit colors for use in HTML
Expand Down Expand Up @@ -447,7 +427,7 @@
print(io, "\">")
end

function Base.show(io::IO, ::MIME"text/html", s::Union{<:AnnotatedString, SubString{<:AnnotatedString}})
function show_html(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}})
# We need to make sure that the customisations are loaded
# before we start outputting any styled content.
load_customisations!()
Expand Down
Loading