vic 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vic.rb CHANGED
@@ -1,6 +1,11 @@
1
+ require 'set'
2
+
1
3
  module Vic
2
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
3
5
  end
4
6
 
5
7
  require 'vic/colorscheme'
6
- require 'vic/highlight'
8
+ require 'vic/colorscheme/highlight'
9
+ require 'vic/colorscheme/highlight_set'
10
+ require 'vic/colorscheme/highlight/argument'
11
+ require 'vic/colorscheme/highlight/argument_set'
@@ -1,20 +1,31 @@
1
1
  module Vic
2
2
  class Colorscheme
3
- attr_accessor :colors_name
3
+ attr_accessor :colors_name, :information
4
4
 
5
+ # A new instance of Colorscheme. If a block is given with no arguments, the
6
+ # the block will be evaluated in the context of the new colorscheme.
7
+ # Otherwise the block will yield self.
8
+ #
9
+ # @param [String] colors_name the name of the colorscheme
10
+ # @param [Proc] block the block to be evaluated
11
+ # @return [Colorscheme]
5
12
  def initialize(colors_name, &block)
6
13
  @colors_name = colors_name
7
- instance_eval(&block) if block_given?
14
+ if block_given?
15
+ block.arity == 0 ? instance_eval(&block) : yield(self)
16
+ end
8
17
  end
9
18
 
10
- # Adds info to the header of the scheme, or retrieves the info hash.
19
+ # Returns/sets the information attribute
11
20
  #
12
- # @return [Hash,String]
13
- def info(args={})
14
- (@info ||= {}).merge!(args) if args.respond_to? :merge
21
+ # @param [Hash] information the information, :author => 'Joel Holdbrooks'
22
+ # @return [Hash]
23
+ def information(inf={})
24
+ (@information ||= {}).merge!(inf) if inf.respond_to? :merge
15
25
  end
26
+ alias_method :info, :information
16
27
 
17
- # Retrieves the background color.
28
+ # Returns the background color.
18
29
  #
19
30
  # @return [String] 'light' or 'dark'
20
31
  def background
@@ -36,7 +47,7 @@ module Vic
36
47
  # @return[String] the background attribute
37
48
  def background!
38
49
  @background =
39
- if (normal = highlights.select {|h| h.group_name == 'Normal'}.first)
50
+ if normal = highlights.find_by_group('Normal')
40
51
  return 'dark' unless color = normal.guibg
41
52
  color.partition('#').last.to_i(16) <= 8421504 ? 'dark' : 'light'
42
53
  else
@@ -44,32 +55,68 @@ module Vic
44
55
  end
45
56
  end
46
57
 
47
- # Returns the set of highlights belonging the colorscheme
48
- #
49
- # @return[Array] the highlights
50
- def highlights
51
- @highlights ||= []
58
+ # Returns the set of highlights for the colorscheme
59
+ def highlight_set
60
+ @highlight_set ||= HighlightSet.new
52
61
  end
62
+ alias_method :highlights, :highlight_set
53
63
 
54
- # Proxy method for `Vim::Highlight#new`. If inside of a language block the
55
- # langauge name is automatcially prepended to the group name of the new
56
- # highlight.
64
+ # Creates a new highlight or updates one if it exists.
65
+ #
66
+ # If inside of a language block the langauge name is automatcially prepended
67
+ # to the group name of the new highlight.
57
68
  #
58
- # @see Vim::Highlight
59
- # @return [Vim::Highlight] the new highlight
60
- def highlight(group_name, args={}, &block)
61
- group_name = "#{@language}#{group_name}" if @language
62
- highlights.push(Highlight.new group_name, args, &block).first
69
+ # @see Vic::Highlight
70
+ # @return [Vic::Highlight] the new highlight
71
+ def highlight(group, args={})
72
+ hilight = highlight_set.find_by_group(group)
73
+ no_args = args.empty?
74
+
75
+ if not hilight and no_args
76
+ return
77
+ elsif hilight and no_args
78
+ return hilight
79
+ elsif hilight
80
+ hilight.update_arguments!(args)
81
+ else
82
+ hilight = Highlight.new("#{language}#{group}", args)
83
+ highlight_set.add(hilight)
84
+ end
85
+
86
+ hilight
63
87
  end
64
88
  alias_method :hi, :highlight
65
89
 
