Skip to content

Commit

Permalink
Call load_customisations! automatically, lazily
Browse files Browse the repository at this point in the history
I've been wary about doing lazy application of load_customisations!, but
with the @noinline addition the calls are really cheap: only 1.5ns
according to Chairmarks on my machine. Thus, the cost of calling it
after initialisation will be negligeable for any non-trivial function.

Having rolled this around further, other than printing I think only
withfaces calls actually require loading of user styles to be valid.

With these two realisations, calling load_customisations! lazily as
appropriate seems like a bit of a no-brainer.

We'll probably want to add this to public API eventually for
non-text/html output implementations, but we can wait to see how this is
bourne out first.
  • Loading branch information
tecosaur committed Sep 20, 2024
1 parent af972e0 commit da41b6a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/StyledStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@ Load customisations from the user's `faces.toml` file, if it exists as well as
the current environment.
This function should be called before producing any output in situations where
the user's customisations should be considered.
the user's customisations should be considered. This is called automatically
when printing text or HTML output, and when calling `withfaces`, but may need
to be called manually in unusual situations.
Unless `force` is set, customisations are only applied when this function is
called for the first time, and subsequent calls are a no-op.
"""
function load_customisations!(; force::Bool=false)
!force && HAVE_LOADED_CUSTOMISATIONS[] && return
if !isempty(DEPOT_PATH)
userfaces = joinpath(first(DEPOT_PATH), "config", "faces.toml")
isfile(userfaces) && loaduserfaces!(userfaces)
end
Legacy.load_env_colors!()
HAVE_LOADED_CUSTOMISATIONS[] = true
(function ()
@noinline
if !isempty(DEPOT_PATH)
userfaces = joinpath(first(DEPOT_PATH), "config", "faces.toml")
isfile(userfaces) && loaduserfaces!(userfaces)
end
Legacy.load_env_colors!()
HAVE_LOADED_CUSTOMISATIONS[] = true
end)()
nothing
end

Expand Down
3 changes: 3 additions & 0 deletions src/faces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ red and blue mixed make purple
```
"""
function withfaces(f, keyvals_itr)
# Before modifying the current `FACES`, we should ensure
# that we've loaded the user's customisations.
load_customisations!()
if !(eltype(keyvals_itr) <: Pair{Symbol})
throw(MethodError(withfaces, (f, keyvals_itr)))
end
Expand Down
6 changes: 6 additions & 0 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ end

function _ansi_writer(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}},
string_writer::F) where {F <: Function}
# We need to make sure that the customisations are loaded
# before we start outputting any styled content.
load_customisations!()
if get(io, :color, false)::Bool
buf = IOBuffer() # Avoid the overhead in repeatadly printing to `stdout`
lastface::Face = FACES.default[:default]
Expand Down Expand Up @@ -442,6 +445,9 @@ function htmlstyle(io::IO, face::Face, lastface::Face=getface())
end

function Base.show(io::IO, ::MIME"text/html", s::Union{<:AnnotatedString, SubString{<:AnnotatedString}})
# We need to make sure that the customisations are loaded
# before we start outputting any styled content.
load_customisations!()
htmlescape(str) = replace(str, '&' => "&amp;", '<' => "&lt;", '>' => "&gt;")
buf = IOBuffer() # Avoid potential overhead in repeatadly printing a more complex IO
lastface::Face = getface()
Expand Down
2 changes: 2 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ StyledStrings.resetfaces!()
StyledStrings.withfaces(:yellow => StyledStrings.Face(foreground=:red), :green => :blue) do
println(colorio, styled"{yellow:red} and {green:blue} mixed make {magenta:purple}")
end

StyledStrings.HAVE_LOADED_CUSTOMISATIONS[] = false

0 comments on commit da41b6a

Please sign in to comment.