logo

Decorating Text Output with Built-in Functions in Julia 📂Julia

Decorating Text Output with Built-in Functions in Julia

Code

Using the function printstyled(string; color = color) allows you to decorate the outputted function. As input for the keyword argument color, symbols and integers $(0 \le n \le 255)$ are possible. Note that strings are not allowed.

The available symbols include not only colors but also options like :blink, :reverse, etc. These can also be applied by entering them as keyword arguments like blink = true, bold = true.

  • :normal
  • :default
  • :blink
  • :bold
  • :hidden
  • :nothing
  • :reverse
  • :underline
  • :white
  • :light_white
  • :black
  • :light_black
  • :blue
  • :light_blue
  • :cyan
  • :light_cyan
  • :green
  • :light_green
  • :magenta
  • :light_magenta
  • :red
  • :light_red
  • :yellow
  • :light_yellow
symbols = [:normal :default :blink :bold :hidden :nothing :reverse :underline :white :light_white :black :light_black :blue :light_blue :cyan :light_cyan :green :light_green :magenta :light_magena :red :light_red :yellow :light_yellow]

for i ∈ 1:length(symbols)
    printstyled("Hello ($(symbols[i]))\n", color = symbols[i])
end

Entering Base.text_colors returns all possible values for the keyword argument (including symbols).

julia> Base.text_colors
Dict{Union{Int64, Symbol}, String} with 280 entries:
  56     => "\e[38;5;56m"
  35     => "\e[38;5;35m"
  60     => "\e[38;5;60m"
  220    => "\e[38;5;220m"
  :blink => "\e[5m"
  67     => "\e[38;5;67m"
  215    => "\e[38;5;215m"
  73     => "\e[38;5;73m"
  251    => "\e[38;5;251m"
  115    => "\e[38;5;115m"
  ⋮      => ⋮

See Also

The package Crayons.jl can also be used.

Environment

  • OS: Windows11
  • Version: Julia 1.9.4