Class: AnsiColor::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi_color/helpers.rb

Class Method Summary collapse

Class Method Details

.build_open_tag(args = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ansi_color/helpers.rb', line 19

def self.build_open_tag(args={})
  # X;FG;BG;Bm
  return if args.size == 0
  options = {
    :color => nil,
    :background => nil,
    :effects => nil
  }

  args.each do |k, v|
    unless options.keys.include?(k)
      valid_options = options.keys.join(', ')
      raise ArgumentError, "#{k} is not a valid argument. Valid options are #{valid_options}"
    end
  end

  options.merge!(args)

  fg = code_from_name(options[:color]) unless options[:color].nil?
  bg = code_from_background_name(options[:background]) unless options[:background].nil?

  if effects = options[:effects]
    effects = Array(effects).flatten
    if (effects - EFFECTS.keys).empty?
      effects = Effects.new(effects).to_codes!
    else
      raise InvalidEffect, "The only valid effects are #{EFFECTS.keys.join(', ')}"
    end
  end

  tag = [fg || 0, bg, effects].compact.join(';')
  "#{E + tag}m"
end

.code_from_background_name(name) ⇒ Object



11
12
13
# File 'lib/ansi_color/helpers.rb', line 11

def self.code_from_background_name(name)
  BACKGROUND_COLORS[name] || raise(InvalidColorName, "#{name} is not a background colour")
end

.code_from_name(name) ⇒ Object



3
4
5
# File 'lib/ansi_color/helpers.rb', line 3

def self.code_from_name(name)
  FOREGROUND_COLORS[name] || raise(InvalidColorName, "#{name} is not a colour")
end

.name_from_background_code(code) ⇒ Object



15
16
17
# File 'lib/ansi_color/helpers.rb', line 15

def self.name_from_background_code(code)
  BACKGROUND_COLORS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a background colour code")
end

.name_from_code(code) ⇒ Object



7
8
9
# File 'lib/ansi_color/helpers.rb', line 7

def self.name_from_code(code)
  FOREGROUND_COLORS.index(code.to_i) || raise(InvalidColorCode, "#{code} is not a colour code")
end

.resetObject



53
54
55
# File 'lib/ansi_color/helpers.rb', line 53

def self.reset
  RESET
end