ansi 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -9,7 +9,7 @@ is mildly unconvential, but it allows the arguments to be used
9
9
  as options with common defaults more elegantly.
10
10
 
11
11
  Another important change is that ANSI::Code no longer provides
12
- String extension methods hwen included. For this use the new
12
+ String extension methods when included. For this use the new
13
13
  ANSI::Mixin.
14
14
 
15
15
  Other improvements include a String extension, #ansi, added to
data/PROFILE ADDED
@@ -0,0 +1,23 @@
1
+ ---
2
+ title : ANSI
3
+ suite : rubyworks
4
+ summary : ANSI codes at your fingertips!
5
+ copyright : Copyright (c) 2009 Thomas Sawyer
6
+ license : MIT
7
+ contact : rubyworks-mailinglist@googlegroups.com
8
+ created : 2004-08-01
9
+
10
+ authors:
11
+ - Thomas Sawyer
12
+ - Florian Frank
13
+
14
+ description:
15
+ ANSI codes at your fingertips!
16
+
17
+ resources:
18
+ homepage : http://rubyworks.github.com/ansi
19
+ development : http://github.com/rubyworks/ansi
20
+ reference : http://rubyworks.github.com/ansi/rdoc
21
+ mailinglist : http://groups.google.com/group/rubyworks-mailinglist
22
+ repository : git://github.com/rubyworks/ansi.git
23
+
data/ROADMAP ADDED
@@ -0,0 +1,11 @@
1
+ = ROADMAP
2
+
3
+ == 1.3
4
+
5
+ === Tables
6
+
7
+ Consider adding something a Table class.
8
+
9
+ See http://github.com/visionmedia/terminal-table
10
+ and http://github.com/aptinio/text-table
11
+
data/Syckfile CHANGED
@@ -16,11 +16,8 @@ gemcutter:
16
16
 
17
17
  box:
18
18
  service: Box
19
+ types : [gem]
19
20
  active : true
20
- types : [gem, gz]
21
- include: [bin, demo, lib, meta, plug, test, "[A-Z]*"]
22
- exclude: ~
23
- master : false
24
21
 
25
22
  ridoc:
26
23
  service: RIDoc
data/VERSION ADDED
@@ -0,0 +1,5 @@
1
+ name : ansi
2
+ major: 1
3
+ minor: 2
4
+ patch: 2
5
+ date : 2010-06-11
@@ -0,0 +1,9 @@
1
+ require 'benchmark'
2
+ require 'ansi/code'
3
+
4
+ n = 50000
5
+ Benchmark.bmbm(10) do |x|
6
+ x.report("string:"){ n.times{|i| ANSI::Code.red(i.to_s) } }
7
+ x.report("block :"){ n.times{|i| ANSI::Code.red{i.to_s} } }
8
+ end
9
+
@@ -20,7 +20,7 @@ module ANSI
20
20
  #
21
21
  # == Supported ANSI Commands
22
22
  #
23
- # The following is a list of supported codes.
23
+ # The following is a list of supported display codes.
24
24
  #
25
25
  # save
26
26
  # restore
@@ -33,7 +33,8 @@ module ANSI
33
33
  # down
34
34
  # left
35
35
  # right
36
- # display
36
+ #
37
+ # The following is a list of supported "style" codes.
37
38
  #
38
39
  # clear
39
40
  # reset # synonym for :clear
@@ -48,6 +49,8 @@ module ANSI
48
49
  # concealed
49
50
  # strikethrough # not widely implemented
50
51
  #
52
+ # The following is a list of supported color codes.
53
+ #
51
54
  # black
52
55
  # red
53
56
  # green
@@ -66,13 +69,19 @@ module ANSI
66
69
  # on_cyan
67
70
  # on_white
68
71
  #
72
+ # In addition there are color combinations like +red_on_white+.
73
+ #
74
+ # == Acknowledgement
75
+ #
69
76
  # This library is a partial adaptation of ANSIColor by Florian Frank.
70
77
  #
71
78
  # ANSIColor Copyright (c) 2002 Florian Frank
72
79
  #
80
+ # == Developer's Notes
81
+ #
73
82
  # TODO: Any ANSI codes left to add? Modes?
