Skip to content

Commit da41b6a

Browse files
committed
Call load_customisations! automatically, lazily
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.
1 parent af972e0 commit da41b6a

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/StyledStrings.jl

+12-7
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ Load customisations from the user's `faces.toml` file, if it exists as well as
2525
the current environment.
2626
2727
This function should be called before producing any output in situations where
28-
the user's customisations should be considered.
28+
the user's customisations should be considered. This is called automatically
29+
when printing text or HTML output, and when calling `withfaces`, but may need
30+
to be called manually in unusual situations.
2931
3032
Unless `force` is set, customisations are only applied when this function is
3133
called for the first time, and subsequent calls are a no-op.
3234
"""
3335
function load_customisations!(; force::Bool=false)
3436
!force && HAVE_LOADED_CUSTOMISATIONS[] && return
35-
if !isempty(DEPOT_PATH)
36-
userfaces = joinpath(first(DEPOT_PATH), "config", "faces.toml")
37-
isfile(userfaces) && loaduserfaces!(userfaces)
38-
end
39-
Legacy.load_env_colors!()
40-
HAVE_LOADED_CUSTOMISATIONS[] = true
37+
(function ()
38+
@noinline
39+
if !isempty(DEPOT_PATH)
40+
userfaces = joinpath(first(DEPOT_PATH), "config", "faces.toml")
41+
isfile(userfaces) && loaduserfaces!(userfaces)
42+
end
43+
Legacy.load_env_colors!()
44+
HAVE_LOADED_CUSTOMISATIONS[] = true
45+
end)()
4146
nothing
4247
end
4348

src/faces.jl

+3
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ red and blue mixed make purple
465465
```
466466
"""
467467
function withfaces(f, keyvals_itr)
468+
# Before modifying the current `FACES`, we should ensure
469+
# that we've loaded the user's customisations.
470+
load_customisations!()
468471
if !(eltype(keyvals_itr) <: Pair{Symbol})
469472
throw(MethodError(withfaces, (f, keyvals_itr)))
470473
end

src/io.jl

+6
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ end
227227

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

444447
function Base.show(io::IO, ::MIME"text/html", s::Union{<:AnnotatedString, SubString{<:AnnotatedString}})
448+
# We need to make sure that the customisations are loaded
449+
# before we start outputting any styled content.
450+
load_customisations!()
445451
htmlescape(str) = replace(str, '&' => "&amp;", '<' => "&lt;", '>' => "&gt;")
446452
buf = IOBuffer() # Avoid potential overhead in repeatadly printing a more complex IO
447453
lastface::Face = getface()

src/precompile.jl

+2
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ StyledStrings.resetfaces!()
3939
StyledStrings.withfaces(:yellow => StyledStrings.Face(foreground=:red), :green => :blue) do
4040
println(colorio, styled"{yellow:red} and {green:blue} mixed make {magenta:purple}")
4141
end
42+
43+
StyledStrings.HAVE_LOADED_CUSTOMISATIONS[] = false

0 commit comments

Comments
 (0)