66
- def language(name, &block)
90
+ # Sets the current language to name. Any new highlights created will have
91
+ # the language name automatically prepended.
92
+ #
93
+ # @return [String] the new language name
94
+ def language=(name)
67
95
  @language = name
68
- yield if block_given?
69
- @language = nil
70
96
  end
71
97
 
72
- # The colorscheme header
98
+ # Returns the current language or temporarily sets the language to name if
99
+ # a block is given. If a block is given with no arguments, it will be
100
+ # evaluated in the context of the colorscheme, otherwise it will yield the
101
+ # colorscheme.
102
+ #
103
+ # @param [String,Symbol] name the name of the language
104
+ # @param [Proc] block the block to be evalauted
105
+ # @return [String] the current language
106
+ def language(name=nil, &block)
107
+ if @language and not name
108
+ return @language
109
+ elsif name and block_given?
110
+ previous_language = self.language
111
+ self.language = name
112
+ block.arity == 0 ? instance_eval(&block) : yield(self)
113
+ self.language = previous_language
114
+ end
115
+ end
116
+
117
+ # Returns the colorscheme header.
118
+ #
119
+ # @return [String] the colorscheme header
73
120
  def header
74
121
  <<-EOT.gsub(/^ {6}/, '')
75
122
  " Vim color file
@@ -87,9 +134,11 @@ module Vic
87
134
  EOT
88
135
  end
89
136
 
90
- # Creates the colorscheme file
137
+ # Returns the colorscheme as a string
138
+ #
139
+ # @return [String] the colorscheme
91
140
  def write
92
- [header, highlights.map {|h| h.write }].join("\n")
141
+ [header, highlights.map(&:write)].join("\n")
93
142
  end
94
143
  end
95
144
  end
