Skip to content
Merged
Show file tree
Hide file tree
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
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
Loading