TwP-loquacious 1.1.1 → 1.3.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.
data/History.txt CHANGED
@@ -1,3 +1,13 @@
1
+ == 1.3.0 / 2009-04-05
2
+
3
+ * 1 minor enhancement
4
+ - Adding colorization options to the Help object
5
+
6
+ == 1.2.0 / 2009-04-05
7
+
8
+ * 1 minor enhancement
9
+ - Help#show now matches using a regular expression
10
+
1
11
  == 1.1.1 / 2009-04-05
2
12
 
3
13
  * 1 bug fix
data/lib/loquacious.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  module Loquacious
3
3
 
4
4
  # :stopdoc:
5
- VERSION = '1.1.1'
5
+ VERSION = '1.3.0'
6
6
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
7
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
8
  # :startdoc:
@@ -18,7 +18,14 @@ class Loquacious::Configuration
18
18
  :name_leader => ' - '.freeze,
19
19
  :name_length => 0,
20
20
  :name_value_sep => ' => '.freeze,
21
- :desc_leader => ' '.freeze
21
+ :desc_leader => ' '.freeze,
22
+ :colorize => false,
23
+ :colors => {
24
+ :name => :white,
25
+ :value => :cyan,
26
+ :description => :green,
27
+ :leader => :yellow
28
+ }.freeze
22
29
  }.freeze
23
30
 
24
31
  class Error < StandardError; end
@@ -34,6 +41,12 @@ class Loquacious::Configuration
34
41
  # :name_value_sep String separating the attribute name from the value
35
42
  # :desc_leader String appearing before the description
36
43
  # :io The IO object where help will be written
44
+ # :colorize Flag to colorize the output or not
45
+ # :colors Hash of colors for the name, value, description
46
+ # :name Name color
47
+ # :value Value color
48
+ # :description Description color
49
+ # :leader Leader and spacer color
37
50
  #
38
51
  # The description is printed before each attribute name and value on its
39
52
  # own line.
@@ -46,6 +59,8 @@ class Loquacious::Configuration
46
59
  @io = opts[:io]
47
60
  @name_length = Integer(opts[:name_length])
48
61
  @desc_leader = opts[:desc_leader]
62
+ @colorize = opts[:colorize]
63
+ @colors = opts[:colors]
49
64
 
50
65
  unless @name_length > 0
51
66
  Iterator.new(@config).each do |node|
@@ -64,20 +79,43 @@ class Loquacious::Configuration
64
79
  @format = "#{name_leader}%-#{@name_length}s#{name_value_sep}%s"
65
80
  @name_format = "#{name_leader}%s"
66
81
 
82
+ if colorize?
83
+ @desc_leader = self.__send__(@colors[:leader], @desc_leader)
84
+ name_leader = self.__send__(@colors[:leader], name_leader)
85
+ name_value_sep = self.__send__(@colors[:leader], name_value_sep)
86
+
87
+ @format = name_leader.dup
88
+ @format << self.__send__(@colors[:name], "%-#{@name_length}s")
89
+ @format << name_value_sep.dup
90
+ @format << self.__send__(@colors[:value], "%s")
91
+
92
+ @name_format = name_leader.dup
93
+ @name_format << self.__send__(@colors[:name], "%s")
94
+ end
95
+
67
96
  @desc_leader.freeze
68
97
  @value_leader.freeze
69
98
  @format.freeze
70
99
  @name_format.freeze
71
100
  end
72
101
 
102
+ # Returns +true+ if the help instance is configured to colorize the
103
+ # output messages. Returns +false+ otherwise.
104
+ #
105
+ def colorize?
106
+ @colorize
107
+ end
108
+
73
109
  # call-seq:
74
110
  # show_attribute( name = nil, opts = {} )
75
111
  #
76
- # TODO: finish comments and docos
112
+ # Use this method to show the description for a single attribute or for
113
+ # all the attributes if no _name_ is given. The options allow you to
114
+ # show the values along with the attributes and to hide the descriptions
115
+ # (if all you want to see are the values).
77
116
  #