@@ -0,0 +1,64 @@
1
+ module Vic
2
+ class Colorscheme::Highlight
3
+ attr_accessor :group
4
+
5
+ # Creates an instance of Vic::Colorscheme::Highlight. Uses
6
+ # `update_arguments!` to set the arguments.
7
+ #
8
+ # @param [String] group the group name, 'Normal', 'Function', etc.
9
+ # @param [Hash] args the arguments to set
10
+ # @return [Vic::Colorscheme::Highlight] the new highlight
11
+ def initialize(group, args={})
12
+ @group = group
13
+ update_arguments!(args)
14
+ end
15
+
16
+ # Returns and/or sets an argument for the current highlight.
17
+ #
18
+ # @param [String,Symbol] the argument key
19
+ # @param [String,Array,nil] the values to assign to the argument key
20
+ # @return [String,Symbol,Array,nil] the value of the argument key
21
+ def method_missing(key, *args)
22
+ super unless key =~ (/([a-z]+)=?/) and Argument.is_valid?($1)
23
+ lookup = $1.to_s
24
+ argument = argument_set.find_by_key(lookup)
25
+ args = args.first if args.first.respond_to? :chr
26
+
27
+ if not argument and args.empty?
28
+ return
29
+ elsif argument and not args.empty?
30
+ argument.arg = args
31
+ elsif argument and args.empty?
32
+ return argument.arg
33
+ elsif not args.empty?
34
+ argument = Argument.new(lookup, args)
35
+ arguments.add argument
36
+ end
37
+ argument.arg
38
+ end
39
+
40
+ # Updates/sets the current highlight's arguments.
41
+ #
42
+ # @param [Hash] args the arguments to update/set, `:guibg => '#333333'`
43
+ # @return [Vic::Colorscheme::Highlight::ArgumentSet] the updated argument set
44
+ def update_arguments!(args={})
45
+ args.each {|key, val| send("#{key}=", val)}
46
+ arguments
47
+ end
48
+
49
+ # Returns the set of arguments for the given highlight
50
+ #
51
+ # @return [Vic::Colorscheme::Highlight::ArgumentSet] the argument set
52
+ def argument_set
53
+ @argument_set ||= ArgumentSet.new
54
+ end
55
+ alias_method :arguments, :argument_set
56
+
57
+ # Writes the highlight contents.
58
+ #
59
+ # @return [String] the highlight as a string
60
+ def write
61
+ "hi #{group} #{arguments.sort_by_key.map(&:write).join(' ')}"
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ module Vic
2
+ class Colorscheme::Highlight::Argument
3
+ VALID = {
4
+ :normal_terminal => %w{term start stop},
5
+ :color_terminal => %w{cterm ctermfg ctermbg},
6
+ :gui => %w{gui guifg guibg guisp font}
7
+ }
8
+
9
+ attr_accessor :key, :arg
10
+
11
+ def initialize(key, arg)
12
+ @key, @arg = key, arg
13
+ end
14
+
15
+ # The argument as a string
16
+ #
17
+ # @return [String,nil] the string if argument has been set
18
+ def write
19
+ return unless arg
20
+ "#{key}=#{arg.respond_to?(:join) ? arg.join(',') : arg}"
21
+ end
22
+
23
+ # Checks if `key` is valid vim key for an argument
24
+ #
25
+ # @param [String,Symbol]
26
+ # @return [true,false] the key is valid
27
+ def self.is_valid?(key)
28
+ VALID.values.flatten.include?(key.to_s)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ module Vic
2
+ class Colorscheme::Highlight::ArgumentSet < Set
3
+ # Adds a new argument to the set
4
+ #
5
+ # @param [Colorscheme::Highlight::Argument] argument the argument to add
6
+ # @return [Colorscheme::Highlight::ArgumentSet] the new set of arguments
7
+ def add(argument)
8
+ if argument.respond_to? :arg
9
+ super(argument)
10
+ else
11
+ # Raise an Exception
12
+ end
13
+ end
14
+
15
+ # Finds an argument by a key.
16
+ #
17
+ # @param [String,Symbol] key the key to search for
18
+ # @return [Colorscheme::Highlight::Argument,nil] the new argument
19
+ def find_by_key(key)
20
+ find {|a| a.key.to_s == key.to_s }
21
+ end
22
+
23
+ # Sorts the arguments by key name
24
+ #
25
+ # @return [Colorscheme::Highlight::ArgumentSet] the sorted set of arguments
26
+ def sort_by_key
27
+ sort {|a, b| a.key <=> b.key}
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module Vic
2
+ class Colorscheme::HighlightSet < Set
3
+ # Adds a highlight to the set.
4
+ #
5
+ # @param [Colorscheme::Highlight] highlight the highlight to add
6
+ # @return [Colorscheme::HighlightSet] the updated set of highlights
7
+ def add(highlight)
8
+ if highlight.respond_to?(:group)
9
+ super(highlight)
10
+ else
11
+ # Raise and Exception
12
+ end
13
+ end
14
+
15
+ def find_by_group(group)
16
+ find {|h| h.group == group }
17
+ end
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-06 00:00:00.000000000Z
12
+ date: 2012-01-09 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Create Vim colorschemes with Ruby
15
15
  email: cjholdbrooks@gmail.com
@@ -19,7 +19,10 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/vic.rb
21
21
  - lib/vic/colorscheme.rb
22
- - lib/vic/highlight.rb
22
+ - lib/vic/colorscheme/highlight.rb
23
+ - lib/vic/colorscheme/highlight_set.rb
24
+ - lib/vic/colorscheme/highlight/argument.rb
25
+ - lib/vic/colorscheme/highlight/argument_set.rb
23
26
  homepage: https://github.com/noprompt/vic
24
27
  licenses: []
25
28
  post_install_message:
@@ -1,34 +0,0 @@
1
- module Vic
2
- class Highlight
3
- VALID_ARGS = {
4
- :normal_terminal => %w{term start stop},
5
- :color_terminal => %w{cterm ctermfg ctermbg},
6
- :gui => %w{gui guifg guibg guisp font}
7
- }
8
-
9
- attr_accessor :group_name, *VALID_ARGS.values.flatten
10
-
11
- def initialize(group_name, args={})
12
- @group_name = group_name
13
-
14
- args.each {|arg, val| send("#{arg}=", val) if respond_to? arg }
15
- end
16
-
17
- def write
18
- "hi #{group_name} #{arguments}" unless arguments.empty?
19
- end
20
-
21
- private
22
-
23
- def arguments
24
- valid_arguments.map do |arg|
25
- val = send(arg)
26
- "#{arg}=#{val}" unless val.nil? or val.empty?
27
- end.compact.join(' ')
28
- end
29
-
30
- def valid_arguments
31
- VALID_ARGS.values.flatten
32
- end
33
- end
34
- end