Skip to content

Commit

Permalink
Use @warn for warnings, and improve them
Browse files Browse the repository at this point in the history
This was previously not possible due to load order issues with
Base.CoreLogging, but now that this code exists as a stdlib we can make
use of @warn. While we're at it, we might as well enhance the messages
themselves.
  • Loading branch information
tecosaur committed Oct 20, 2023
1 parent c3ff239 commit dec2bdd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/faces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ function resetfaces!(name::Symbol)
FACES.current[][name] = deepcopy(FACES.default[name])
else # This shouldn't happen
delete!(FACES.current[], name)
println(stderr,
"""! The face $name was reset, but it had no default value, and so has been deleted instead!",
This should not have happened, perhaps the face was added without using `addface!`?""")
@warn """The face $name was reset, but it had no default value, and so has been deleted instead!,
This should not have happened, perhaps the face was added without using `addface!`?"""
end
end

Expand Down
85 changes: 62 additions & 23 deletions src/stylemacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,41 @@ macro styled_str(raw_content::String)
"grey", "gray", "bright_black", "bright_red", "bright_green", "bright_yellow",
"bright_blue", "bright_magenta", "bright_cyan", "bright_white")

function stywarn(state, message::String)
println(stderr, "WARNING: Styled string macro in module ", __module__,
" at ", something(__source__.file, ""),
':', string(__source__.line),
something(if !isempty(state.s)
i, chr = peek(state.s)
" (just before '$chr' [$i])"
end, ""),
", ", message, '.')
function stywarn(message, state=nothing, distance::Int=0)
location = string(something(__source__.file, ""), ':', string(__source__.line))
posinfo = if isnothing(state) || isempty(state.s)
""
else
i, chr = peek(state.s)
if distance > 0
i = prevind(state.content, i, distance)
chr = state.content[i]
end
infowidth = displaysize(stderr)[2] ÷ 3
j = clamp(12 * round(Int, i / 12),
firstindex(state.content):lastindex(state.content))
start = if j <= infowidth firstindex(state.content) else
max(prevind(state.content, j, infowidth), firstindex(state.content))
end
stop = if ncodeunits(state.content) - j <= infowidth lastindex(state.content) else
min(nextind(state.content, j, infowidth), lastindex(state.content))
end
window = Base.escape_string(state.content[start:stop])
begin_ellipsis = ifelse(start == firstindex(state.content), "", "")
end_ellipsis = ifelse(stop == lastindex(state.content), "", "")
nbelip, npre, nwind, neelip =
ncodeunits(begin_ellipsis), ncodeunits(Base.escape_string(state.content[start:i])),
ncodeunits(window), ncodeunits(end_ellipsis)
nprelabel = nbelip+nwind+neelip+textwidth(begin_ellipsis)+npre
TaggedString("\n $begin_ellipsis$window$end_ellipsis\n \
$(' '^(textwidth(begin_ellipsis)+npre))╰─╴around here",
[(3:2+nbelip, :face => :shadow),
(3+nbelip:2+nbelip+nwind, :face => :bright_green),
(3+nbelip+nwind:4+nbelip+nwind+neelip, :face => :shadow),
(5+nprelabel:4+nprelabel+ncodeunits("╰─╴around here"), :face => :info)])
end
@warn(Base.taggedstring("Styled string macro, ", message, '.', posinfo),
_file=String(__source__.file), _line=__source__.line, _module=__module__)
end

