Skip to content

Commit

Permalink
Add namedstyle argument to printstyled
Browse files Browse the repository at this point in the history
This adds an (empty) global dict `named_styles` and an argument
`namedstyle` for the `printstyled` method that takes default color and
text properties from the `named_styles` dict.
  • Loading branch information
goerz committed Aug 18, 2021
1 parent a03392a commit eb32c1c
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const disable_text_style = Dict{Symbol,String}(
:nothing => "",
)

const named_styles = Dict{String, NamedTuple{(:color, :properties), Tuple{Union{Symbol,Int}, Dict{Symbol, Bool}}}}()

# Create a docstring with an automatically generated list
# of colors.
let color_syms = collect(Iterators.filter(x -> !isa(x, Integer), keys(text_colors))),
Expand Down Expand Up @@ -122,10 +124,19 @@ If the keyword `reverse` is given as `true`, the result will have foreground and
If the keyword `hidden` is given as `true`, the result will be hidden.
Keywords can be given in any combination.
"""
printstyled(io::IO, msg...; bold::Bool=false, underline::Bool=false, blink::Bool=false, reverse::Bool=false, hidden::Bool=false, color::Union{Int,Symbol}=:normal) =
with_output_color(print, color, io, msg...; bold=bold, underline=underline, blink=blink, reverse=reverse, hidden=hidden)
printstyled(msg...; bold::Bool=false, underline::Bool=false, blink::Bool=false, reverse::Bool=false, hidden::Bool=false, color::Union{Int,Symbol}=:normal) =
printstyled(stdout, msg...; bold=bold, underline=underline, blink=blink, reverse=reverse, hidden=hidden, color=color)
function printstyled(io::IO, msg...; namedstyle::Union{Nothing, String}=nothing, kwargs...)
if namedstyle nothing
style = named_styles[namedstyle]
properties = merge(style.properties, kwargs)
color = pop!(properties, :color, style.color)
else
properties = copy(kwargs)
color = pop!(properties, :color, :normal)
end
with_output_color(print, color, io, msg...; properties...)
end
printstyled(msg...; namedstyle::Union{Nothing, String}=nothing, kwargs...) =
printstyled(stdout, msg...; namedstyle=namedstyle, kwargs...)

"""
Base.julia_cmd(juliapath=joinpath(Sys.BINDIR::String, julia_exename()))
Expand Down

0 comments on commit eb32c1c

Please sign in to comment.