brice 0.1.1 → 0.2.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/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to brice version 0.1.1
5
+ This documentation refers to brice version 0.2.0
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -0,0 +1,406 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of brice, the extra cool IRb goodness donator #
5
+ # #
6
+ # Copyright (C) 2008-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@uni-koeln.de> #
10
+ # #
11
+ # brice is free software: you can redistribute it and/or modify it under the #
12
+ # terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation, either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # brice is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with brice. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ require 'brice'
28
+
29
+ module Brice
30
+
31
+ # Add colour support to IRb.
32
+
33
+ module Colours
34
+
35
+ extend self
36
+
37
+ # Default IRb colour scheme.
38
+ DEFAULT_COLOURS = {
39
+ # delimiter colours
40
+ :comma => :blue,
41
+ :refers => :blue,
42
+
43
+ # container colours (hash and array)
44
+ :open_hash => :green,
45
+ :close_hash => :green,
46
+ :open_array => :green,
47
+ :close_array => :green,
48
+
49
+ # object colours
50
+ :open_object => :light_red,
51
+ :object_class => :white,
52
+ :object_addr_prefix => :blue,
53
+ :object_line_prefix => :blue,
54
+ :close_object => :light_red,
55
+
56
+ # symbol colours
57
+ :symbol => :yellow,
58
+ :symbol_prefix => :yellow,
59
+
60
+ # string colours
61
+ :open_string => :red,
62
+ :string => :cyan,
63
+ :close_string => :red,
64
+
65
+ # misc colours
66
+ :number => :cyan,
67
+ :keyword => :green,
68
+ :class => :light_green,
69
+ :range => :red,
70
+ :unknown => :green
71
+ }
72
+
73
+ # Fruity testing colours.
74
+ TESTING_COLOURS = {
75
+ :comma => :red,
76
+ :refers => :red,
77
+ :open_hash => :blue,
78
+ :close_hash => :blue,
79
+ :open_array => :green,
80
+ :close_array => :green,
81
+ :open_object => :light_red,
82
+ :object_class => :light_green,
83
+ :object_addr => :purple,
84
+ :object_line => :light_purple,
85
+ :close_object => :light_red,
86
+ :symbol => :yellow,
87
+ :symbol_prefix => :yellow,
88
+ :number => :cyan,
89
+ :string => :cyan,
90
+ :keyword => :white,
91
+ :range => :light_blue
92
+ }
93
+
94
+ def init(opt = {})
95
+ enable_irb if Brice.opt(opt, :irb, STDOUT.tty?)
96
+ enable_pp if Brice.opt(opt, :pp, STDOUT.tty?)
97
+
98
+ self.colours = Brice.opt(opt, :colours, colours)
99
+ end
100
+
101
+ def disable
102
+ disable_irb
103
+ disable_pp
104
+ end
105
+
106
+ # Enable colourized IRb results.
107
+ def enable_irb
108
+ if IRB.const_defined?(:Inspector)
109
+ IRB::Inspector.class_eval {
110
+ unless method_defined?(:inspect_value_with_colour)
111
+ alias_method :inspect_value_without_colour, :inspect_value
112
+
113
+ def inspect_value_with_colour(value)
114
+ Colours.colourize(inspect_value_without_colour(value))
115
+ end
116
+ end
117
+
118
+ alias_method :inspect_value, :inspect_value_with_colour
119
+ }
120
+ else
121
+ IRB::Irb.class_eval {
122
+ unless method_defined?(:output_value_with_colour)
123
+ alias_method :output_value_without_colour, :output_value
124
+
125
+ def output_value_with_colour
126
+ value = @context.last_value
127
+ value = Colours.colourize(value.inspect) if @context.inspect?
128
+
129
+ printf(@context.return_format, value)
130
+ end
131
+ end
132
+
133
+ alias_method :output_value, :output_value_with_colour
134
+ }
135
+ end
136
+ end
137
+
138
+ # Disable colourized IRb results.
139
+ def disable_irb
140
+ if IRB.const_defined?(:Inspector)
141
+ IRB::Inspector.class_eval {
142
+ if method_defined?(:inspect_value_without_colour)
143
+ alias_method :inspect_value, :inspect_value_without_colour
144
+ end
145
+ }
146
+ else
147
+ IRB::Irb.class_eval {
148
+ if method_defined?(:output_value_without_colour)
149
+ alias_method :output_value, :output_value_without_colour
150
+ end
151
+ }
152
+ end
153
+ end
154
+
155
+ def enable_pp
156
+ require 'pp'
157
+
158
+ class << PP
159
+ unless method_defined?(:pp_with_colour)
160
+ alias_method :pp_without_colour, :pp
161
+
162
+ def pp_with_colour(obj, out = $>, width = 79)
163
+ res = pp_without_colour(obj, str = '', width)
164
+ out << Colours.colourize(str)
165
+ res
166
+ end
167
+
168
+ alias_method :pp, :pp_with_colour
169
+ end
170
+
171
+ unless method_defined?(:singleline_pp_with_colour)
172
+ alias_method :singleline_pp_without_colour, :singleline_pp
173
+
174
+ def singleline_pp_with_colour(obj, out = $>)
175
+ res = singleline_pp_without_colour(obj, str = '')
176
+ out << Colours.colourize(str)
177
+ res
178
+ end
179
+
180
+ alias_method :singleline_pp, :singleline_pp_with_colour
181
+ end
182
+ end
183
+ end
184
+
185
+ def disable_pp
186
+ PP.class_eval {
187
+ if method_defined?(:pp_without_colour)
188
+ alias_method :pp, :pp_without_colour
189
+ end
190
+
191
+ if method_defined?(:singleline_pp_without_colour)
192
+ alias_method :singleline_pp, :singleline_pp_without_colour
193
+ end
194
+ }
195
+ end
196
+
197
+ # Set colour map to hash
198
+ def colours=(hash)
199
+ @colours = hash
200
+ end
201
+
202
+ # Get current colour map
203
+ def colours
204
+ @colours ||= DEFAULT_COLOURS.dup
205
+ end
206
+
207
+ # Return a string with the given colour.
208
+ def colourize_string(str, colour)
209
+ (col = Colour[colour]) ? "#{col}#{str}#{Colour[:reset]}" : str
210
+ end
211
+
212
+ # Colourize the results of inspect
213
+ def colourize(str)
214
+ res = ''
215
+
216
+ Tokenizer.tokenize(str) { |token, value|
217
+ res << colourize_string(value, colours[token])
218
+ }
219
+
220
+ res
221
+ rescue
222
+ str
223
+ end
224
+
225
+ # Terminal escape codes for colours.
226
+
227
+ module Colour
228
+
229
+ extend self
230
+
231
+ COLOURS = {
232
+ :reset => '0;0',
233
+ :black => '0;30',
234
+ :red => '0;31',
235
+ :green => '0;32',
236
+ :brown => '0;33',
237
+ :blue => '0;34',
238
+ :cyan => '0;36',
239
+ :purple => '0;35',
240
+ :light_gray => '0;37',
241
+ :dark_gray => '1;30',
242
+ :light_red => '1;31',
243
+ :light_green => '1;32',
244
+ :yellow => '1;33',
245
+ :light_blue => '1;34',
246
+ :light_cyan => '1;36',
247
+ :light_purple => '1;35',
248
+ :white => '1;37'
249
+ }
250
+
251
+ # Return the escape code for a given colour.
252
+ def escape(key)
253
+ "\033[#{COLOURS[key]}m" if COLOURS.has_key?(key)
254
+ end
255
+
256
+ alias_method :[], :escape
257
+
258
+ end
259
+
260
+ # Tokenize an inspection string.
261
+
262
+ module Tokenizer
263
+
264
+ extend self
265
+
266
+ def tokenize(str)
267
+ raise ArgumentError, 'no block given' unless block_given?
268
+
269
+ chars = str.split(//)
270
+ char = last_char = repeat = nil
271
+ states, value, index = [], '', 0
272
+
273
+ reset = lambda { |*args|
274
+ states.pop
275
+
276
+ value = ''
277
+ repeat = args.first unless args.empty?
278
+ }
279
+
280
+ yield_last = lambda { |*args|
281
+ yield states.last, value
282
+ reset[*args]
283
+ }
284
+
285
+ until index > chars.size
286
+ char, repeat = chars[index], false
287
+
288
+ case states.last
289
+ when nil
290
+ case char
291
+ when ':' then states << :symbol
292
+ when '"' then states << :string
293
+ when '#' then states << :object
294
+ when /[a-z]/i
295
+ states << :keyword
296
+ repeat = true
297
+ when /[0-9-]/
298
+ states << :number
299
+ repeat = true
300
+ when '{' then yield :open_hash, '{'
301
+ when '[' then yield :open_array, '['
302
+ when ']' then yield :close_array, ']'
303
+ when '}' then yield :close_hash, '}'
304
+ when /\s/ then yield :whitespace, char
305
+ when ',' then yield :comma, ','
306
+ when '>' then yield :refers, '=>' if last_char == '='
307
+ when '.' then yield :range, '..' if last_char == '.'
308
+ when '=' then nil
309
+ else yield :unknown, char
310
+ end
311
+ when :symbol
312
+ if char =~ /[a-z0-9_!?]/ # should have =, but that messes up foo=>bar
313
+ value << char
314
+ else
315
+ yield :symbol_prefix, ':'
316
+ yield_last[true]
317
+ end
318
+ when :string
319
+ if char == '"'
320
+ if last_char == '\\'
321
+ value[-1] = char
322
+ else
323
+ yield :open_string, char
324
+ yield_last[]
325
+ yield :close_string, char
326
+ end
327
+ else
328
+ value << char
329
+ end
330
+ when :keyword
331
+ if char =~ /[a-z0-9_]/i
332
+ value << char
333
+ else
334
+ states[-1] = :class if value =~ /\A[A-Z]/
335
+ yield_last[true]
336
+
337
+ value << char if char == '.'
338
+ end
339
+ when :number
340
+ case char
341
+ when /[0-9e-]/
342
+ value << char
343
+ when '.'
344
+ if last_char == char
345
+ value.chop!
346
+
347
+ yield_last[]
348
+ yield :range, '..'
349
+ else
350
+ value << char
351
+ end
352
+ else
353
+ yield_last[true]
354
+ end
355
+ when :object
356
+ case char
357
+ when '<'
358
+ yield :open_object, '#<'
359
+ states << :object_class
360
+ when ':'
361
+ states << :object_addr
362
+ when '@'
363
+ states << :object_line
364
+ when '>'
365
+ yield :close_object, '>'
366
+ reset[]
367
+ end
368
+ when :object_class
369
+ if char == ':'
370
+ yield_last[true]
371
+ else
372
+ value << char
373
+ end
374
+ when :object_addr
375
+ case char
376
+ when '>'
377
+ # ignore
378
+ when '@'
379
+ yield :object_addr_prefix, ':'
380
+ yield_last[true]
381
+ else
382
+ value << char
383
+ end
384
+ when :object_line
385
+ if char == '>'
386
+ yield :object_line_prefix, '@'
387
+ yield_last[true]
388
+ else
389
+ value << char
390
+ end
391
+ else
392
+ raise "unknown state: #{states}"
393
+ end
394
+
395
+ unless repeat
396
+ index += 1
397
+ last_char = char
398
+ end
399
+ end
400
+ end
401
+
402
+ end
403
+
404
+ end
405
+
406
+ end
data/lib/brice/config.rb CHANGED
@@ -25,8 +25,9 @@
25
25
  #++
26
26
 
27
27
  require 'ostruct'
28
+ require 'brice'
28
29
 
29
- class Brice
30
+ module Brice
30
31
 
31
32
  # Exclude unwanted packages:
32
33
  #
@@ -45,9 +46,9 @@ class Brice
45
46
  #
46
47
  # # set multiple properties
47
48
  # config.foo = %w[bar baz]
48
- # # equivalent to:
49
- # #config.foo.bar = true
50
- # #config.foo.baz = true
49
+ # # which is equivalent to:
50
+ # config.foo.bar = true
51
+ # config.foo.baz = true
51
52
  #
52
53
  # # reset package configuration
53
54
  # config.foo!
@@ -69,7 +70,7 @@ class Brice
69
70
  #
70
71
  # Accessor for package +package+.
71
72
  def [](package)
72
- @packages[package.to_s]
73
+ packages[package.to_s]
73
74
  end
74
75
 
75
76
  # call-seq:
@@ -90,7 +91,7 @@ class Brice
90
91
  #
91
92
  # Disable/exclude packages +packages+.
92
93
  def exclude(*packages)
93
- packages.each { |package| @packages.delete(package.to_s) }
94
+ packages.each { |package| self.packages.delete(package.to_s) }
94
95
  self
95
96
  end
96
97
 
@@ -101,7 +102,7 @@ class Brice
101
102
  #
102
103
  # Clear all packages.
103
104
  def clear
104
- @packages.clear
105
+ packages.clear
105
106
  end
106
107
 
107
108
  # call-seq:
@@ -110,7 +111,7 @@ class Brice
110
111
  #
111
112
  # See whether package +package+ is enabled/included.
112
113
  def include?(package)
113
- @packages.include?(package.to_s)
114
+ packages.include?(package.to_s)
114
115
  end
115
116
 
116
117
  alias_method :have?, :include?
@@ -126,17 +127,21 @@ class Brice
126
127
  def method_missing(method, *args)
127
128
  package, punctuation = method.to_s.sub(/([=!?])?\z/, ''), $1
128
129
 
129
- raise ArgumentError, "wrong number of arguments (#{args.size} for 0)" \
130
- unless punctuation == '=' || args.empty?
130
+ assignment = punctuation == '='
131
+
132
+ unless assignment || args.empty?
133
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
134
+ end
131
135
 
132
136
  case punctuation
133
- when '='
134
- @packages[package] = PackageConfig.new
135
- [*args.first].each { |arg| @packages[package].send("#{arg}=", true) }
136
- when '!'
137
- @packages[package] = PackageConfig.new
137
+ when '=', '!'
138
+ config = packages[package] = PackageConfig.new
139
+
140
+ assignment ? Array(args.first).each { |arg|
141
+ config.send("#{arg}=", true)
142
+ } : config
138
143
  when '?'
139
- self.include?(package)
144
+ include?(package)
140
145
  else
141
146
  self[package]
142
147
  end
@@ -144,12 +149,23 @@ class Brice
144
149
 
145
150
  class PackageConfig < OpenStruct
146
151
 
152
+ include Enumerable
153
+
154
+ # call-seq:
155
+ # pkgconfig.each { |entry| ... } -> pkgconfig
156
+ #
157
+ # Iterates over all entries in +pkgconfig+. Returns +pkgconfig+.
158
+ def each
159
+ @table.keys.each { |key| yield key.to_s }
160
+ self
161
+ end
162
+
147
163
  # call-seq:
148
- # pkgconfig.entries => anArray
164
+ # pkgconfig.empty? -> true | false
149
165
  #
150
- # Returns all entries/keys.
151
- def entries
152
- instance_variable_get(:@table).keys.map { |key| key.to_s }.sort
166
+ # Checks whether +pkgconfig+ is empty?
167
+ def empty?
168
+ @table.empty?
153
169
  end
154
170
 
155
171
  end
data/lib/brice/dsl.rb CHANGED
@@ -27,7 +27,9 @@
27
27
  require 'nuggets/object/silence_mixin'
28
28
  require 'nuggets/env/set'
29
29
 
30
- class Brice
30
+ require 'brice'
31
+
32
+ module Brice
31
33
 
32
34
  # Certain global helper methods for use inside IRb extensions. Also
33
35
  # available inside the IRb session.
@@ -40,8 +42,8 @@ class Brice
40
42
  # irb_rc { ... }
41
43
  #
42
44
  # Add IRB_RC proc (to be executed whenever a (sub-)session is started).
43
- def irb_rc
44
- Brice.irb_rc << Proc.new
45
+ def irb_rc(&block)
46
+ Brice.irb_rc << block
45
47
  end
46
48
 
47
49
  # call-seq:
@@ -49,14 +51,10 @@ class Brice
49
51
  # irb_def(symbol, method)
50
52
  #
51
53
  # Define a method for use inside the IRb session.
52
- def irb_def(symbol, method = nil)
54
+ def irb_def(symbol, method = nil, &block)
53
55
  irb_rc {
54
56
  Object.instance_eval {
55
- if method
56
- define_method(symbol, method)
57
- else
58
- define_method(symbol, &Proc.new)
59
- end
57
+ define_method(symbol, method || block)
60
58
  }
61
59
  }
62
60
  end
@@ -93,14 +91,8 @@ class Brice
93
91
  # block in case of success.
94
92
  #
95
93
  # Returns either the result of the executed method or of the block.
96
- def brice_require(string)
97
- args = [:require, [string], LoadError]
98
-
99
- if block_given?
100
- brice_rescue(*args) { |*a| yield(*a) }
101
- else
102
- brice_rescue(*args)
103
- end
94
+ def brice_require(string, &block)
95
+ brice_rescue(:require, [string], LoadError, &block)
104
96
  end
105
97
 
106
98
  # call-seq:
@@ -110,14 +102,8 @@ class Brice
110
102
  # block in case of success.
111
103
  #
112
104
  # Returns either the result of the executed method or of the block.
113
- def brice_load(filename, wrap = false)
114
- args = [:load, [filename, wrap]]
115
-
116
- if block_given?
117
- brice_rescue(*args) { |*a| yield(*a) }
118
- else
119
- brice_rescue(*args)
120
- end
105
+ def brice_load(filename, wrap = false, &block)
106
+ brice_rescue(:load, [filename, wrap], &block)
121
107
  end
122
108
 
123
109
  # call-seq:
@@ -125,7 +111,7 @@ class Brice
125
111
  #
126
112
  # Runs +cmd+ with ENV modified according to +env+.
127
113
  def brice_run_cmd(cmd, env = {})
128
- ENV.with(env) { `#{cmd}` }
114
+ ENV.with(env) { %x{#{cmd}} }
129
115
  end
130
116
 
131
117
  # call-seq:
@@ -141,12 +127,14 @@ class Brice
141
127
  def brice(package)
142
128
  package, libs = case package
143
129
  when Hash
144
- raise ArgumentError, "Too many package names: #{package.keys.join(' ')}" \
145
- if package.size > 1
146
- raise ArgumentError, 'No package name given' \
147
- if package.size < 1
130
+ names = package.keys
131
+
132
+ err = names.size > 1 ? "Too many package names: #{names.join(' ')}" :
133
+ names.size < 1 ? 'No package name given' : nil
134
+
135
+ raise ArgumentError, err if err
148
136
 
149
- [package.keys.first, [*package.values.first]]
137
+ [names.first, Array(package.values.first)]
150
138
  else
151
139
  [package, [package]]
152
140
  end