Class: Palette::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/palette/rule.rb

Constant Summary collapse

@@max_length =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Rule

Returns a new instance of Rule.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/palette/rule.rb', line 6

def initialize(name, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  @name = name.to_s

  @@max_length = @name.length if @name.length > @@max_length

  @fg   = options[:fg]  || args.first
  @bg   = options[:bg]  || (args.length > 1 ? args.last : nil)
  @gui  = options[:gui]
end

Instance Attribute Details

#bgObject (readonly)

Returns the value of attribute bg.



4
5
6
# File 'lib/palette/rule.rb', line 4

def bg
  @bg
end

#fgObject (readonly)

Returns the value of attribute fg.



4
5
6
# File 'lib/palette/rule.rb', line 4

def fg
  @fg
end

#guiObject (readonly)

Returns the value of attribute gui.



4
5
6
# File 'lib/palette/rule.rb', line 4

def gui
  @gui
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/palette/rule.rb', line 4

def name
  @name
end

Instance Method Details

#to_sObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/palette/rule.rb', line 18

def to_s
  return "" if fg.nil? && bg.nil? && gui.nil?
  output = ["hi #{sprintf("%-#{@@max_length}s", name)}"]

  if fg
    color = Palette::Color.new(fg)
    output << %{guifg=##{color.to_hex}}
    output << %{ctermfg=#{sprintf("%-3s", color.to_cterm)}}
  end

  if bg
    color = Palette::Color.new(bg)
    output << %{guibg=##{color.to_hex}}
    output << %{ctermbg=#{sprintf("%-3s", color.to_cterm)}}
  end

  if gui
    output << %{gui=#{gui}}
    if gui !~ /italic/
      output << %{cterm=#{gui}}
    end
  end

  output.join(" ").strip
end