Skip to content

Commit

Permalink
Introduce AnnotatedIOBuffer
Browse files Browse the repository at this point in the history
This allows for styled content to be constructed incrementally, without
resorting to repeated concatenation. It operates very similarly to
IOContext, just with a special `write` method and specifically wrapping
an IOBuffer.
  • Loading branch information
tecosaur committed Oct 21, 2023
1 parent f71228d commit 43f88eb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ function show_circular(io::IOContext, @nospecialize(x))
return false
end

AnnotatedIOBuffer(io::IOContext{IOBuffer}) = AnnotatedIOBuffer(io.io, io.dict)
IOContext(io::AnnotatedIOBuffer) = IOContext(io.io, io.dict)

"""
show([io::IO = stdout], x)
Expand Down
62 changes: 62 additions & 0 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,65 @@ annotations(s::SubString{<:AnnotatedString}, pos::UnitRange{<:Integer}) =
Get all annotations of `chr`.
"""
annotations(c::AnnotatedChar) = c.annotations

## AnnotatedIOBuffer

struct AnnotatedIOBuffer <: IO
io::IOBuffer
annotations::Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}
dict::ImmutableDict{Symbol, Any}
end

AnnotatedIOBuffer(io::IOBuffer, dict::ImmutableDict = ImmutableDict{Symbol,Any}()) =
AnnotatedIOBuffer(io, Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}(), dict)

function AnnotatedIOBuffer(io::IOBuffer, (k, v)::Pair{Symbol, Any}, kvs::Pair{Symbol, Any}...)
dict = ImmutableDict{Symbol,Any}(k, v)
for (k, v) in kvs
dict = ImmutableDict{Symbol,Any}(dict, k, v)
end
AnnotatedIOBuffer(io, dict)
end

AnnotatedIOBuffer() = AnnotatedIOBuffer(IOBuffer())

function show(io::IO, annio::AnnotatedIOBuffer)
show(io, AnnotatedIOBuffer)
print(io, '(', annio.io.size, " bytes)")
end

position(io::AnnotatedIOBuffer) = position(io.io)
lock(io::AnnotatedIOBuffer) = lock(io.io)
unlock(io::AnnotatedIOBuffer) = unlock(io.io)
get(io::AnnotatedIOBuffer, key, default) = key == :color || get(io.dict, key, default)
displaysize(io::AnnotatedIOBuffer) =
if haskey(io.dict, :displaysize) io.dict[:displaysize]::Tuple{Int,Int} else displaysize(io.io) end

write(io::AnnotatedIOBuffer, x) = write(io.io, x)

function write(io::AnnotatedIOBuffer, astr::Union{AnnotatedString, SubString{<:AnnotatedString}})
astr = AnnotatedString(astr)
offset = position(io.io)
for (region, annot) in astr.annotations
start, stop = first(region), last(region)
push!(io.annotations, (start+offset:stop+offset, annot))
end
write(io.io, astr)
end
write(io::AnnotatedIOBuffer, achr::AnnotatedChar) = write(aio, AnnotatedString(achr))

# To prevent method ambiguity
write(io::AnnotatedIOBuffer, x::Char) = write(io.io, x)
write(io::AnnotatedIOBuffer, x::Union{SubString{String}, String}) = write(io.io, x)
write(io::AnnotatedIOBuffer, x::AbstractString) = write(io.io, x)

function take!(aiob::AnnotatedIOBuffer)
str = String(take!(aiob.io))
annot = copy(aiob.annotations)
empty!(aiob.annotations)
seekstart(aiob.io)
str, annot
end

AnnotatedString((str, annots)::Tuple{<:AbstractString, Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}}) =
AnnotatedString(str, annots)

0 comments on commit 43f88eb

Please sign in to comment.