Class: VimColorScheme::HighlightNode

Inherits:
Object
  • Object
show all
Defined in:
lib/vimcolorscheme/highlight_node.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HighlightNode.



3
4
5
6
# File 'lib/vimcolorscheme/highlight_node.rb', line 3

def initialize name, options = {}
  @name    = name
  @options = options
end

Instance Method Details

#attr_to_s(attribute) ⇒ Object

Converts an attribute to string. This accounts for cases such as :none and :reverse and returns the appropriate string.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vimcolorscheme/highlight_node.rb', line 67

def attr_to_s attribute
  case attribute
  when Array
    attribute.map { |a| attr_to_s(a) }.join(',')
  when :none
    'NONE'
  when :reverse
    'REVERSE'
  else
    attribute
  end
end

#cterm(*args) ⇒ Object



99
100
101
102
# File 'lib/vimcolorscheme/highlight_node.rb', line 99

def cterm *args
  @options[:cterm] = args if args.length > 0
  @options[:cterm]
end

#ctermbg(new_ctermbg = nil) ⇒ Object



104
105
106
107
# File 'lib/vimcolorscheme/highlight_node.rb', line 104

def ctermbg new_ctermbg = nil
  @options[:ctermbg] = new_ctermbg if new_ctermbg
  @options[:ctermbg]
end

#ctermfg(new_ctermfg = nil) ⇒ Object



109
110
111
112
# File 'lib/vimcolorscheme/highlight_node.rb', line 109

def ctermfg new_ctermfg = nil
  @options[:ctermfg] = new_ctermfg if new_ctermfg
  @options[:ctermfg]
end

#gui(*args) ⇒ Object

The following are just default accessors for the various members of the options hash on this object.



84
85
86
87
# File 'lib/vimcolorscheme/highlight_node.rb', line 84

def gui *args
  @options[:gui] = args if args.length > 0
  @options[:gui]
end

#guibg(new_guibg = nil) ⇒ Object



89
90
91
92
# File 'lib/vimcolorscheme/highlight_node.rb', line 89

def guibg new_guibg = nil
  @options[:guibg] = new_guibg if new_guibg
  @options[:guibg]
end

#guifg(new_guifg = nil) ⇒ Object



94
95
96
97
# File 'lib/vimcolorscheme/highlight_node.rb', line 94

def guifg new_guifg = nil
  @options[:guifg] = new_guifg if new_guifg
  @options[:guifg]
end

#processObject

Processes the values of a node. Basically, if there are values that have not been set, we try to guess them. So we convert gui colors to cterm colors if the cterm colors are not present and default to :none if that isn’t possible and so on.

This gets called in the to_s method so there’s no need to call it explicitly.



15
16
17
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
43
44
45
46
47
48
49
# File 'lib/vimcolorscheme/highlight_node.rb', line 15

def process
  # Process colors. Set cterm if none, gui if none, based on their
  # counterparts.
  if !gui and cterm
    gui(cterm)
  end

  if !guifg and ctermfg
    guifg Hex2Term.convert(ctermfg)
  end

  if !guibg and ctermbg
    guibg Hex2Term.convert(ctermbg)
  end

  if !cterm and gui
    cterm(gui)
  end

  if !ctermfg and guifg
    ctermfg Hex2Term.convert(guifg)
  end

  if !ctermbg and guibg
    ctermbg Hex2Term.convert(guibg)
  end

  # Default things to none if they are nil or false.
  gui     :none unless gui
  guifg   :none unless guifg
  guibg   :none unless guibg
  cterm   :none unless cterm
  ctermfg :none unless ctermfg
  ctermbg :none unless ctermbg
end

#to_sObject

Converts the Node to a valid entry in a vim color scheme file.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vimcolorscheme/highlight_node.rb', line 52

def to_s
  # Make sure the node has been processed before converting to string.
  process

  result  = "highlight #{@name.to_s} "
  result += "gui=#{attr_to_s(gui)} "
  result += "guifg=#{attr_to_s(guifg)} "
  result += "guibg=#{attr_to_s(guibg)} "
  result += "cterm=#{attr_to_s(cterm)} "
  result += "ctermfg=#{attr_to_s(ctermfg)} "
  result += "ctermbg=#{attr_to_s(ctermbg)}\n"
end