From 35a3cdf9a585951227932f961ad09fed5f858074 Mon Sep 17 00:00:00 2001 From: TEC Date: Wed, 7 Aug 2024 01:42:22 +0800 Subject: [PATCH] Load user-customisations lazily This is primarily motivated by reducing the amount of work that trimming is unable to remove. --- docs/src/internals.md | 1 + src/StyledStrings.jl | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/src/internals.md b/docs/src/internals.md index 55c55d5..6cfca87 100644 --- a/docs/src/internals.md +++ b/docs/src/internals.md @@ -25,6 +25,7 @@ StyledStrings.resetfaces! StyledStrings.termcolor StyledStrings.termcolor24bit StyledStrings.termcolor8bit +StyledStrings.load_customisations! ``` ## Styled Markup parsing diff --git a/src/StyledStrings.jl b/src/StyledStrings.jl index c43ad13..6d332a3 100644 --- a/src/StyledStrings.jl +++ b/src/StyledStrings.jl @@ -16,12 +16,29 @@ include("legacy.jl") using .StyledMarkup -function __init__() +const HAVE_LOADED_CUSTOMISATIONS = Base.Threads.Atomic{Bool}(false) + +""" + load_customisations!(; force::Bool=false) + +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. + +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 + nothing end if Base.generating_output()