acclaim 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.5.0
@@ -7,6 +7,7 @@ end
7
7
 
8
8
  %w(
9
9
 
10
+ acclaim/ansi
10
11
  acclaim/command
11
12
  acclaim/gem
12
13
  acclaim/option
@@ -0,0 +1,193 @@
1
+ require 'ribbon'
2
+
3
+ module Acclaim
4
+
5
+ # ANSI escape codes for colors and effects.
6
+ #
7
+ # @see http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape code
8
+ # @see Acclaim::CoreExtensions::String::ANSI
9
+ # @author Matheus Afonso Martins Moreira
10
+ # @since 0.5.0
11
+ module ANSI
12
+ end
13
+
14
+ class << ANSI
15
+
16
+ # Extends the String class with an object-oriented ANSI escape code API.
17
+ #
18
+ # @see Acclaim::CoreExtensions::String::ANSI
19
+ def extend_string!
20
+ require 'acclaim/core_extensions/string/ansi'
21
+ end
22
+
23
+ # Colors from the standard color pallete.
24
+ #
25
+ # @return [Array<Symbol>] available colors
26
+ # @see foreground_color
27
+ # @see background_color
28
+ # @see supported_effects
29
+ def supported_colors
30
+ colors.keys
31
+ end
32
+
33
+ alias available_colors supported_colors
34
+
35
+ # Text effects that can be applied.
36
+ #
37
+ # @return [Array<Symbol>] available text effects
38
+ # @see effects
39
+ # @see supported_colors
40
+ def supported_effects
41
+ text_effects.keys
42
+ end
43
+
44
+ alias available_effects supported_effects
45
+
46
+ # Applies foreground color to the given string.
47
+ #
48
+ # @param [String, #to_s] string the string the color will be applied to
49
+ # @param [Symbol] color the name of the color that will be applied
50
+ # @return [String] new string with the color applied
51
+ # @see background_color
52
+ # @see supported_colors
53
+ def foreground_color(string, color)
54
+ apply_color_to string, color, foreground_offset
55
+ end
56
+
57
+ # Applies background color to the given string.
58
+ #
59
+ # @param [String, #to_s] string the string the color will be applied to
60
+ # @param [Symbol] color the name of the color that will be applied
61
+ # @return [String] new string with the color applied
62
+ # @see foreground_color
63
+ # @see supported_colors
64
+ def background_color(string, color)
65
+ apply_color_to string, color, background_offset
66
+ end
67
+
68
+ # Applies text effects to the given string.
69
+ #
70
+ # @param [String, #to_s] string the string the effects will be applied to
71
+ # @param [Array<Symbol>] effects the text effects to apply
72
+ # @return [String] new string with the text effects applied
73
+ # @see supported_effects
74
+ def effects(string, *effects)
75
+ apply_escape_codes_to string do
76
+ effects.select do |effect|
77
+ text_effects.has_key? effect
78
+ end.map do |effect|
79
+ text_effects[effect]
80
+ end
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ # Computes the color escape code and applies it to the string.
87
+ #
88
+ # @param [String, #to_s] string the string the color will be applied to
89
+ # @param [Symbol] color the name of the color that will be applied
90
+ # @param [Integer] offset background or foreground color code offset
91
+ # @return [String] new string with the color applied
92
+ def apply_color_to(string, color, offset)
93
+ apply_escape_code_to string do
94
+ offset + colors[color] if colors.has_key? color
95
+ end
96
+ end
97
+
98
+ # Applies the escape codes returned by the block to the given string.
99
+ #
100
+ # @note A reset escape code will be appended to the string if it does not
101
+ # end with one.
102
+ #
103
+ # @param [String, #to_s] string the string the code will be applied to
104
+ # @yieldreturn [Integer, Array<Integer>] escape code(s) to apply
105
+ # @return [String] new string with the escape codes applied
106
+ def apply_escape_codes_to(string)
107
+ string.to_s.dup.tap do |string|
108
+ [*yield].map(&:to_i).each do |escape_code|
109
+ string.prepend select_graphic_rendition escape_code
110
+ end
111
+
112
+ code = reset
113
+ string.concat code unless self =~ /#{Regexp.escape code}\z/ix
114
+ end
115
+ end
116
+
117
+ alias apply_escape_code_to apply_escape_codes_to
118
+
119
+ # Escape code that resets all SGR settings.
120
+ #
121
+ # @return [String] reset escape code
122
+ def reset
123
+ select_graphic_rendition
124
+ end
125
+
126
+ # Creates a Select Graphic Rendition (SGR) escape code.
127
+ #
128
+ # @param [Integer] parameter the SGR parameter
129
+ # @return [String] escape code string
130
+ def select_graphic_rendition(parameter = 0)
131
+ "\e[#{parameter}m"
132
+ end
133
+
134
+ alias sgr select_graphic_rendition
135
+
136
+ # Foreground color SGR parameter offset.
137
+ #
138
+ # @return [Integer] the offset where background colors start
139
+ # @see background_offset
140
+ # @see colors
141
+ def foreground_offset
142
+ 30
143
+ end
144
+
145
+ # Background color SGR parameter offset.
146
+ #
147
+ # @return [Integer] the offset where foreground colors start
148
+ # @see foreground_offset
149
+ # @see colors
150
+ def background_offset
151
+ 40
152
+ end
153
+
154
+ # Color names mapped to their escape codes.
155
+ #
156
+ # @note These values should be used with an appropriate offset.
157
+ #
158
+ # @return [Hash] colors associated with their escape codes
159
+ # @see foreground_offset
160
+ # @see background_offset
161
+ def colors
162
+ @colors ||= Ribbon.wrap do
163
+ black 0
164
+ red 1
165
+ green 2
166
+ yellow 3
167
+ blue 4
168
+ magenta 5
169
+ cyan 6
170
+ white 7
171
+ ribbon.default 9
172
+ end.to_hash
173
+ end
174
+
175
+ # Text effects mapped to their escape codes.
176
+ #
177
+ # @return [Hash] effects associated with their escape codes
178
+ def text_effects
179
+ @text_effects ||= Ribbon.wrap do
180
+ bright bold 1
181
+ faint 2
182
+ italic 3
183
+ underline 4
184
+ blink 5
185
+ negative inverse reverse 7
186
+ conceal hide 8
187
+ strikethrough strikeout crossed_out 9
188
+ end.to_hash
189
+ end
190
+
191
+ end
192
+
193
+ end
@@ -0,0 +1,16 @@
1
+ module Acclaim
2
+
3
+ # Acclaim extensions to the Ruby standard library.
4
+ #
5
+ # @author Matheus Afonso Martins Moreira
6
+ # @since 0.5.0
7
+ module CoreExtensions
8
+ end
9
+
10
+ end
11
+
12
+ %w(
13
+
14
+ acclaim/core_extensions/string
15
+
16
+ ).each { |file| require file }
@@ -0,0 +1,18 @@
1
+ module Acclaim
2
+ module CoreExtensions
3
+
4
+ # Acclaim extensions to the String class.
5
+ #
6
+ # @author Matheus Afonso Martins Moreira
7
+ # @since 0.5.0
8
+ module String
9
+ end
10
+
11
+ end
12
+ end
13
+
14
+ %w(
15
+
16
+ acclaim/core_extensions/string/ansi
17
+
18
+ ).each { |file| require file }
@@ -0,0 +1,49 @@
1
+ require 'acclaim/ansi'
2
+
3
+ module Acclaim
4
+ module CoreExtensions
5
+ module String
6
+
7
+ # Object-oriented ANSI escape code API.
8
+ #
9
+ # @see Acclaim::ANSI Acclaim::ANSI
10
+ # @author Matheus Afonso Martins Moreira
11
+ # @since 0.5.0
12
+ module ANSI
13
+
14
+ # Applies foreground color to this string.
15
+ #
16
+ # @param [Symbol] color the name of the color that will be applied
17
+ # @return [String] new string with the color applied
18
+ # @see #background
19
+ # @see Acclaim::ANSI.supported_colors
20
+ def foreground(color)
21
+ Acclaim::ANSI.foreground_color self, color
22
+ end
23
+
24
+ # Applies background color to this string.
25
+ #
26
+ # @param [Symbol] color the name of the color that will be applied
27
+ # @return [String] new string with the color applied
28
+ # @see #foreground
29
+ # @see Acclaim::ANSI.supported_colors
30
+ def background(color)
31
+ Acclaim::ANSI.background_color self, color
32
+ end
33
+
34
+ # Applies text effects to this string.
35
+ #
36
+ # @param [Array<Symbol>] effects the text effects to apply
37
+ # @return [String] new string with the text effects applied
38
+ # @see Acclaim::ANSI.supported_effects
39
+ def effects(*effects)
40
+ Acclaim::ANSI.effects self, *effects
41
+ end
42
+
43
+ end
44
+
45
+ ::String.send :include, ANSI
46
+
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acclaim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
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-06-22 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jewel
@@ -122,6 +122,7 @@ files:
122
122
  - acclaim.gemspec
123
123
  - acclaim.version
124
124
  - lib/acclaim.rb
125
+ - lib/acclaim/ansi.rb
125
126
  - lib/acclaim/command.rb
126
127
  - lib/acclaim/command/dsl.rb
127
128
  - lib/acclaim/command/dsl/root.rb
@@ -130,6 +131,9 @@ files:
130
131
  - lib/acclaim/command/help/template/command.erb
131
132
  - lib/acclaim/command/parser.rb
132
133
  - lib/acclaim/command/version.rb
134
+ - lib/acclaim/core_extensions.rb
135
+ - lib/acclaim/core_extensions/string.rb
136
+ - lib/acclaim/core_extensions/string/ansi.rb
133
137
  - lib/acclaim/gem.rb
134
138
  - lib/acclaim/option.rb
135
139
  - lib/acclaim/option/arity.rb
@@ -168,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
172
  version: '0'
169
173
  segments:
170
174
  - 0
171
- hash: 603733755456817877
175
+ hash: 1568560335460056762
172
176
  required_rubygems_version: !ruby/object:Gem::Requirement
173
177
  none: false
174
178
  requirements:
@@ -177,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
181
  version: '0'
178
182
  segments:
179
183
  - 0
180
- hash: 603733755456817877
184
+ hash: 1568560335460056762
181
185
  requirements: []
182
186
  rubyforge_project:
183
187
  rubygems_version: 1.8.24