74
83
  #
75
- # TODO: up, down, right, left, etc could have yielding methods too
84
+ # TODO: up, down, right, left, etc could have yielding methods too?
76
85
 
77
86
  module Code
78
87
  extend self
@@ -135,7 +144,7 @@ module ANSI
135
144
  module_eval <<-END, __FILE__, __LINE__
136
145
  def #{style}(string=nil)
137
146
  if string
138
- warn "use ANSI block notation for future versions"
147
+ #warn "use ANSI block notation for future versions"
139
148
  return "\#{#{style.upcase}}\#{string}\#{CLEAR}"
140
149
  end
141
150
  if block_given?
@@ -150,11 +159,13 @@ module ANSI
150
159
  %w{black red green yellow blue magenta cyan white}
151
160
  end
152
161
 
162
+ # Dynamically create color methods.
163
+
153
164
  colors.each do |color|
154
165
  module_eval <<-END, __FILE__, __LINE__
155
166
  def #{color}(string=nil)
156
167
  if string
157
- warn "use ANSI block notation for future versions"
168
+ #warn "use ANSI block notation for future versions"
158
169
  return "\#{#{color.upcase}}\#{string}\#{CLEAR}"
159
170
  end
160
171
  if block_given?
@@ -165,7 +176,7 @@ module ANSI
165
176
 
166
177
  def on_#{color}(string=nil)
167
178
  if string
168
- warn "use ANSI block notation for future versions"
179
+ #warn "use ANSI block notation for future versions"
169
180
  return "\#{ON_#{color.upcase}}\#{string}\#{CLEAR}"
170
181
  end
171
182
  if block_given?
@@ -181,7 +192,11 @@ module ANSI
181
192
  colors.each do |color|
182
193
  colors.each do |on_color|
183
194
  module_eval <<-END, __FILE__, __LINE__
184
- def #{color}_on_#{on_color}
195
+ def #{color}_on_#{on_color}(string=nil)
196
+ if string
197
+ #warn "use ANSI block notation for future versions"
198
+ return #{color.upcase} + ON_#{color.upcase} + string + CLEAR
199
+ end
185
200
  if block_given?
186
201
  #{color.upcase} + ON_#{on_color.upcase} + yield.to_s + CLEAR
187
202
  else
@@ -192,10 +207,12 @@ module ANSI
192
207
  end
193
208
  end
194
209
 
210
+ # Clear code.
195
211
  def clear
196
212
  CLEAR
197
213
  end
198
214
 
215
+ # Reset code.
199
216
  def reset
200
217
  RESET
201
218
  end
@@ -230,13 +247,22 @@ module ANSI
230
247
  CLS
231
248
  end
232
249
 
233
- #--
234
- #def position
235
- # "\e[#;#R"
236
- #end
237
- #++
250
+ # Like +move+ but returns to original positon after
251
+ # yielding the block.
252
+ def display(line, column=0) #:yield:
253
+ result = "\e[s"
254
+ result << "\e[#{line.to_i};#{column.to_i}H"
255
+ if block_given?
256
+ result << yield
257
+ result << "\e[u"
258
+ #elsif string
259
+ # result << string
260
+ # result << "\e[u"
261
+ end
262
+ result
263
+ end
238
264
 
239
- # Move curose to line and column.
265
+ # Move cursor to line and column.
240
266
  def move(line, column=0)
241
267
  "\e[#{line.to_i};#{column.to_i}H"
242
268
  end
@@ -261,52 +287,42 @@ module ANSI
261
287
  "\e[#{spaces.to_i}C"
262
288
  end
263
289
 
264
- # Like +move+ but returns to original positon after
265
- # yielding the block.
266
- def display(line, column=0) #:yield:
267
- result = "\e[s"
268
- result << "\e[#{line.to_i};#{column.to_i}H"
269
- if block_given?
270
- result << yield
271
- result << "\e[u"
272
- #elsif string
273
- # result << string
274
- # result << "\e[u"
275
- #elsif respond_to?(:to_str)
276
- # result << self
277
- # result << "\e[u"
278
- end
279
- result
280
- end
290
+ ##
291
+ #def position
292
+ # "\e[#;#R"
293
+ #end
281
294
 