function addpart!(state, stop::Int)
Expand Down Expand Up @@ -284,7 +310,7 @@ macro styled_str(raw_content::String)
skipwhitespace!(state)
true
else
stywarn(state, "malformed styled string construct")
stywarn("malformed styled string construct", state, 1)
false
end
end
Expand All @@ -309,7 +335,10 @@ macro styled_str(raw_content::String)
tryparse(SimpleColor, '#' * color[3:end])
else
color valid_colornames ||
stywarn(state, "unrecognised named color '$color' (should be $(join(valid_colornames, ", ", ", or ")))")
stywarn(TaggedString("unrecognised named color '$color' (should be $(join(valid_colornames, ", ", ", or ")))",
[(ncodeunits("unrecognised named color '."):ncodeunits("unrecognised named color '")+ncodeunits(color),
:face => :warning)]),
state, 2 + length(color))
SimpleColor(Symbol(color))
end
end
Expand All @@ -322,7 +351,7 @@ macro styled_str(raw_content::String)
end
function read_underline!(state, lastchar)
if last(peek(state.s)) == '('
popfirst!(state.s)
ustart, _ = popfirst!(state.s)
skipwhitespace!(state)
ucolor_str, ucolor = if last(peek(state.s)) == ','
lastchar = last(popfirst!(state.s))
Expand All @@ -339,14 +368,17 @@ macro styled_str(raw_content::String)
if lastchar == ')'
lastchar = last(popfirst!(state.s))
else
stywarn(state, "malformed underline value, should be (<color>, <style>)")
stywarn("malformed underline value, should be (<color>, <style>)",
state, first(something(peek(state.s), (ustart+2, '.'))) - ustart + 1)
end
else
stywarn(state, "malformed underline value, should be (<color>, <style>)")
stywarn("malformed underline value, should be (<color>, <style>)",
state, first(something(peek(state.s), (ustart+2, '.'))) - ustart + 1)
ustyle = "straight"
end
ustyle valid_underline_styles ||
stywarn(state, "unrecognised underline style '$ustyle' (should be $(join(valid_underline_styles, ", ", ", or ")))")
stywarn("unrecognised underline style '$ustyle' (should be $(join(valid_underline_styles, ", ", ", or ")))",
state, 3 + length(ustyle))
Expr(:tuple, ucolor, QuoteNode(Symbol(ustyle)))
else
word, lastchar = readsymbol!(state, lastchar)
Expand Down Expand Up @@ -438,14 +470,17 @@ macro styled_str(raw_content::String)
ifelse(num isa Number, num, nothing)
else
invalid, lastchar = readsymbol!(state, lastchar)
stywarn(state, "invalid height $invalid, should be a natural number or positive float")
stywarn("invalid height $invalid, should be a natural number or positive float",
state, 3)
end
elseif key (:weight, :slant)
v, lastchar = readalph!(state, lastchar)
if key == :weight && v valid_weights
stywarn(state, "unrecognised weight '$v' (should be $(join(valid_weights, ", ", ", or ")))")
stywarn("unrecognised weight '$v' (should be $(join(valid_weights, ", ", ", or ")))",
state, 3)
elseif key == :slant && v valid_slants
stywarn(state, "unrecognised slant '$v' (should be $(join(valid_slants, ", ", ", or ")))")
stywarn("unrecognised slant '$v' (should be $(join(valid_slants, ", ", ", or ")))",
state, 3)
end
Symbol(v) |> QuoteNode
elseif key (:foreground, :background)
Expand All @@ -465,12 +500,12 @@ macro styled_str(raw_content::String)
inherit, lastchar = read_inherit!(state, lastchar)
inherit
else
stywarn(state, "uses unrecognised face key '$key'")
stywarn("uses unrecognised face key '$key'", state, length(str_key)+2)
end
if !any(k -> first(k.args) == key, kwargs)
push!(kwargs, Expr(:kw, key, val))
else
stywarn(state, "contains repeated face key '$key'")
stywarn("contains repeated face key '$key'", state, length(str_key)+2)
end
isempty(state.s) || last(peek(state.s)) != ',' || break
end
Expand Down Expand Up @@ -578,16 +613,20 @@ macro styled_str(raw_content::String)
interpolated!(state, i, char)
elseif char == '{'
begin_style!(state, i, char)
elseif char == '}' && !isempty(state.active_styles)
end_style!(state, i, char)
elseif char == '}'
if !isempty(state.active_styles)
end_style!(state, i, char)
else
stywarn("contains extranious style terminations", state, 2)
end
end
end
# Ensure that any trailing unstyled content is added
if state.point[] <= lastindex(state.content) + state.offset[]
addpart!(state, lastindex(state.content))
end
if !isempty(state.active_styles)
stywarn(state, "contains unterminated styled constructs")
stywarn("contains unterminated styled constructs")
end
end

Expand Down

0 comments on commit dec2bdd

Please sign in to comment.