Class: Test::Unit::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/test/unit/color.rb

Defined Under Namespace

Classes: Error, ParseError

Constant Summary collapse

NAMES =
["black", "red", "green", "yellow",
"blue", "magenta", "cyan", "white"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Color

Returns a new instance of Color.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/test/unit/color.rb', line 30

def initialize(name, options={})
  @name = name
  if options.has_key?(:foreground)
    if options[:foreground].nil?
      @background = false
    else
      @background = !options[:foreground]
    end
  else
    @background = options[:background]
  end
  @intensity = options[:intensity]
  @bold = options[:bold]
  @italic = options[:italic]
  @underline = options[:underline]
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/test/unit/color.rb', line 29

def name
  @name
end

Class Method Details

.parse_256_color(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/test/unit/color.rb', line 13

def parse_256_color(string)
  case string
  when /\A([0-5])([0-5])([0-5])\z/
    red, green, blue = $1, $2, $3
    red.to_i * 36 + green.to_i * 6 + blue.to_i + 16
  else
    message = "must be 'RGB' format and R, G and B " +
      "are in 0-5: #{string.inspect}"
    raise ParseError, message
  end
end

Instance Method Details

#+(other) ⇒ Object



106
107
108
# File 'lib/test/unit/color.rb', line 106

def +(other)
  MixColor.new([self, other])
end

#==(other) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/test/unit/color.rb', line 71

def ==(other)
  self.class === other and
    [name, background?, intensity?,
     bold?, italic?, underline?] ==
    [other.name, other.background?, other.intensity?,
     other.bold?, other.italic?, other.underline?]
end

#background? ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/test/unit/color.rb', line 51

def background?
  @background
end

#bold? ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/test/unit/color.rb', line 59

def bold?
  @bold
end

#escape_sequence ⇒ Object



102
103
104
# File 'lib/test/unit/color.rb', line 102

def escape_sequence
  "\e[#{sequence.join(';')}m"
end

#foreground? ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/test/unit/color.rb', line 47

def foreground?
  not background?
end

#intensity? ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/test/unit/color.rb', line 55

def intensity?
  @intensity
end

#italic? ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/test/unit/color.rb', line 63

def italic?
  @italic
end

#sequence ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/test/unit/color.rb', line 79

def sequence
  sequence = []
  if @name == "none"
  elsif @name == "reset"
    sequence << "0"
  else
    if NAMES.include?(@name)
      color_parameter = foreground? ? 3 : 4
      color_parameter += 6 if intensity?
      color = NAMES.index(@name)
      sequence << "#{color_parameter}#{color}"
    else
      sequence << (foreground? ? "38" : "48")
      sequence << "5"
      sequence << self.class.parse_256_color(@name).to_s
    end
  end
  sequence << "1" if bold?
  sequence << "3" if italic?
  sequence << "4" if underline?
  sequence
end

#underline? ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/test/unit/color.rb', line 67

def underline?
  @underline
end