295
+ # Apply ansi codes to block yield.
282
296
  #
283
- def style(*codes)
297
+ # style(:red, :on_white){ "Valentine" }
298
+ #
299
+ def style(*codes) #:yield:
284
300
  s = ""
285
301
  codes.each do |code|
286
302
  s << "\e[#{TABLE[code]}m"
287
303
  end
288
- s << yield
304
+ s << yield.to_s
289
305
  s << CLEAR
290
306
  end
291
307
 
308
+ # Alternate term for #style.
292
309
  alias_method :color, :style
293
310
 
294
311
  #
295
- def unstyle
312
+ def unstyle #:yield:
296
313
  if block_given?
297
314
  yield.gsub(PATTERN, '')
298
- #elsif string
299
- # string.gsub(ColoredRegexp, '')
300
315
  else
301
316
  ''
302
317
  end
303
318
  end
304
319
 
320
+ #
305
321
  alias_method :uncolor, :unstyle
306
322
 
307
- # This old term will be deprecated.
323
+ # DEPRECATE: This old term will be deprecated.
308
324
  def uncolered(string=nil)
309
- warn "use #uncolor in block form for future version"
325
+ warn "ansi: use #uncolor or #unansi for future version"
310
326
  if block_given?
311
327
  yield.gsub(PATTERN, '')
312
328
  elsif string
@@ -316,6 +332,27 @@ module ANSI
316
332
  end
317
333
  end
318
334
 
335
+ # This method is just like #style, except it takes a string
336
+ # rather than a block. The primary purpose of this method
337
+ # is to speed up the String#ansi call.
338
+ #
339
+ # ansi("Valentine", :red, :on_white)
340
+ #
341
+ def ansi(string, *codes)
342
+ s = ""
343
+ codes.each do |code|
344
+ s << "\e[#{TABLE[code]}m"
345
+ end
346
+ s << string
347
+ s << CLEAR
348
+ end
349
+
350
+ # Remove ansi codes from +string+. This method is like unstyle,
351
+ # but takes a string rather than a block.
352
+ def unansi(string)
353
+ string.gsub(PATTERN, '')
354
+ end
355
+
319
356
  # Regexp for matching style and color codes.
