Skip to content

Commit

Permalink
support 256 colors in print_with_color and env variables (#18473)
Browse files Browse the repository at this point in the history
support 256 colors in print_with_color and env variables
  • Loading branch information
KristofferC authored Sep 21, 2016
1 parent aebf3ac commit 98f4f45
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ New language features
without having to scrub away prompts and outputs.
This can be disabled or enabled at will with `Base.REPL.enable_promptpaste(::Bool)`.

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

Language changes
----------------

Expand Down
14 changes: 10 additions & 4 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ const text_colors = AnyDict(
:bold => "\033[1m",
)

for i in 0:255
text_colors[i] = "\033[1m\033[38;5;$(i)m"
end

# Create a docstring with an automatically generated list
# of colors.
const possible_formatting_symbols = [:normal, :bold]
available_text_colors = collect(keys(text_colors))
available_text_colors = collect(filter(x -> !isa(x, Integer), keys(text_colors)))
available_text_colors = cat(1,
sort(intersect(available_text_colors, possible_formatting_symbols), rev=true),
sort(setdiff( available_text_colors, possible_formatting_symbols)))
Expand All @@ -30,7 +34,7 @@ const available_text_colors_docstring =

"""Dictionary of color codes for the terminal.
Available colors are: $available_text_colors_docstring.
Available colors are: $available_text_colors_docstring as well as the integers 0 to 255 inclusive.
"""
text_colors

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

function repl_color(key, default)
c = Symbol(get(ENV, key, ""))
haskey(text_colors, c) ? c : default
env_str = get(ENV, key, "")
c = tryparse(Int, env_str)
c_conv = isnull(c) ? Symbol(env_str) : get(c)
haskey(text_colors, c_conv) ? c_conv : default
end

warn_color() = repl_color("JULIA_WARN_COLOR", default_color_warn)
Expand Down
9 changes: 0 additions & 9 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3081,15 +3081,6 @@ Return `true` if `A` is a subset of or equal to `S`.
"""
issubset

"""
print_with_color(color::Symbol, [io], strings...)
Print strings in a color specified as a symbol.
`color` may take any of the values $(Base.available_text_colors_docstring).
"""
print_with_color

"""
stringmime(mime, x)
Expand Down
18 changes: 13 additions & 5 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ end

## printing with color ##

function with_output_color(f::Function, color::Symbol, io::IO, args...)
function with_output_color(f::Function, color::Union{Int, Symbol}, io::IO, args...)
buf = IOBuffer()
have_color && print(buf, get(text_colors, color, color_normal))
try f(buf, args...)
Expand All @@ -309,13 +309,21 @@ function with_output_color(f::Function, color::Symbol, io::IO, args...)
end
end

print_with_color(color::Symbol, io::IO, msg::AbstractString...) =
"""
print_with_color(color::Union{Symbol, Int}, [io], strings...)
Print strings in a color specified as a symbol.
`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.
"""
print_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) =
with_output_color(print, color, io, msg...)
print_with_color(color::Symbol, msg::AbstractString...) =
print_with_color(color::Union{Int, Symbol}, msg::AbstractString...) =
print_with_color(color, STDOUT, msg...)
println_with_color(color::Symbol, io::IO, msg::AbstractString...) =
println_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) =
with_output_color(println, color, io, msg...)
println_with_color(color::Symbol, msg::AbstractString...) =
println_with_color(color::Union{Int, Symbol}, msg::AbstractString...) =
println_with_color(color, STDOUT, msg...)

## warnings and messages ##
Expand Down
2 changes: 1 addition & 1 deletion doc/manual/interacting-with-julia.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ prompt you can add something like the following to your ``juliarc.jl`` file::
atreplinit(customize_colors)

The available color keys in ``Base.text_colors`` are ``:black``, ``:red``, ``:green``, ``:yellow``,
``:blue``, ``:magenta``, ``:cyan``, ``:white``, ``:normal``, and ``:bold``. Similarly, you can
``:blue``, ``:magenta``, ``:cyan``, ``:white``, ``:normal``, and ``:bold`` as well as the integers 0 to 255 for terminals with 256 color support. Similarly, you can
change the colors for the help and shell prompts and input and answer text by setting the
appropriate field of ``repl`` in the ``customize_colors`` function above (respectively, ``help_color``, ``shell_color``,
``input_color``, and ``answer_color``). For the latter two, be sure that the ``envcolors`` field
Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,13 @@ Text I/O
Print (using :func:`print`\ ) ``xs`` followed by a newline. If ``io`` is not supplied, prints to :obj:`STDOUT`\ .

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

.. Docstring generated from Julia source
Print strings in a color specified as a symbol.

``color`` may take any of the values ``:normal``\ , ``:bold``\ , ``:black``\ , ``:blue``\ , ``:cyan``\ , ``:green``\ , ``:magenta``\ , ``:red``\ , ``:white``\ , or ``:yellow``\ .
``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.

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

Expand Down

0 comments on commit 98f4f45

Please sign in to comment.