Skip to content

Commit

Permalink
in printstyled, support underline and other flags (JuliaLang#40288)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkoniak authored and johanmon committed Jul 5, 2021
1 parent e475152 commit 1c2ecf8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
8 changes: 1 addition & 7 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,7 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m
# filename, separator, line
# use escape codes for formatting, printstyled can't do underlined and color
# codes are bright black (90) and underlined (4)
function print_underlined(io::IO, s...)
colored = get(io, :color, false)::Bool
start_s = colored ? "\033[90;4m" : ""
end_s = colored ? "\033[0m" : ""
print(io, start_s, s..., end_s)
end
print_underlined(io, pathparts[end], ":", line)
printstyled(io, pathparts[end], ":", line; color = :light_black, underline = true)

# inlined
printstyled(io, inlined ? " [inlined]" : "", color = :light_black)
Expand Down
36 changes: 28 additions & 8 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ Printing with the color `:nothing` will print the string without modifications.
"""
text_colors

function with_output_color(@nospecialize(f::Function), color::Union{Int, Symbol}, io::IO, args...; bold::Bool = false)
function with_output_color(@nospecialize(f::Function), color::Union{Int, Symbol}, io::IO, args...;
bold::Bool = false, underline::Bool = false, blink::Bool = false,
reverse::Bool = false, hidden::Bool = false)
buf = IOBuffer()
iscolor = get(io, :color, false)::Bool
try f(IOContext(buf, io), args...)
Expand All @@ -77,9 +79,22 @@ function with_output_color(@nospecialize(f::Function), color::Union{Int, Symbol}
print(io, str)
else
bold && color === :bold && (color = :nothing)
underline && color === :underline && (color = :nothing)
blink && color === :blink && (color = :nothing)
reverse && color === :reverse && (color = :nothing)
hidden && color === :hidden && (color = :nothing)
enable_ansi = get(text_colors, color, text_colors[:default]) *
(bold ? text_colors[:bold] : "")
disable_ansi = (bold ? disable_text_style[:bold] : "") *
(bold ? text_colors[:bold] : "") *
(underline ? text_colors[:underline] : "") *
(blink ? text_colors[:blink] : "") *
(reverse ? text_colors[:reverse] : "") *
(hidden ? text_colors[:hidden] : "")

disable_ansi = (hidden ? disable_text_style[:hidden] : "") *
(reverse ? disable_text_style[:reverse] : "") *
(blink ? disable_text_style[:blink] : "") *
(underline ? disable_text_style[:underline] : "") *
(bold ? disable_text_style[:bold] : "") *
get(disable_text_style, color, text_colors[:default])
first = true
for line in split(str, '\n')
Expand All @@ -94,18 +109,23 @@ function with_output_color(@nospecialize(f::Function), color::Union{Int, Symbol}
end

"""
printstyled([io], xs...; bold::Bool=false, color::Union{Symbol,Int}=:normal)
printstyled([io], xs...; bold::Bool=false, underline::Bool=false, blink::Bool=false, reverse::Bool=false, hidden::Bool=false, color::Union{Symbol,Int}=:normal)
Print `xs` in a color specified as a symbol or integer, optionally in bold.
`color` may take any of the values $(Base.available_text_colors_docstring)
or an integer between 0 and 255 inclusive. Note that not all terminals support 256 colors.
If the keyword `bold` is given as `true`, the result will be printed in bold.
If the keyword `underline` is given as `true`, the result will be printed underlined.
If the keyword `blink` is given as `true`, the result will blink.
If the keyword `reverse` is given as `true`, the result will have foreground and background colors inversed.
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, color::Union{Int,Symbol}=:normal) =
with_output_color(print, color, io, msg...; bold=bold)
printstyled(msg...; bold::Bool=false, color::Union{Int,Symbol}=:normal) =
printstyled(stdout, msg...; bold=bold, color=color)
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)

"""
Base.julia_cmd(juliapath=joinpath(Sys.BINDIR::String, julia_exename()))
Expand Down
20 changes: 20 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,26 @@ let buf = IOBuffer()
# Check that boldness is turned off
printstyled(buf_color, "foo"; bold=true, color=:red)
@test String(take!(buf)) == "\e[31m\e[1mfoo\e[22m\e[39m"

# Check that underline is turned off
printstyled(buf_color, "foo"; color = :red, underline = true)
@test String(take!(buf)) == "\e[31m\e[4mfoo\e[24m\e[39m"

# Check that blink is turned off
printstyled(buf_color, "foo"; color = :red, blink = true)
@test String(take!(buf)) == "\e[31m\e[5mfoo\e[25m\e[39m"

# Check that reverse is turned off
printstyled(buf_color, "foo"; color = :red, reverse = true)
@test String(take!(buf)) == "\e[31m\e[7mfoo\e[27m\e[39m"

# Check that hidden is turned off
printstyled(buf_color, "foo"; color = :red, hidden = true)
@test String(take!(buf)) == "\e[31m\e[8mfoo\e[28m\e[39m"

# Check that all options can be turned on simultaneously
printstyled(buf_color, "foo"; color = :red, bold = true, underline = true, blink = true, reverse = true, hidden = true)
@test String(take!(buf)) == "\e[31m\e[1m\e[4m\e[5m\e[7m\e[8mfoo\e[28m\e[27m\e[25m\e[24m\e[22m\e[39m"
end

abstract type DA_19281{T, N} <: AbstractArray{T, N} end
Expand Down

0 comments on commit 1c2ecf8

Please sign in to comment.