320
357
  PATTERN = /\e\[([34][0-7]|[0-9])m/
321
358
 
@@ -354,14 +391,28 @@ module ANSI
354
391
  end
355
392
 
356
393
  extend Code
357
-
358
394
  end
359
395
 
360
396
  #
361
397
  class String
362
398
  #
363
399
  def ansi(*codes)
364
- ANSI::Code.style(*codes){ self }
400
+ ANSI::Code.ansi(self, *codes)
401
+ end
402
+
403
+ #
404
+ def ansi!(*codes)
405
+ replace(ansi(*codes))
406
+ end
407
+
408
+ #
409
+ def unansi
410
+ ANSI::Code.unansi(self)
411
+ end
412
+
413
+ #
414
+ def unansi!
415
+ replace(unansi)
365
416
  end
366
417
  end
367
418
 
@@ -107,6 +107,16 @@ module ANSI
107
107
  def white_on_magenta ; ANSI::Code.white_on_magenta { to_s } ; end
108
108
  def white_on_cyan ; ANSI::Code.white_on_cyan { to_s } ; end
109
109
 
110
+ # Move cursor to line and column, insert +self.to_s+ and return to
111
+ # original positon.
112
+ def display(line, column=0)
113
+ result = "\e[s"
114
+ result << "\e[#{line.to_i};#{column.to_i}H"
115
+ result << to_s
116
+ result << "\e[u"
117
+ result
118
+ end
119
+
110
120
  end
111
121
 
112
122
  end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'ansi/mixin'
3
+
4
+ class TestANSIMixin < Test::Unit::TestCase
5
+
6
+ class ::String
7
+ include ANSI::Mixin
8
+ end
9
+
10
+ def test_methods
11
+ str = "Hello".red + "World".blue
12
+ out = "\e[31mHello\e[0m\e[34mWorld\e[0m"
13
+ assert_equal(out, str)
14
+ end
15
+
16
+ def test_display
17
+ str = "Hello".display(4,10)
18
+ out = "\e[s\e[4;10HHello\e[u"
19
+ assert_equal(out, str)
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 1
9
- version: 1.2.1
8
+ - 2
9
+ version: 1.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Thomas Sawyer
@@ -15,19 +15,21 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-05-10 00:00:00 -04:00
18
+ date: 2010-06-12 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
22
  description: ANSI codes at your fingertips!
23
- email:
23
+ email: rubyworks-mailinglist@googlegroups.com
24
24
  executables: []
25
25
 
26
26
  extensions: []
27
27
 
28
- extra_rdoc_files: []
29
-
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
30
  files:
31
+ - Syckfile
32
+ - eg/string_vs_block.rb
31
33
  - lib/ansi/bbcode.rb
32
34
  - lib/ansi/code.rb
33
35
  - lib/ansi/logger.rb
@@ -40,38 +42,26 @@ files:
40
42
  - lib/ansi/terminal/win32.rb
41
43
  - lib/ansi/terminal.rb
42
44
  - lib/ansi.rb
43
- - meta/authors
44
- - meta/collection
45
- - meta/copyright
46
- - meta/created
47
- - meta/description
48
- - meta/license
49
- - meta/name
50
- - meta/released
51
- - meta/ruby
52
- - meta/sites/development
53
- - meta/sites/documentation
54
- - meta/sites/homepage
55
- - meta/sites/mailinglist
56
- - meta/sites/repository
57
- - meta/summary
58
- - meta/title
59
- - meta/version
60
45
  - test/test_ansicode.rb
61
46
  - test/test_bbcode.rb
47
+ - test/test_mixin.rb
62
48
  - test/test_progressbar.rb
49
+ - PROFILE
63
50
  - LICENSE
64
51
  - README.rdoc
65
52
  - HISTORY
66
- - Syckfile
53
+ - ROADMAP
54
+ - VERSION
67
55
  has_rdoc: true
68
- homepage: http://death.rubyforge.org/ansi
56
+ homepage: http://rubyworks.github.com/ansi
69
57
  licenses: []
70
58
 
71
59
  post_install_message:
72
60
  rdoc_options:
73
61
  - --title
74
62
  - ANSI API
63
+ - --main
64
+ - README.rdoc
75
65
  require_paths:
76
66
  - lib
77
67
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -90,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
80
  version: "0"
91
81
  requirements: []
92
82
 
93
- rubyforge_project: death
83
+ rubyforge_project: ansi
94
84
  rubygems_version: 1.3.6
95
85
  signing_key:
96
86
  specification_version: 3
@@ -98,4 +88,5 @@ summary: ANSI codes at your fingertips!
98
88
  test_files:
99
89
  - test/test_ansicode.rb
100
90
  - test/test_bbcode.rb
91
+ - test/test_mixin.rb
101
92
  - test/test_progressbar.rb
@@ -1,2 +0,0 @@
1
- Thomas Sawyer
2
- Florian Frank
@@ -1 +0,0 @@
1
- death
@@ -1 +0,0 @@
1
- Copyright (c) 2009 Thomas Sawyer
@@ -1 +0,0 @@
1
- 2004-08-01
@@ -1 +0,0 @@
1
- ANSI codes at your fingertips!
@@ -1 +0,0 @@
1
- LGPLv3
data/meta/name DELETED
@@ -1 +0,0 @@
1
- ansi
@@ -1 +0,0 @@
1
- 2010-05-10
data/meta/ruby DELETED
@@ -1 +0,0 @@
1
- 1.8.7
@@ -1 +0,0 @@
1
- http://github.com/rubyworks/ansi/
@@ -1 +0,0 @@
1
- http://rubyworks.github.com/ansi/rdoc/
@@ -1 +0,0 @@
1
- http://death.rubyforge.org/ansi
@@ -1 +0,0 @@
1
- http://groups.google.com/group/rubyworks-mailinglist/
@@ -1 +0,0 @@
1
- git://github.com/rubyworks/ansi.git
@@ -1 +0,0 @@
1
- ANSI codes at your fingertips!
data/meta/title DELETED
@@ -1 +0,0 @@
1
- ANSI
@@ -1 +0,0 @@
1
- 1.2.1