Face

November 13, 2023 ยท View on GitHub

With the Reline::Face class, you can modify the text color and text decorations in your terminal emulator. This is primarily used to customize the appearance of the method completion dialog in IRB.

Usage

ex: Change the background color of the completion dialog cyan to blue

Reline::Face.config(:completion_dialog) do |conf|
  conf.define :default, foreground: :white, background: :blue
  #                                                     ^^^^^ `:cyan` by default
  conf.define :enhanced, foreground: :white, background: :magenta
  conf.define :scrollbar, foreground: :white, background: :blue
end

If you provide the above code to an IRB session in some way, you can apply the configuration. It's generally done by writing it in .irbrc.

Regarding .irbrc, please refer to the following link: https://docs.ruby-lang.org/en/master/IRB.html

Available parameters

Reline::Face internally creates SGR (Select Graphic Rendition) code according to the block parameter of Reline::Face.config method.

KeyValueSGR Code (numeric part following "\e[")
:foreground:black30
:red31
:green32
:yellow33
:blue34
:magenta35
:cyan36
:white37
:bright_black90
:gray90
:bright_red91
:bright_green92
:bright_yellow93
:bright_blue94
:bright_magenta95
:bright_cyan96
:bright_white97
:background:black40
:red41
:green42
:yellow43
:blue44
:magenta45
:cyan46
:white47
:bright_black100
:gray100
:bright_red101
:bright_green102
:bright_yellow103
:bright_blue104
:bright_magenta105
:bright_cyan106
:bright_white107
:style:reset0
:bold1
:faint2
:italicized3
:underlined4
:slowly_blinking5
:blinking5
:rapidly_blinking6
:negative7
:concealed8
:crossed_out9
  • The value for :style can be both a Symbol and an Array
      # Single symbol
      conf.define :default, style: :bold
      # Array
      conf.define :default, style: [:bold, :negative]
    
  • The availability of specific SGR codes depends on your terminal emulator
  • You can specify a hex color code to :foreground and :background color like foreground: "#FF1020". Its availability also depends on your terminal emulator

Debugging

You can see the current Face configuration by Reline::Face.configs method

Example:

irb(main):001:0> Reline::Face.configs
=>
{:default=>
  {:default=>{:style=>:reset, :escape_sequence=>"\e[0m"},
   :enhanced=>{:style=>:reset, :escape_sequence=>"\e[0m"},
   :scrollbar=>{:style=>:reset, :escape_sequence=>"\e[0m"}},
 :completion_dialog=>
  {:default=>{:foreground=>:white, :background=>:cyan, :escape_sequence=>"\e[0m\e[37;46m"},
   :enhanced=>{:foreground=>:white, :background=>:magenta, :escape_sequence=>"\e[0m\e[37;45m"},
   :scrollbar=>{:foreground=>:white, :background=>:cyan, :escape_sequence=>"\e[0m\e[37;46m"}}}

256-Color and TrueColor

Reline will automatically detect if your terminal emulator supports truecolor with ENV['COLORTERM] in 'truecolor' | '24bit'. When this env is not set, Reline will fallback to 256-color. If your terminal emulator supports truecolor but does not set COLORTERM env, add this line to .irbrc.

Reline::Face.force_truecolor