diff --git a/src/StyledStrings.jl b/src/StyledStrings.jl index 6d332a3..1eb4b5a 100644 --- a/src/StyledStrings.jl +++ b/src/StyledStrings.jl @@ -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 diff --git a/src/faces.jl b/src/faces.jl index 89217a8..6dcbd09 100644 --- a/src/faces.jl +++ b/src/faces.jl @@ -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 diff --git a/src/io.jl b/src/io.jl index bbd6efd..4a60121 100644 --- a/src/io.jl +++ b/src/io.jl @@ -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] @@ -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, '&' => "&", '<' => "<", '>' => ">") buf = IOBuffer() # Avoid potential overhead in repeatadly printing a more complex IO lastface::Face = getface() diff --git a/src/precompile.jl b/src/precompile.jl index 8406f9a..0ae30d1 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -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