Class: Ruptr::TTYColors::ANSICodes

Inherits:
Ruptr::TTYColors show all
Defined in:
lib/ruptr/tty_colors.rb

Constant Summary collapse

SUPPORTED =
%i[bright faint italic underline reverse strike overstrike].freeze
COLORS =
%i[black red green yellow blue magenta cyan white].freeze

Instance Method Summary collapse

Methods inherited from Ruptr::TTYColors

for, probably_ansi_terminal?, seems_to_contain_formatting_codes?, want_cli_colors?

Instance Method Details

#supports?(*args) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/ruptr/tty_colors.rb', line 30

def supports?(*args)
  args.all? do |name|
    case name
    when :color, :bg_color then COLORS.include?(name)
    else SUPPORTED.include?(name)
    end
  end
end

#wrap(s, **opts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruptr/tty_colors.rb', line 39

def wrap(s, **opts)
  b = +''; e = +''
  if opts[:bright] then b << "\e[1m"; e << "\e[22m" end
  if opts[:faint] then b << "\e[2m"; e << "\e[22m" end
  if opts[:italic] then b << "\e[3m"; e << "\e[23m" end
  if opts[:underline] then b << "\e[4m"; e << "\e[24m" end
  if opts[:reverse] then b << "\e[7m"; e << "\e[27m" end
  if opts[:strike] then b << "\e[9m"; e << "\e[29m" end
  if opts[:overstrike] then b << "\e[53m"; e << "\e[55m" end
  if (color_index = COLORS.find_index(opts[:color]))
    b << "\e[#{30 + color_index}m"
    e << "\e[39m"
  end
  if (color_index = COLORS.find_index(opts[:bg_color]))
    b << "\e[#{40 + color_index}m"
    e << "\e[49m"
  end
  "#{b}#{s}#{e}"
end