Class: Vic::ColorScheme

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, &block) ⇒ ColorScheme

Returns a new instance of ColorScheme.



5
6
7
8
9
10
11
12
13
14
# File 'lib/vic/color_scheme.rb', line 5

def initialize(title, &block)
  @title = title
  @highlights = []
  @links = []
  @info = OpenStruct.new

  if block_given?
    block.arity.zero? ? instance_eval(&block) : yield(self)
  end
end

Instance Attribute Details

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_info(data = {}) ⇒ Hash

Updates the colorscheme information

Parameters:

  • data (Hash) (defaults to: {})

    the data to use

Returns:

  • (Hash)

    the colorscheme information



48
49
50
# File 'lib/vic/color_scheme.rb', line 48

def add_info(data = {})
  self.info = @info.marshal_dump.merge(data)
end

#backgroundNilClass, ...

Return the background setting

Returns:

  • (NilClass, String, Symbol)

    the background setting



56
57
58
# File 'lib/vic/color_scheme.rb', line 56

def background
  @background
end

#background!String

Sets the background automatically

By default this will return “dark”. If a highlight with the ‘group` “Normal” and a `guibg` setting has been added to the colorscheme, it will determine the setting to use. If the `guibg` is strictly less than “#808080” the background will be set to “dark” otherwise it will be set to “light”.

Returns:

  • (String)

    the background setting



88
89
90
91
92
# File 'lib/vic/color_scheme.rb', line 88

def background!
  normal = find_highlight(:Normal)
  return @background = 'dark' unless normal && normal.guibg
  @background = normal.guibg[1..-1].to_i(16) < 8421504 ? 'dark' : 'light'
end

#background=(setting) ⇒ String, Symbol

Sets the background

Parameters:

  • setting (String, Symbol)

    a background setting of either “light” or “dark”

Returns:

  • (String, Symbol)

    the background setting



69
70
71
72
73
74
# File 'lib/vic/color_scheme.rb', line 69

def background=(setting)
  unless /\A(light|dark)\z/o.match(setting)
    raise ArgumentError.new("expected 'light' or 'dark'")
  end
  @background = setting
end

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

Add or update a highlight

Examples:

scheme.highlight(:Normal, fg: 'eee', bg: '333')

Parameters:

  • group (String, Symbol)

    the group to highlight

  • attributes (Hash) (defaults to: {})

    the attributes to set or update

Returns:



119
120
121
122
123
124
125
126
127
128
# File 'lib/vic/color_scheme.rb', line 119

def highlight(group, attributes={})
  if hilight = find_highlight(group)
    hilight.update_attributes!(attributes)
  else
    hilight = Highlight.new(:"#{ language }#{ group }", attributes)
    @highlights << hilight
  end

  hilight
end

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

Add or update a forced highlight

Examples:

scheme.highlight!(:Normal, fg: 'eee', bg: '333').force? # => true

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to set or update

Returns:



143
144
145
# File 'lib/vic/color_scheme.rb', line 143

def highlight!(group, attributes={})
  highlight(group, attributes.dup.tap { |hash| hash[:force] = true })
end

#highlightsArray

Return the colorscheme’s highlights

Returns:

  • (Array)

    the colorscheme’s highlights



100
101
102
# File 'lib/vic/color_scheme.rb', line 100

def highlights
  @highlights.dup
end

#infoHash

Return the colorscheme information

Returns:

  • (Hash)

    the colorscheme information



22
23
24
# File 'lib/vic/color_scheme.rb', line 22

def info
  @info.marshal_dump
end

#info=(data = {}) ⇒ Hash

Set the colorscheme information

Parameters:

  • data (Hash) (defaults to: {})

    the data to use

Returns:

  • (Hash)

    the colorscheme information



35
36
37
# File 'lib/vic/color_scheme.rb', line 35

def info=(data = {})
  @info.marshal_load(data)
end

#language(name = nil) {|the| ... } ⇒ String, Symbol

Return the language

If a name and a block is passed the language will be temporarily set to name inside the block.

Examples:

scheme.language :ruby do |s|
  s.hi(:InstanceVariable)
  s.hi(:ClassVariable)
end
scheme.highlights.any? { |h| h.group == :rubyClassVarible } # => true

Parameters:

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

    the language to temporarily set if a block is given

Yield Parameters:

  • the (Vic::Colorscheme)

    colorscheme

Returns:

  • (String, Symbol)

    the language setting



251
252
253
254
255
256
257
258
# File 'lib/vic/color_scheme.rb', line 251

def language(name = nil, &block)
  return @language unless name and block_given?

  previous_language = self.language
  @language = name
  block.arity == 0 ? instance_eval(&block) : yield(self)
  @language = previous_language
end

#language=(name) ⇒ String, Symbol

Set a language to prepend new highlight group names with

Examples:

scheme.language = :ruby
scheme.hi(:InstanceVariable).group == :rubyInstanceVariable # => true

Parameters:

  • name (String, Symbol)

    the language name

Returns:

  • (String, Symbol)

    the language name



225
226
227
# File 'lib/vic/color_scheme.rb', line 225

def language=(name)
  @language = name
end

Add highlight links to the colorscheme

Examples:

scheme.link(:rubyInstanceVariable, :rubyClassVariable)

Parameters:

  • from_groups (String, Symbol)

    a list of groups to link from

  • to_groups (String, Symbol)

    the group to link to

Returns:

  • (Array)

    the added links



173
174
175
176
177
178
179
180
# File 'lib/vic/color_scheme.rb', line 173

def link(*from_groups, to_group)
  from_groups.flatten.map do |from_group|
    # Don't add anything we don't already have.
    next if find_link(from_group, to_group)
    link = Link.new(from_group, to_group)
    link.tap { |l| @links << l }
  end.compact
end

#link!(*from_groups, to_group) ⇒ Array

Add forced highlight links to the colorscheme

Examples:

scheme.link!(:rubyInstanceVariable, :rubyClassVariable)
link = scheme.links.find do |l|
  l.from_group == :rubyInstanceVariable and l.to_group = :rubyClassVariable
end
link.force? # => true

Parameters:

  • from_groups (String, Symbol)

    a list of groups to link from

  • to_groups (String, Symbol)

    the group to link to

Returns:

  • (Array)

    the added/updated links



201
202
203
204
205
206
207
208
209
210
# File 'lib/vic/color_scheme.rb', line 201

def link!(*from_groups, to_group)
  # Use the default method first.
  self.link(from_groups, to_group)

  # Then update the links.
  from_groups.flatten.map do |from_group|
    link = find_link(from_group, to_group)
    link.tap(&:force!)
  end
end

Return the colorscheme’s links

Returns:

  • (Array)

    the colorscheme’s links



154
155
156
# File 'lib/vic/color_scheme.rb', line 154

def links
  @links.dup
end

#renderString

Render the colorscheme

Returns:

  • (String)

    the colorscheme as a string



266
267
268
# File 'lib/vic/color_scheme.rb', line 266

def render
  ColorSchemeWriter.write(self)
end