Skip to content

Commit 98f4f45

Browse files
authored
support 256 colors in print_with_color and env variables (#18473)
support 256 colors in print_with_color and env variables
1 parent aebf3ac commit 98f4f45

File tree

6 files changed

+32
-21
lines changed

6 files changed

+32
-21
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ New language features
1111
without having to scrub away prompts and outputs.
1212
This can be disabled or enabled at will with `Base.REPL.enable_promptpaste(::Bool)`.
1313

14+
* The function `print_with_color` can now take a color represented by an integer between 0 and 255 inclusive as its first argument.
15+
For a number to color mapping please refer to [this chart](https://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg).
16+
It is also possible to use numbers as colors in environment variables that customizes colors in the REPL.
17+
For example, to get orange warning messages, simply set `ENV["JULIA_WARN_COLOR"] = 208`.
18+
Please note that not all terminals support 256 colors.
19+
1420
Language changes
1521
----------------
1622

base/client.jl

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ const text_colors = AnyDict(
1616
:bold => "\033[1m",
1717
)
1818

19+
for i in 0:255
20+
text_colors[i] = "\033[1m\033[38;5;$(i)m"
21+
end
22+
1923
# Create a docstring with an automatically generated list
2024
# of colors.
2125
const possible_formatting_symbols = [:normal, :bold]
22-
available_text_colors = collect(keys(text_colors))
26+
available_text_colors = collect(filter(x -> !isa(x, Integer), keys(text_colors)))
2327
available_text_colors = cat(1,
2428
sort(intersect(available_text_colors, possible_formatting_symbols), rev=true),
2529
sort(setdiff( available_text_colors, possible_formatting_symbols)))
@@ -30,7 +34,7 @@ const available_text_colors_docstring =
3034

3135
"""Dictionary of color codes for the terminal.
3236
33-
Available colors are: $available_text_colors_docstring.
37+
Available colors are: $available_text_colors_docstring as well as the integers 0 to 255 inclusive.
3438
"""
3539
text_colors
3640

@@ -47,8 +51,10 @@ end
4751
color_normal = text_colors[:normal]
4852

4953
function repl_color(key, default)
50-
c = Symbol(get(ENV, key, ""))
51-
haskey(text_colors, c) ? c : default
54+
env_str = get(ENV, key, "")
55+
c = tryparse(Int, env_str)
56+
c_conv = isnull(c) ? Symbol(env_str) : get(c)
57+
haskey(text_colors, c_conv) ? c_conv : default
5258
end
5359

5460
warn_color() = repl_color("JULIA_WARN_COLOR", default_color_warn)

base/docs/helpdb/Base.jl

-9
Original file line numberDiff line numberDiff line change
@@ -3081,15 +3081,6 @@ Return `true` if `A` is a subset of or equal to `S`.
30813081
"""
30823082
issubset
30833083

3084-
"""
3085-
print_with_color(color::Symbol, [io], strings...)
3086-
3087-
Print strings in a color specified as a symbol.
3088-
3089-
`color` may take any of the values $(Base.available_text_colors_docstring).
3090-
"""
3091-
print_with_color
3092-
30933084
"""
30943085
stringmime(mime, x)
30953086

base/util.jl

+13-5
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ end
299299

300300
## printing with color ##
301301

302-
function with_output_color(f::Function, color::Symbol, io::IO, args...)
302+
function with_output_color(f::Function, color::Union{Int, Symbol}, io::IO, args...)
303303
buf = IOBuffer()
304304
have_color && print(buf, get(text_colors, color, color_normal))
305305
try f(buf, args...)
@@ -309,13 +309,21 @@ function with_output_color(f::Function, color::Symbol, io::IO, args...)
309309
end
310310
end
311311

312-
print_with_color(color::Symbol, io::IO, msg::AbstractString...) =
312+
"""
313+
print_with_color(color::Union{Symbol, Int}, [io], strings...)
314+
315+
Print strings in a color specified as a symbol.
316+
317+
`color` may take any of the values $(Base.available_text_colors_docstring)
318+
or an integer between 0 and 255 inclusive. Note that not all terminals support 256 colors.
319+
"""
320+
print_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) =
313321
with_output_color(print, color, io, msg...)
314-
print_with_color(color::Symbol, msg::AbstractString...) =
322+
print_with_color(color::Union{Int, Symbol}, msg::AbstractString...) =
315323
print_with_color(color, STDOUT, msg...)
316-
println_with_color(color::Symbol, io::IO, msg::AbstractString...) =
324+
println_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) =
317325
with_output_color(println, color, io, msg...)
318-
println_with_color(color::Symbol, msg::AbstractString...) =
326+
println_with_color(color::Union{Int, Symbol}, msg::AbstractString...) =
319327
println_with_color(color, STDOUT, msg...)
320328

321329
## warnings and messages ##

doc/manual/interacting-with-julia.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ prompt you can add something like the following to your ``juliarc.jl`` file::
296296
atreplinit(customize_colors)
297297

298298
The available color keys in ``Base.text_colors`` are ``:black``, ``:red``, ``:green``, ``:yellow``,
299-
``:blue``, ``:magenta``, ``:cyan``, ``:white``, ``:normal``, and ``:bold``. Similarly, you can
299+
``:blue``, ``:magenta``, ``:cyan``, ``:white``, ``:normal``, and ``:bold`` as well as the integers 0 to 255 for terminals with 256 color support. Similarly, you can
300300
change the colors for the help and shell prompts and input and answer text by setting the
301301
appropriate field of ``repl`` in the ``customize_colors`` function above (respectively, ``help_color``, ``shell_color``,
302302
``input_color``, and ``answer_color``). For the latter two, be sure that the ``envcolors`` field

doc/stdlib/io-network.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ Text I/O
502502
503503
Print (using :func:`print`\ ) ``xs`` followed by a newline. If ``io`` is not supplied, prints to :obj:`STDOUT`\ .
504504

505-
.. function:: print_with_color(color::Symbol, [io], strings...)
505+
.. function:: print_with_color(color::Union{Symbol, Int}, [io], strings...)
506506

507507
.. Docstring generated from Julia source
508508
509509
Print strings in a color specified as a symbol.
510510

511-
``color`` may take any of the values ``:normal``\ , ``:bold``\ , ``:black``\ , ``:blue``\ , ``:cyan``\ , ``:green``\ , ``:magenta``\ , ``:red``\ , ``:white``\ , or ``:yellow``\ .
511+
``color`` may take any of the values ``:normal``\ , ``:bold``\ , ``:black``\ , ``:blue``\ , ``:cyan``\ , ``:green``\ , ``:magenta``\ , ``:red``\ , ``:white``\ , or ``:yellow`` or an integer between 0 and 255 inclusive. Note that not all terminals support 256 colors.
512512

513513
.. function:: info(msg...; prefix="INFO: ")
514514

0 commit comments

Comments
 (0)