Class: HighLine::Style

Inherits:
Object show all
Defined in:
lib/highline/style.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defn = {}) ⇒ Style

Single color/styles have :name, :code, :rgb (possibly), :builtin Compound styles have :name, :list, :builtin


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/highline/style.rb', line 106

def initialize(defn = {})
  @definition = defn
  @name    = defn[:name]
  @code    = defn[:code]
  @rgb     = defn[:rgb]
  @list    = defn[:list]
  @builtin = defn[:builtin]
  if @rgb
    hex = self.class.rgb_hex(@rgb)
    @name ||= 'rgb_' + hex
  elsif @list
    @name ||= @list
  end
  self.class.index self unless defn[:no_index]
end

Instance Attribute Details

#builtinObject

Returns the value of attribute builtin


102
103
104
# File 'lib/highline/style.rb', line 102

def builtin
  @builtin
end

#listObject (readonly)

Returns the value of attribute list


101
102
103
# File 'lib/highline/style.rb', line 101

def list
  @list
end

#nameObject (readonly)

Returns the value of attribute name


101
102
103
# File 'lib/highline/style.rb', line 101

def name
  @name
end

#rgbObject

Returns the value of attribute rgb


102
103
104
# File 'lib/highline/style.rb', line 102

def rgb
  @rgb
end

Class Method Details

.ansi_rgb_to_hex(ansi_number) ⇒ Object


83
84
85
86
87
# File 'lib/highline/style.rb', line 83

def self.ansi_rgb_to_hex(ansi_number)
  raise "Invalid ANSI rgb code #{ansi_number}" unless (16..231).include?(ansi_number)
  parts = (ansi_number-16).to_s(6).rjust(3,'0').scan(/./).map{|d| (d.to_i*255.0/6.0).ceil}
  rgb_hex(*parts)
end

.code_indexObject


93
94
95
# File 'lib/highline/style.rb', line 93

def self.code_index
  @@code_index ||= {}
end

.index(style) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/highline/style.rb', line 43

def self.index(style)
  if style.name
    @@styles ||= {}
    @@styles[style.name] = style
  end
  if !style.list
    @@code_index ||= {}
    @@code_index[style.code] ||= []
    @@code_index[style.code].reject!{|indexed_style| indexed_style.name == style.name}
    @@code_index[style.code] << style
  end
  style
end

.listObject


89
90
91
# File 'lib/highline/style.rb', line 89

def self.list
  @@styles ||= {}
end

.rgb(*colors) ⇒ Object


67
68
69
70
71
72
73
74
75
76
# File 'lib/highline/style.rb', line 67

def self.rgb(*colors)
  hex = rgb_hex(*colors)
  name = ('rgb_' + hex).to_sym
  if style = list[name]
    style
  else
    parts = rgb_parts(hex)
    new(:name=>name, :code=>"\e[38;5;#{rgb_number(parts)}m", :rgb=>parts)
  end
end

.rgb_hex(*colors) ⇒ Object


57
58
59
60
61
# File 'lib/highline/style.rb', line 57

def self.rgb_hex(*colors)
  colors.map do |color|
    color.is_a?(Numeric) ? '%02x'%color : color.to_s
  end.join
end

.rgb_number(*parts) ⇒ Object


78
79
80
81
# File 'lib/highline/style.rb', line 78

def self.rgb_number(*parts)
  parts = parts.flatten
  16 + parts.inject(0) {|kode, part| kode*6 + (part/256.0*6.0).floor}
end

.rgb_parts(hex) ⇒ Object


63
64
65
# File 'lib/highline/style.rb', line 63

def self.rgb_parts(hex)
  hex.scan(/../).map{|part| part.to_i(16)}
end

.uncolor(string) ⇒ Object


97
98
99
# File 'lib/highline/style.rb', line 97

def self.uncolor(string)
  string.gsub(/\e\[\d+(;\d+)*m/, '')
end

Instance Method Details

#blueObject


150
151
152
# File 'lib/highline/style.rb', line 150

def blue
  @rgb && @rgb[2]
end

#brightObject


170
171
172
173
174
175
176
177
178
179
# File 'lib/highline/style.rb', line 170

def bright
  raise "Cannot create a bright variant of a style list (#{inspect})" if @list
  new_name = ('bright_'+@name.to_s).to_sym
  if style = self.class.list[new_name]
    style
  else
    new_rgb = @rgb == [0,0,0] ? [128, 128, 128] : @rgb.map {|color|  color==0 ? 0 : [color+128,255].min }
    variant(new_name, :increment=>60, :rgb=>new_rgb)
  end
end

#codeObject


134
135
136
137
138
139
140
# File 'lib/highline/style.rb', line 134

def code
  if @list
    @list.map{|element| HighLine.Style(element).code}.join
  else
    @code
  end
end

#color(string) ⇒ Object


130
131
132
# File 'lib/highline/style.rb', line 130

def color(string)
  code + string + HighLine::CLEAR
end

#dupObject


122
123
124
# File 'lib/highline/style.rb', line 122

def dup
  self.class.new(@definition)
end

#greenObject


146
147
148
# File 'lib/highline/style.rb', line 146

def green
  @rgb && @rgb[1]
end

#onObject


165
166
167
168
# File 'lib/highline/style.rb', line 165

def on
  new_name = ('on_'+@name.to_s).to_sym
  self.class.list[new_name] ||= variant(new_name, :increment=>10)
end

#redObject


142
143
144
# File 'lib/highline/style.rb', line 142

def red
  @rgb && @rgb[0]
end

#to_hashObject


126
127
128
# File 'lib/highline/style.rb', line 126

def to_hash
  @definition
end

#variant(new_name, options = {}) ⇒ Object


154
155
156
157
158
159
160
161
162
163
# File 'lib/highline/style.rb', line 154

def variant(new_name, options={})
  raise "Cannot create a variant of a style list (#{inspect})" if @list
  new_code = options[:code] || code
  if options[:increment]
    raise "Unexpected code in #{inspect}" unless new_code =~ /^(.*?)(\d+)(.*)/
    new_code = $1 + ($2.to_i + options[:increment]).to_s + $3
  end
  new_rgb = options[:rgb] || @rgb
  self.class.new(self.to_hash.merge(:name=>new_name, :code=>new_code, :rgb=>new_rgb))
end