78
- # show available attributes (with/without descriptions)
79
- # show current config
80
- # show everything
117
+ # :descriptions => true to show descriptions and false to hide them
118
+ # :values => true to show values and false to hide them
81
119
  #
82
120
  def show_attribute( name = nil, opts = {} )
83
121
  name, opts = nil, name if name.is_a?(Hash)
@@ -86,11 +124,12 @@ class Loquacious::Configuration
86
124
  :values => false
87
125
  }.merge!(opts)
88
126
 
89
- name = normalize_attr(name)
127
+ rgxp = Regexp.new(normalize_attr(name))
90
128
  show_description = opts[:descriptions]
91
129
  show_value = opts[:values]
92
130
 
93
- Iterator.new(@config).each(name) do |node|
131
+ Iterator.new(@config).each do |node|
132
+ next unless rgxp =~ node.name
94
133
  print_node(node, show_description, show_value)
95
134
  end
96
135
  end
@@ -108,7 +147,7 @@ class Loquacious::Configuration
108
147
  #
109
148
  def normalize_attr( name )
110
149
  case name
111
- when String, nil; name
150
+ when String, nil; name.to_s
112
151
  when Symbol; name.to_s
113
152
  when Array; name.join('.')
114
153
  else
@@ -125,7 +164,15 @@ class Loquacious::Configuration
125
164
  def print_node( node, show_description, show_value )
126
165
  desc = node.desc.to_s.dup
127
166
  show_description = false if desc.empty?
128
- @io.puts(desc.indent(@desc_leader)) if show_description
167
+
168
+ if show_description
169
+ if colorize?
170
+ desc = desc.gsub(%r/([^\n]+)/,
171
+ self.__send__(@colors[:description], '\1'))
172
+ end
173
+ @io.puts(desc.indent(@desc_leader))
174
+ end
175
+
129
176
  @io.puts(format_name(node, show_value))
130
177
  @io.puts if show_description
131
178
  end
@@ -145,6 +192,42 @@ class Loquacious::Configuration
145
192
  @format % [name, obj]
146
193
  end
147
194
 
195
+ [ [ :clear , 0 ],
196
+ [ :reset , 0 ], # synonym for :clear
197
+ [ :bold , 1 ],
198
+ [ :dark , 2 ],
199
+ [ :italic , 3 ], # not widely implemented
200
+ [ :underline , 4 ],
201
+ [ :underscore , 4 ], # synonym for :underline
202
+ [ :blink , 5 ],
203
+ [ :rapid_blink , 6 ], # not widely implemented
204
+ [ :negative , 7 ], # no reverse because of String#reverse
205
+ [ :concealed , 8 ],
206
+ [ :strikethrough, 9 ], # not widely implemented
207
+ [ :black , 30 ],
208
+ [ :red , 31 ],
209
+ [ :green , 32 ],
210
+ [ :yellow , 33 ],
211
+ [ :blue , 34 ],
212
+ [ :magenta , 35 ],
213
+ [ :cyan , 36 ],
214
+ [ :white , 37 ],
215
+ [ :on_black , 40 ],
216
+ [ :on_red , 41 ],
217
+ [ :on_green , 42 ],
218
+ [ :on_yellow , 43 ],
219
+ [ :on_blue , 44 ],
220
+ [ :on_magenta , 45 ],
221
+ [ :on_cyan , 46 ],
222
+ [ :on_white , 47 ] ].each do |name,code|
223
+
224
+ class_eval <<-CODE
225
+ def #{name.to_s}( str )
226
+ "\e[#{code}m\#{str}\e[0m"
227
+ end
228
+ CODE
229
+ end
230
+
148
231
  end # class Help
149
232
  end # module Loquacious
150
233
 
data/loquacious.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{loquacious}
5
- s.version = "1.1.1"
5
+ s.version = "1.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Tim Pease"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: TwP-loquacious
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease