Method: HighLine.find_or_create_style

Defined in:
lib/highline/style.rb

.find_or_create_style(arg) ⇒ Style

Search for a Style with the given properties and return it. If there’s no matched Style, it creates one. You can pass a Style, String or a Hash.

Parameters:

Returns:

  • (Style)

    found or creted Style



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/highline/style.rb', line 30

def self.find_or_create_style(arg)
  if arg.is_a?(Style)
    Style.list[arg.name] || Style.index(arg)
  elsif arg.is_a?(::String) && arg =~ /^\e\[/ # arg is a code
    styles = Style.code_index[arg]
    if styles
      styles.first
    else
      Style.new(code: arg)
    end
  elsif Style.list[arg]
    Style.list[arg]
  elsif HighLine.color_scheme && HighLine.color_scheme[arg]
    HighLine.color_scheme[arg]
  elsif arg.is_a?(Hash)
    Style.new(arg)
  elsif arg.to_s.downcase =~ /^rgb_([a-f0-9]{6})$/
    Style.rgb(Regexp.last_match(1))
  elsif arg.to_s.downcase =~ /^on_rgb_([a-f0-9]{6})$/
    Style.rgb(Regexp.last_match(1)).on
  else
    raise NameError, "#{arg.inspect} is not a defined Style"
  end
end