Class: Vic::Colorscheme

Inherits:
Object
  • Object
show all
Defined in:
lib/vic/colorscheme.rb

Defined Under Namespace

Classes: Highlight, HighlightSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colors_name, &block) ⇒ Colorscheme

A new instance of Colorscheme. If a block is given with no arguments, the the block will be evaluated in the context of the new colorscheme. Otherwise the block will yield self.

Parameters:

  • colors_name (String)

    the name of the colorscheme

  • block (Proc)

    the block to be evaluated



12
13
14
15
16
17
# File 'lib/vic/colorscheme.rb', line 12

def initialize(colors_name, &block)
  @colors_name = colors_name
  if block_given?
    block.arity == 0 ? instance_eval(&block) : yield(self)
  end
end

Instance Attribute Details

#colors_nameObject

Returns the value of attribute colors_name.



3
4
5
# File 'lib/vic/colorscheme.rb', line 3

def colors_name
  @colors_name
end

#information(inf = {}) ⇒ Hash Also known as: info

Returns/sets the information attribute

Parameters:

  • information (Hash)

    the information, :author => ‘Joel Holdbrooks’

Returns:

  • (Hash)


23
24
25
# File 'lib/vic/colorscheme.rb', line 23

def information
  @information
end

Instance Method Details

#backgroundString

Returns the background color.

Returns:

  • (String)

    ‘light’ or ‘dark’



31
32
33
# File 'lib/vic/colorscheme.rb', line 31

def background
  @background ||= background!
end

#background!Object

Sets the background color by attempting to determine it.

@return the background attribute



48
49
50
51
52
53
54
55
56
# File 'lib/vic/colorscheme.rb', line 48

def background!
  @background =
    if normal = highlights.find_by_group('Normal')
      return 'dark' unless color = normal.guibg
      color.partition('#').last.to_i(16) <= 8421504 ? 'dark' : 'light'
    else
      'dark'
    end
end

#background=(light_or_dark) ⇒ String

Sets the background color.

Parameters:

  • a (String)

    value of ‘light’ or ‘dark’

Returns:

  • (String)

    the background attribute



39
40
41
42
43
# File 'lib/vic/colorscheme.rb', line 39

def background=(light_or_dark)
  unless (light_or_dark =~ /^light$|^dark$/).nil?
    @background = light_or_dark
  end
end

#find_highlight(group) ⇒ Object



83
84
85
# File 'lib/vic/colorscheme.rb', line 83

def find_highlight(group)
  highlight_set.find_by_group(group)
end

#headerString

Returns the colorscheme header.

Returns:

  • (String)

    the colorscheme header



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vic/colorscheme.rb', line 117

def header
  <<-EOT.gsub(/^ {6}/, '')
  " Vim color file
  #{info.to_a.map do |pair|
    pair.join(": ").tap {|s| s[0] = '" ' + s[0].upcase }
  end.join("\n")}
  set background=#{background}
  hi clear

  if exists("syntax_on")
    syntax reset
  endif

  let g:colors_name="#{colors_name}"
  EOT
end

#highlight(group, args = {}) ⇒ Vic::Highlight Also known as: hi

Creates a new highlight

If inside of a language block the langauge name is automatcially prepended to the group name of the new highlight.

Returns:

  • (Vic::Highlight)

    the new highlight

See Also:

  • Highlight


71
72
73
74
75
76
77
78
79
80
# File 'lib/vic/colorscheme.rb', line 71

def highlight(group, args={})
  return if args.empty?

  if h = find_highlight(group)
    h.update_arguments! args
  else
    h = Highlight.new "#{language}#{group}", args
    highlight_set.add h
  end
end

#highlight_setObject Also known as: highlights

Returns the set of highlights for the colorscheme



59
60
61
# File 'lib/vic/colorscheme.rb', line 59

def highlight_set
  @highlight_set ||= HighlightSet.new
end

#language(name = nil, &block) ⇒ String

Returns the current language or temporarily sets the language to name if a block is given. If a block is given with no arguments, it will be evaluated in the context of the colorscheme, otherwise it will yield the colorscheme.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    the name of the language

  • block (Proc)

    the block to be evalauted

Returns:

  • (String)

    the current language



103
104
105
106
107
108
109
110
111
112
# File 'lib/vic/colorscheme.rb', line 103

def language(name=nil, &block)
  if @language and not name
    return @language
  elsif name and block_given?
    previous_language = self.language
    self.language = name
    block.arity == 0 ? instance_eval(&block) : yield(self)
    self.language = previous_language
  end
end

#language=(name) ⇒ String

Sets the current language to name. Any new highlights created will have the language name automatically prepended.

Returns:

  • (String)

    the new language name



91
92
93
# File 'lib/vic/colorscheme.rb', line 91

def language=(name)
  @language = name
end

#writeString

Returns the colorscheme as a string

Returns:

  • (String)

    the colorscheme



137
138
139
# File 'lib/vic/colorscheme.rb', line 137

def write
  [header, highlights.map(&:write)].join("\n")
end