Skip to content

Commit f4cddb6

Browse files
committed
use ternery instead of ifelse since ifelse force evaluation of the RHS
1 parent 667aaa5 commit f4cddb6

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/faces.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct Face
128128
Tuple{<:Union{Nothing, SimpleColor}, Symbol}}
129129
strikethrough::Union{Nothing, Bool}
130130
inverse::Union{Nothing, Bool}
131-
inherit::Vector{Symbol}
131+
inherit::Union{Nothing ,Vector{Symbol}}
132132
end
133133

134134
function Face(; font::Union{Nothing, String} = nothing,
@@ -143,7 +143,7 @@ function Face(; font::Union{Nothing, String} = nothing,
143143
} = nothing,
144144
strikethrough::Union{Nothing, Bool} = nothing,
145145
inverse::Union{Nothing, Bool} = nothing,
146-
inherit::Union{Symbol, Vector{Symbol}} = Symbol[],
146+
inherit::Union{Nothing, Symbol, Vector{Symbol}} = nothing,
147147
_...) # Simply ignore unrecognised keyword arguments.
148148
ascolor(::Nothing) = nothing
149149
ascolor(c::AbstractString) = parse(SimpleColor, c)
@@ -173,7 +173,7 @@ Base.:(==)(a::Face, b::Face) =
173173
Base.copy(f::Face) =
174174
Face(f.font, f.height, f.weight, f.slant,
175175
f.foreground, f.background, f.underline,
176-
f.strikethrough, f.inverse, copy(f.inherit))
176+
f.strikethrough, f.inverse, f.inherit === nothing ? f.inherit : copy(f.inherit))
177177

178178
function Base.show(io::IO, ::MIME"text/plain", color::SimpleColor)
179179
skiptype = get(io, :typeinfo, nothing) === SimpleColor
@@ -228,7 +228,7 @@ function Base.show(io::IO, ::MIME"text/plain", face::Face)
228228
show(io, getfield(face, field))
229229
end
230230
end
231-
if !isempty(face.inherit)
231+
if !isnothing(face.inherit) && !isempty(face.inherit)
232232
if isfirst; isfirst = false else print(io, ", ") end
233233
print(io, "inherit=")
234234
show(IOContext(io, :typeinfo => Vector{Symbol}), face.inherit)
@@ -274,7 +274,7 @@ function Base.show(io::IO, ::MIME"text/plain", face::Face)
274274
getfield(face, field))
275275
end
276276
end
277-
if !isempty(face.inherit)
277+
if !isnothing(face.inherit) && !isempty(face.inherit)
278278
print(io, '\n', lpad("inherit", fieldnamepad, ' '), ": ")
279279
isfirst = true
280280
for iface in face.inherit
@@ -490,30 +490,30 @@ Merge the properties of the `initial` face and `others`, with
490490
later faces taking priority.
491491
"""
492492
function Base.merge(a::Face, b::Face)
493-
if isempty(b.inherit)
493+
if isnothing(b.inherit) || isempty(b.inherit)
494494
# Extract the heights to help type inference a bit to be able
495495
# to narrow the types in e.g. `aheight * bheight`
496496
aheight = a.height
497497
bheight = b.height
498-
Face(ifelse(isnothing(b.font), a.font, b.font),
498+
Face(isnothing(b.font) ? a.font : b.font,
499499
if isnothing(bheight) aheight
500500
elseif isnothing(aheight) bheight
501501
elseif bheight isa Int bheight
502502
elseif aheight isa Int round(Int, aheight * bheight)
503503
else aheight * bheight end,
504-
ifelse(isnothing(b.weight), a.weight, b.weight),
505-
ifelse(isnothing(b.slant), a.slant, b.slant),
506-
ifelse(isnothing(b.foreground), a.foreground, b.foreground),
507-
ifelse(isnothing(b.background), a.background, b.background),
508-
ifelse(isnothing(b.underline), a.underline, b.underline),
509-
ifelse(isnothing(b.strikethrough), a.strikethrough, b.strikethrough),
510-
ifelse(isnothing(b.inverse), a.inverse, b.inverse),
504+
isnothing(b.weight) ? a.weight : b.weight,
505+
isnothing(b.slant) ? a.slant : b.slant,
506+
isnothing(b.foreground) ? a.foreground : b.foreground,
507+
isnothing(b.background) ? a.background : b.background,
508+
isnothing(b.underline) ? a.underline : b.underline,
509+
isnothing(b.strikethrough) ? a.strikethrough : b.strikethrough,
510+
isnothing(b.inverse) ? a.inverse : b.inverse,
511511
a.inherit)
512512
else
513513
b_noinherit = Face(
514514
b.font, b.height, b.weight, b.slant, b.foreground, b.background,
515515
b.underline, b.strikethrough, b.inverse, Symbol[])
516-
b_inheritance = map(fname -> get(Face, FACES.current[], fname), Iterators.reverse(b.inherit))
516+
b_inheritance = map(fname -> get(Face, FACES.current[], fname), b.inherit === nothing ? nothing : Iterators.reverse(b.inherit))
517517
b_resolved = merge(foldl(merge, b_inheritance), b_noinherit)
518518
merge(a, b_resolved)
519519
end
@@ -537,7 +537,7 @@ Obtain the final merged face from `faces`, an iterator of
537537
function getface(faces)
538538
isempty(faces) && return FACES.current[][:default]
539539
combined = mapfoldl(_mergedface, merge, faces)::Face
540-
if !isempty(combined.inherit)
540+
if !isnothing(combined.inherit) && !isempty(combined.inherit)
541541
combined = merge(Face(), combined)
542542
end
543543
merge(FACES.current[][:default], combined)

0 commit comments

Comments
 (0)