Skip to content

Commit 0d514ce

Browse files
committed
no longer print bold by default in print_with_color
1 parent a7496bc commit 0d514ce

File tree

8 files changed

+108
-55
lines changed

8 files changed

+108
-55
lines changed

NEWS.md

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ Library improvements
5454
For example, to get orange warning messages, simply set `ENV["JULIA_WARN_COLOR"] = 208`.
5555
Please note that not all terminals support 256 colors.
5656

57+
* The function `print_with_color` no longer prints text in bold by default ([#18628]).
58+
Instead, the function now take a keyword argument `bold::Bool` which determines whether to print in bold or not.
59+
On some terminals, printing a color in non bold results in slightly darker colors being printed than when printing in bold.
60+
Therefore, light versions of the colors are now supported.
61+
For the available colors see the help entry on `print_with_color`.
62+
5763
* The default color for info messages has been changed from blue to cyan ([#18442]).
5864
This can be changed back to the original color by setting the environment variable `JULIA_INFO_COLOR` to `"blue"`.
5965
One way of doing this is by adding `ENV["JULIA_INFO_COLOR"] = :blue` to the `.juliarc.jl` file.
@@ -719,6 +725,7 @@ Language tooling improvements
719725
[#18473]: https://github.com/JuliaLang/julia/issues/18473
720726
[#18644]: https://github.com/JuliaLang/julia/issues/18644
721727
[#18690]: https://github.com/JuliaLang/julia/issues/18690
728+
[#18628]: https://github.com/JuliaLang/julia/issues/18628
722729
[#18839]: https://github.com/JuliaLang/julia/issues/18839
723730
[#18931]: https://github.com/JuliaLang/julia/issues/18931
724731
[#18977]: https://github.com/JuliaLang/julia/issues/18977

base/LineEdit.jl

+1
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ function write_prompt(terminal, p::Prompt)
629629
prefix = isa(p.prompt_prefix,Function) ? p.prompt_prefix() : p.prompt_prefix
630630
suffix = isa(p.prompt_suffix,Function) ? p.prompt_suffix() : p.prompt_suffix
631631
write(terminal, prefix)
632+
write(terminal, Base.text_colors[:bold])
632633
write(terminal, p.prompt)
633634
write(terminal, Base.text_colors[:normal])
634635
write(terminal, suffix)

base/REPL.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ function ip_matches_func(ip, func::Symbol)
111111
end
112112

113113
function display_error(io::IO, er, bt)
114+
print_with_color(Base.error_color(), io, "ERROR: "; bold = true)
114115
Base.with_output_color(Base.error_color(), io) do io
115-
print(io, "ERROR: ")
116116
# remove REPL-related frames from interactive printing
117117
eval_ind = findlast(addr->ip_matches_func(addr, :eval), bt)
118118
if eval_ind != 0

base/client.jl

+38-19
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@
44
## and REPL
55

66
const text_colors = AnyDict(
7-
:black => "\033[1m\033[30m",
8-
:red => "\033[1m\033[31m",
9-
:green => "\033[1m\033[32m",
10-
:yellow => "\033[1m\033[33m",
11-
:blue => "\033[1m\033[34m",
12-
:magenta => "\033[1m\033[35m",
13-
:cyan => "\033[1m\033[36m",
14-
:white => "\033[1m\033[37m",
15-
:normal => "\033[0m",
16-
:bold => "\033[1m",
7+
:black => "\033[30m",
8+
:red => "\033[31m",
9+
:green => "\033[32m",
10+
:yellow => "\033[33m",
11+
:blue => "\033[34m",
12+
:magenta => "\033[35m",
13+
:cyan => "\033[36m",
14+
:white => "\033[37m",
15+
:light_black => "\033[90m", # gray
16+
:light_red => "\033[91m",
17+
:light_green => "\033[92m",
18+
:light_yellow => "\033[93m",
19+
:light_blue => "\033[94m",
20+
:light_magenta => "\033[95m",
21+
:light_cyan => "\033[96m",
22+
:normal => "\033[0m",
23+
:default => "\033[39m",
24+
:bold => "\033[1m",
25+
)
26+
27+
const disable_text_style = AnyDict(
28+
:bold => "\033[22m",
29+
:normal => "",
30+
:default => "",
1731
)
1832

1933
for i in 0:255
20-
text_colors[i] = "\033[1m\033[38;5;$(i)m"
34+
text_colors[i] = "\033[38;5;$(i)m"
2135
end
2236

2337
# Create a docstring with an automatically generated list
2438
# of colors.
25-
const possible_formatting_symbols = [:normal, :bold]
2639
available_text_colors = collect(Iterators.filter(x -> !isa(x, Integer), keys(text_colors)))
40+
const possible_formatting_symbols = [:normal, :bold, :default]
2741
available_text_colors = cat(1,
2842
sort(intersect(available_text_colors, possible_formatting_symbols), rev=true),
2943
sort(setdiff( available_text_colors, possible_formatting_symbols)))
@@ -35,12 +49,15 @@ const available_text_colors_docstring =
3549
"""Dictionary of color codes for the terminal.
3650
3751
Available colors are: $available_text_colors_docstring as well as the integers 0 to 255 inclusive.
52+
53+
The color `:default` will print text in the default color while the color `:normal`
54+
will print text with all text properties (like boldness) reset.
3855
"""
3956
text_colors
4057

4158
have_color = false
42-
default_color_warn = :red
43-
default_color_error = :red
59+
default_color_warn = :light_red
60+
default_color_error = :light_red
4461
default_color_info = :cyan
4562
if is_windows()
4663
default_color_input = :normal
@@ -59,10 +76,12 @@ function repl_color(key, default)
5976
end
6077

6178
error_color() = repl_color("JULIA_ERROR_COLOR", default_color_error)
62-
warn_color() = repl_color("JULIA_WARN_COLOR", default_color_warn)
63-
info_color() = repl_color("JULIA_INFO_COLOR", default_color_info)
64-
input_color() = text_colors[repl_color("JULIA_INPUT_COLOR", default_color_input)]
65-
answer_color() = text_colors[repl_color("JULIA_ANSWER_COLOR", default_color_answer)]
79+
warn_color() = repl_color("JULIA_WARN_COLOR" , default_color_warn)
80+
info_color() = repl_color("JULIA_INFO_COLOR" , default_color_info)
81+
82+
# Print input and answer in bold.
83+
input_color() = text_colors[:bold] * text_colors[repl_color("JULIA_INPUT_COLOR", default_color_input)]
84+
answer_color() = text_colors[:bold] * text_colors[repl_color("JULIA_ANSWER_COLOR", default_color_answer)]
6685

6786
function repl_cmd(cmd, out)
6887
shell = shell_split(get(ENV,"JULIA_SHELL",get(ENV,"SHELL","/bin/sh")))
@@ -103,8 +122,8 @@ end
103122

104123
display_error(er) = display_error(er, [])
105124
function display_error(er, bt)
125+
print_with_color(Base.error_color(), STDERR, "ERROR: "; bold = true)
106126
with_output_color(Base.error_color(), STDERR) do io
107-
print(io, "ERROR: ")
108127
showerror(io, er, bt)
109128
println(io)
110129
end

base/test.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ immutable Pass <: Result
4343
value
4444
end
4545
function Base.show(io::IO, t::Pass)
46-
print_with_color(:green, io, "Test Passed\n")
46+
print_with_color(:green, io, "Test Passed\n"; bold = true)
4747
if !(t.orig_expr === nothing)
4848
print(io, " Expression: ", t.orig_expr)
4949
end
@@ -70,7 +70,7 @@ type Fail <: Result
7070
value
7171
end
7272
function Base.show(io::IO, t::Fail)
73-
print_with_color(:red, io, "Test Failed\n")
73+
print_with_color(:light_red, io, "Test Failed\n"; bold = true)
7474
print(io, " Expression: ", t.orig_expr)
7575
if t.test_type == :test_throws_wrong
7676
# An exception was thrown, but it was of the wrong type
@@ -102,7 +102,7 @@ type Error <: Result
102102
backtrace
103103
end
104104
function Base.show(io::IO, t::Error)
105-
print_with_color(:red, io, "Error During Test\n")
105+
print_with_color(:light_red, io, "Error During Test\n"; bold = true)
106106
if t.test_type == :test_nonbool
107107
println(io, " Expression evaluated to non-Boolean")
108108
println(io, " Expression: ", t.orig_expr)
@@ -140,7 +140,7 @@ type Broken <: Result
140140
orig_expr
141141
end
142142
function Base.show(io::IO, t::Broken)
143-
print_with_color(:yellow, io, "Test Broken\n")
143+
print_with_color(:yellow, io, "Test Broken\n"; bold = true)
144144
if t.test_type == :skipped && !(t.orig_expr === nothing)
145145
println(io, " Skipped: ", t.orig_expr)
146146
elseif !(t.orig_expr === nothing)
@@ -478,21 +478,21 @@ function print_test_results(ts::DefaultTestSet, depth_pad=0)
478478
# recursively walking the tree of test sets
479479
align = max(get_alignment(ts, 0), length("Test Summary:"))
480480
# Print the outer test set header once
481-
print_with_color(:white, rpad("Test Summary:",align," "), " | ")
481+
print_with_color(:white, rpad("Test Summary:",align," "), " | "; bold = true)
482482
if pass_width > 0
483-
print_with_color(:green, lpad("Pass",pass_width," "), " ")
483+
print_with_color(:green, lpad("Pass",pass_width," "), " "; bold = true)
484484
end
485485
if fail_width > 0
486-
print_with_color(:red, lpad("Fail",fail_width," "), " ")
486+
print_with_color(:light_red, lpad("Fail",fail_width," "), " "; bold = true)
487487
end
488488
if error_width > 0
489-
print_with_color(:red, lpad("Error",error_width," "), " ")
489+
print_with_color(:light_red, lpad("Error",error_width," "), " "; bold = true)
490490
end
491491
if broken_width > 0
492-
print_with_color(:yellow, lpad("Broken",broken_width," "), " ")
492+
print_with_color(:yellow, lpad("Broken",broken_width," "), " "; bold = true)
493493
end
494494
if total_width > 0
495-
print_with_color(:blue, lpad("Total",total_width, " "))
495+
print_with_color(:blue, lpad("Total",total_width, " "); bold = true)
496496
end
497497
println()
498498
# Recursively print a summary at every level
@@ -595,40 +595,40 @@ function print_counts(ts::DefaultTestSet, depth, align,
595595

596596
np = passes + c_passes
597597
if np > 0
598-
print_with_color(:green, lpad(string(np), pass_width, " "), " ")
598+
print_with_color(:green, lpad(string(np), pass_width, " "), " "; bold = true)
599599
elseif pass_width > 0
600600
# No passes at this level, but some at another level
601601
print(lpad(" ", pass_width), " ")
602602
end
603603

604604
nf = fails + c_fails
605605
if nf > 0
606-
print_with_color(:red, lpad(string(nf), fail_width, " "), " ")
606+
print_with_color(:light_red, lpad(string(nf), fail_width, " "), " "; bold = true)
607607
elseif fail_width > 0
608608
# No fails at this level, but some at another level
609609
print(lpad(" ", fail_width), " ")
610610
end
611611

612612
ne = errors + c_errors
613613
if ne > 0
614-
print_with_color(:red, lpad(string(ne), error_width, " "), " ")
614+
print_with_color(:light_red, lpad(string(ne), error_width, " "), " "; bold = true)
615615
elseif error_width > 0
616616
# No errors at this level, but some at another level
617617
print(lpad(" ", error_width), " ")
618618
end
619619

620620
nb = broken + c_broken
621621
if nb > 0
622-
print_with_color(:yellow, lpad(string(nb), broken_width, " "), " ")
622+
print_with_color(:yellow, lpad(string(nb), broken_width, " "), " "; bold = true)
623623
elseif broken_width > 0
624624
# None broken at this level, but some at another level
625625
print(lpad(" ", broken_width), " ")
626626
end
627627

628628
if np == 0 && nf == 0 && ne == 0 && nb == 0
629-
print_with_color(:blue, "No tests")
629+
print_with_color(:blue, "No tests"; bold = true)
630630
else
631-
print_with_color(:blue, lpad(string(subtotal), total_width, " "))
631+
print_with_color(:blue, lpad(string(subtotal), total_width, " "); bold = true)
632632
end
633633
println()
634634

base/util.jl

+19-13
Original file line numberDiff line numberDiff line change
@@ -303,35 +303,39 @@ end
303303

304304
## printing with color ##
305305

306-
function with_output_color(f::Function, color::Union{Int, Symbol}, io::IO, args...)
306+
function with_output_color(f::Function, color::Union{Int, Symbol}, io::IO, args...; bold::Bool = false)
307307
buf = IOBuffer()
308+
have_color && bold && print(buf, text_colors[:bold])
308309
have_color && print(buf, get(text_colors, color, color_normal))
309310
try f(IOContext(buf, io), args...)
310311
finally
311-
have_color && print(buf, color_normal)
312+
have_color && print(buf, get(disable_text_style, color, text_colors[:default]))
313+
have_color && (bold || color == :bold) && print(buf, disable_text_style[:bold])
312314
print(io, String(take!(buf)))
313315
end
314316
end
315317

316318
"""
317-
print_with_color(color::Union{Symbol, Int}, [io], strings...)
319+
print_with_color(color::Union{Symbol, Int}, [io], strings...; bold::Bool = false)
318320
319321
Print strings in a color specified as a symbol.
320322
321323
`color` may take any of the values $(Base.available_text_colors_docstring)
322324
or an integer between 0 and 255 inclusive. Note that not all terminals support 256 colors.
325+
If the keyword `bold` is given as `true`, the result will be printed in bold.
323326
"""
324-
print_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) =
325-
with_output_color(print, color, io, msg...)
326-
print_with_color(color::Union{Int, Symbol}, msg::AbstractString...) =
327-
print_with_color(color, STDOUT, msg...)
328-
println_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) =
329-
with_output_color(println, color, io, msg...)
330-
println_with_color(color::Union{Int, Symbol}, msg::AbstractString...) =
331-
println_with_color(color, STDOUT, msg...)
327+
print_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...; bold::Bool = false) =
328+
with_output_color(print, color, io, msg...; bold = bold)
329+
print_with_color(color::Union{Int, Symbol}, msg::AbstractString...; bold::Bool = false) =
330+
print_with_color(color, STDOUT, msg...; bold = bold)
331+
println_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...; bold::Bool = false) =
332+
with_output_color(println, color, io, msg...; bold = bold)
333+
println_with_color(color::Union{Int, Symbol}, msg::AbstractString...; bold::Bool = false) =
334+
println_with_color(color, STDOUT, msg...; bold = bold)
332335

333336
## warnings and messages ##
334337

338+
335339
"""
336340
info(msg...; prefix="INFO: ")
337341
@@ -349,7 +353,8 @@ MY INFO: hello world
349353
```
350354
"""
351355
function info(io::IO, msg...; prefix="INFO: ")
352-
println_with_color(info_color(), io, prefix, chomp(string(msg...)))
356+
print_with_color(info_color(), io, prefix; bold = true)
357+
println_with_color(info_color(), io, chomp(string(msg...)))
353358
end
354359
info(msg...; prefix="INFO: ") = info(STDERR, msg..., prefix=prefix)
355360

@@ -371,7 +376,8 @@ function warn(io::IO, msg...;
371376
(key in have_warned) && return
372377
push!(have_warned, key)
373378
end
374-
print_with_color(warn_color(), io, prefix, str)
379+
print_with_color(warn_color(), io, prefix; bold = true)
380+
print_with_color(warn_color(), io, str)
375381
if bt !== nothing
376382
show_backtrace(io, bt)
377383
end

doc/src/manual/interacting-with-julia.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,21 @@ end
310310
atreplinit(customize_colors)
311311
```
312312
313-
The available color keys in `Base.text_colors` are `:black`, `:red`, `:green`, `:yellow`, `:blue`,
314-
`:magenta`, `:cyan`, `:white`, `:normal`, and `:bold` as well as the integers 0 to 255 for terminals
315-
with 256 color support. Similarly, you can change the colors for the help and shell prompts and
313+
The available color keys can be seen by typing `Base.text_colors` in the help mode of the REPL.
314+
In addition, the integers 0 to 255 can be used as color keys for terminals
315+
with 256 color support.
316+
317+
You can also change the colors for the help and shell prompts and
316318
input and answer text by setting the appropriate field of `repl` in the `customize_colors` function
317319
above (respectively, `help_color`, `shell_color`, `input_color`, and `answer_color`). For the
318320
latter two, be sure that the `envcolors` field is also set to false.
319321
320-
You can also customize the color used to render warning and informational messages by setting
321-
the appropriate environment variable. For instance, to render warning messages in yellow and informational
322-
messages in cyan you can add the following to your `juliarc.jl` file:
322+
You can also customize the color used to render warning and informational messages by
323+
setting the appropriate environment variables. For instance, to render error, warning, and informational
324+
messages respectively in magenta, yellow, and cyan you can add the following to your `juliarc.jl` file:
323325
324326
```julia
327+
ENV["JULIA_ERROR_COLOR"] = :magenta
325328
ENV["JULIA_WARN_COLOR"] = :yellow
326329
ENV["JULIA_INFO_COLOR"] = :cyan
327330
```

test/misc.jl

+17
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,20 @@ let
483483
end
484484
@test c_18711 == 1
485485
end
486+
487+
let
488+
old_have_color = Base.have_color
489+
try
490+
eval(Base, :(have_color = true))
491+
buf = IOBuffer()
492+
print_with_color(:red, buf, "foo")
493+
# Check that we get back to normal text color in the end
494+
@test String(take!(buf)) == "\e[31mfoo\e[39m"
495+
496+
# Check that boldness is turned off
497+
print_with_color(:red, buf, "foo"; bold = true)
498+
@test String(take!(buf)) == "\e[1m\e[31mfoo\e[39m\e[22m"
499+
finally
500+
eval(Base, :(have_color = $(old_have_color)))
501+
end
502+
end

0 commit comments

Comments
 (0)