Skip to content

Commit

Permalink
Avoid needlessly creating a new Face in get calls
Browse files Browse the repository at this point in the history
Instead of providing Face() as the fallback value with a get call, we
can instead provide the constructor Face as the first argument,
preventing it from being called needlessly.
  • Loading branch information
KristofferC authored and tecosaur committed Aug 6, 2024
1 parent c647af9 commit eada2dc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/faces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function withfaces(f, keyvals_itr)
if face isa Face
newfaces[name] = face
elseif face isa Symbol
newfaces[name] = get(FACES.current[], face, Face())
newfaces[name] = get(Face, FACES.current[], face)
elseif face isa Vector{Symbol}
newfaces[name] = Face(inherit=face)
elseif haskey(newfaces, name)
Expand Down Expand Up @@ -517,7 +517,7 @@ function Base.merge(a::Face, b::Face)
b_noinherit = Face(
b.font, b.height, b.weight, b.slant, b.foreground, b.background,
b.underline, b.strikethrough, b.inverse, Symbol[])
b_inheritance = map(fname -> get(FACES.current[], fname, Face()), Iterators.reverse(b.inherit))
b_inheritance = map(fname -> get(Face, FACES.current[], fname), Iterators.reverse(b.inherit))
b_resolved = merge(foldl(merge, b_inheritance), b_noinherit)
merge(a, b_resolved)
end
Expand All @@ -529,7 +529,7 @@ Base.merge(a::Face, b::Face, others::Face...) = merge(merge(a, b), others...)

# Putting these inside `getface` causes the julia compiler to box it
_mergedface(face::Face) = face
_mergedface(face::Symbol) = get(FACES.current[], face, Face())
_mergedface(face::Symbol) = get(Face, FACES.current[], face)
_mergedface(faces::Vector) = mapfoldl(_mergedface, merge, Iterators.reverse(faces))

"""
Expand Down Expand Up @@ -558,7 +558,7 @@ function getface(annotations::Vector{Pair{Symbol, Any}})
end

getface(face::Face) = merge(FACES.current[][:default], merge(Face(), face))
getface(face::Symbol) = getface(get(FACES.current[], face, Face()))
getface(face::Symbol) = getface(get(Face, FACES.current[], face))

"""
getface()
Expand Down

0 comments on commit eada2dc

Please sign in to comment.