Skip to content

Commit c4c2119

Browse files
committed
Introduce recolouring hooks
1 parent b35aa43 commit c4c2119

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/faces.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,46 @@ function Base.convert(::Type{Face}, spec::Dict{String,Any})
763763
end)
764764
end
765765

766+
## Recolouring ##
767+
768+
const recolor_hooks = Function[]
769+
const recolor_lock = ReentrantLock()
770+
771+
"""
772+
recolor(f::Function)
773+
774+
Register a hook function `f` to be called whenever the colors change.
775+
776+
Usually hooks will be called once after terminal colors have been
777+
determined. These hooks enable dynamic retheming, but are specifically *not* run when faces
778+
are changed. They sit in between the default faces and modifications layered on
779+
top with `loadface!` and user customisations.
780+
"""
781+
function recolor(f::Function)
782+
@lock recolor_lock push!(recolor_hooks, f)
783+
nothing
784+
end
785+
786+
function setcolors!(color::Vector{Pair{Symbol, RGBTuple}})
787+
@lock recolor_lock begin
788+
for (name, rgb) in color
789+
FACES.basecolors[name] = rgb
790+
end
791+
current = FACES.current[]
792+
for (name, _) in FACES.modifications[]
793+
default = get(FACES.default, name, nothing)
794+
isnothing(default) && continue
795+
current[name] = default
796+
end
797+
for hook in recolor_hooks
798+
hook()
799+
end
800+
for (name, face) in FACES.modifications[]
801+
current[name] = merge(current[name], face)
802+
end
803+
end
804+
end
805+
766806
## Color utils ##
767807

768808
"""

0 commit comments

Comments
 (0)