ansi 1.1.0 → 1.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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2004 Thomas Sawyer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+
@@ -0,0 +1,85 @@
1
+ = ANSI
2
+
3
+ * http://rubyworks.github.com/ansi
4
+ * http://github.com/rubyworks/ansi
5
+
6
+
7
+ == DESCRIPTION
8
+
9
+ The ANSI project is a collection of ANSI escape code related libraries
10
+ enabling ANSI code based colorization and stylization of output.
11
+ It is very nice for beautifying shell output.
12
+
13
+ This collection is based on a set of scripts spun-off from
14
+ Ruby Facets. Include are Code (used to be ANSICode), Logger,
15
+ ProgressBar and String. In addition the library includes
16
+ Terminal which provides information about the current output
17
+ device.
18
+
19
+
20
+ == FEATURES/ISSUES
21
+
22
+ * ANSI::Code can be used as a mixin or for module functions.
23
+ * ANSI::Mixin provides an alternative mixin like +colored+ gem.
24
+ * Single extension to String class makes common usage very elegant.
25
+ * Very Good coverage of standard ANSI codes.
26
+
27
+
28
+ == RELEASE NOTES
29
+
30
+ Please see HISTORY file.
31
+
32
+
33
+ == SYNOPSIS
34
+
35
+ There are a number of modules and classes provided by the ANSI
36
+ package. To get a good unserstanding of them it is best to puruse
37
+ the QED documents[http://github.com/rubyworks/ansi/tree/master/qed/]
38
+ or the API documentation[http://rubyworks.github.com/ansi/rdoc/].
39
+
40
+ At the heart of all the provided libraries lies the ANSI::Code module
41
+ which defines ANSI codes as constants and methods. For exmaple:
42
+
43
+ require 'ansi/code'
44
+
45
+ include ANSI::Code
46
+
47
+ red + "Hello" + blue + "World"
48
+ => "\e[31mHello\e[34mWorld"
49
+
50
+ Or in block form.
51
+
52
+ red{ "Hello" } + blue{ "World" }
53
+ => "\e[31mHello\e[0m\e[34mWorld\e[0m"
54
+
55
+ Rather than include +ANSI::Code+, these mehods can also be called as module
56
+ methods. Either as <code>ANSI::Code.red</code> or just <code>ANSI.red</code>.
57
+
58
+ The methods defined by this module are used throughout the rest of
59
+ the system.
60
+
61
+
62
+ == HOW TO INSTALL
63
+
64
+ To install with RubyGems simply open a console and type:
65
+
66
+ $ sudo gem install ansi
67
+
68
+ Local installation requires Setup.rb (gem install setup),
69
+ then download[http://github.com/rubyworks/ansi/download] the tarball package and type:
70
+
71
+ $ tar -xvzf ansi-1.0.0.tgz
72
+ $ cd ansi-1.0.0
73
+ $ sudo setup.rb all
74
+
75
+ Windows users use 'ruby setup.rb all'.
76
+
77
+
78
+ == LICENSE/COPYRIGHT
79
+
80
+ Copyright (c) 2004 Thomas Sawyer
81
+
82
+ This program is ditributed unser the terms of the MIT license.
83
+
84
+ See LICENSE file for details.
85
+
@@ -0,0 +1,77 @@
1
+ ---
2
+ email:
3
+ service : Email
4
+ file : ~
5
+ subject : ~
6
+ mailto : ruby-talk@ruby-lang.org
7
+ active : true
8
+
9
+ grancher:
10
+ service: grancher
11
+ active: true
12
+
13
+ gemcutter:
14
+ service: GemCutter
15
+ active: true
16
+
17
+ box:
18
+ service: Box
19
+ active : true
20
+ types : [gem, gz]
21
+ include: [bin, demo, lib, meta, plug, test, "[A-Z]*"]
22
+ exclude: ~
23
+ master : false
24
+
25
+ ridoc:
26
+ service: RIDoc
27
+ include: ~
28
+ exclude: ~
29
+ active : true
30
+
31
+ rdoc:
32
+ service : RDoc
33
+ template: newfish
34
+ include : ~
35
+ exclude : [ Syckfile ]
36
+ active : true
37
+
38
+ syntax:
39
+ service : Syntax
40
+ loadpath : ~
41
+ exclude : ~
42
+ active : false
43
+
44
+ testrb:
45
+ service : testrb
46
+ tests : ~
47
+ exclude : ~
48
+ loadpath : ~
49
+ requires : ~
50
+ live : false
51
+ active : false
52
+
53
+ dnote:
54
+ service : DNote
55
+ loadpath : ~
56
+ labels : ~
57
+ exclude : [work]
58
+ output : ~
59
+ format : ~
60
+ active : true
61
+
62
+ stats:
63
+ service : Stats
64
+ title : ~
65
+ loadpath : ~
66
+ exclude : ~
67
+ output : ~
68
+ active : true
69
+
70
+ vclog:
71
+ service : VClog
72
+ formats : [json]
73
+ #layout : rel # gnu
74
+ typed : false
75
+ output : ~
76
+ active : false
77
+
@@ -1,18 +1,9 @@
1
- # Ansi::Code Copyright (c) 2009 Thomas Sawyer, LGPL
2
- #
3
- # This library is a partial adaptation of ANSIColor by Florian Frank.
4
- #
5
- # ANSIColor Copyright (c) 2002 Florian Frank, LGPL
6
- #
7
- # TODO: Need to add rest of ANSI codes. Include modes?
8
- # TODO: Re-evaluate how color/yielding methods are defined.
9
- # TODO: Maybe up, down, right, left should have yielding methods too?
1
+ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
10
2
 
11
- #
12
3
  module ANSI
13
4
 
14
- # Currently Windows is not supported.
15
- SUPPORTED = !(RUBY_PLATFORM =~ /win/)
5
+ # deprecate
6
+ SUPPORTED = true
16
7
 
17
8
  # = ANSI Codes
18
9
  #
@@ -21,10 +12,10 @@ module ANSI
21
12
  #
22
13
  # include Ansi::Code
23
14
  #
24
- # p red, "Hello", blue, "World"
15
+ # red + "Hello" + blue + "World"
25
16
  # => "\e[31mHello\e[34mWorld"
26
17
  #
27
- # p red { "Hello" } + blue { "World" }
18
+ # red { "Hello" } + blue { "World" }
28
19
  # => "\e[31mHello\e[0m\e[34mWorld\e[0m"
29
20
  #
30
21
  # == Supported ANSI Commands
@@ -75,31 +66,169 @@ module ANSI
75
66
  # on_cyan
76
67
  # on_white
77
68
  #
78
- module Code
69
+ # This library is a partial adaptation of ANSIColor by Florian Frank.
70
+ #
71
+ # ANSIColor Copyright (c) 2002 Florian Frank
72
+ #
73
+ # TODO: Any ANSI codes left to add? Modes?
74
+ #
75
+ # TODO: up, down, right, left, etc could have yielding methods too
79
76
 
77
+ module Code
80
78
  extend self
81
79
 
80
+ CLEAR = "\e[0m"
81
+ RESET = "\e[0m"
82
+ BOLD = "\e[1m"
83
+ DARK = "\e[2m"
84
+ ITALIC = "\e[3m" # not widely implemented
85
+ UNDERLINE = "\e[4m"
86
+ UNDERSCORE = "\e[4m"
87
+ BLINK = "\e[5m"
88
+ RAPID = "\e[6m" # not widely implemented
89
+ REVERSE = "\e[7m"
90
+ NEGATIVE = "\e[7m" # alternate to reverse because of String#reverse
91
+ CONCEALED = "\e[8m"
92
+ STRIKE = "\e[9m" # not widely implemented
93
+
94
+ BLACK = "\e[30m"
95
+ RED = "\e[31m"
96
+ GREEN = "\e[32m"
97
+ YELLOW = "\e[33m"
98
+ BLUE = "\e[34m"
99
+ MAGENTA = "\e[35m"
100
+ CYAN = "\e[36m"
101
+ WHITE = "\e[37m"
102
+
103
+ ON_BLACK = "\e[40m"
104
+ ON_RED = "\e[41m"
105
+ ON_GREEN = "\e[42m"
106
+ ON_YELLOW = "\e[43m"
107
+ ON_BLUE = "\e[44m"
108
+ ON_MAGENTA = "\e[45m"
109
+ ON_CYAN = "\e[46m"
110
+ ON_WHITE = "\e[47m"
111
+
112
+ # Save current cursor positon.
113
+ SAVE = "\e[s"
114
+
115
+ # Restore saved cursor positon.
116
+ RESTORE = "\e[u"
117
+
118
+ # Clear to the end of the current line.
119
+ CLEAR_LINE = "\e[K"
120
+
121
+ # Clear to the end of the current line.
122
+ CLR = "\e[K"
123
+
124
+ # Clear the screen and move cursor to home.
125
+ CLEAR_SCREEN = "\e[2J"
126
+
127
+ # Clear the screen and move cursor to home.
128
+ CLS = "\e[2J"
129
+
130
+ def self.styles
131
+ %w{bold dark italic underline underscore blink rapid reverse negative concealed strike}
132
+ end
133
+
134
+ styles.each do |style|
135
+ module_eval <<-END, __FILE__, __LINE__
136
+ def #{style}(string=nil)
137
+ if string
138
+ warn "use ANSI block notation for future versions"
139
+ return "\#{#{style.upcase}}\#{string}\#{CLEAR}"
140
+ end
141
+ if block_given?
142
+ return "\#{#{style.upcase}}\#{yield}\#{CLEAR}"
143
+ end
144
+ #{style.upcase}
145
+ end
146
+ END
147
+ end
148
+
149
+ def self.colors
150
+ %w{black red green yellow blue magenta cyan white}
151
+ end
152
+
153
+ colors.each do |color|
154
+ module_eval <<-END, __FILE__, __LINE__
155
+ def #{color}(string=nil)
156
+ if string
157
+ warn "use ANSI block notation for future versions"
158
+ return "\#{#{color.upcase}}\#{string}\#{CLEAR}"
159
+ end
160
+ if block_given?
161
+ return "\#{#{color.upcase}}\#{yield}\#{CLEAR}"
162
+ end
163
+ #{color.upcase}
164
+ end
165
+
166
+ def on_#{color}(string=nil)
167
+ if string
168
+ warn "use ANSI block notation for future versions"
169
+ return "\#{ON_#{color.upcase}}\#{string}\#{CLEAR}"
170
+ end
171
+ if block_given?
172
+ return "\#{ON_#{color.upcase}}\#{yield}\#{CLEAR}"
173
+ end
174
+ ON_#{color.upcase}
175
+ end
176
+ END
177
+ end
178
+
179
+ # Dynamically create color on color methods.
180
+
181
+ colors.each do |color|
182
+ colors.each do |on_color|
183
+ module_eval <<-END, __FILE__, __LINE__
184
+ def #{color}_on_#{on_color}
185
+ if block_given?
186
+ #{color.upcase} + ON_#{on_color.upcase} + yield.to_s + CLEAR
187
+ else
188
+ #{color.upcase} + ON_#{on_color.upcase}
189
+ end
190
+ end
191
+ END
192
+ end
193
+ end
194
+
195
+ def clear
196
+ CLEAR
197
+ end
198
+
199
+ def reset
200
+ RESET
201
+ end
202
+
82
203
  # Save current cursor positon.
83
204
  def save
84
- "\e[s"
205
+ SAVE
85
206
  end
86
207
 
87
208
  # Restore saved cursor positon.
88
209
  def restore
89
- "\e[u"
210
+ RESTORE
211
+ end
212
+
213
+ # Clear to the end of the current line.
214
+ def clear_line
215
+ CLEAR_LINE
216
+ end
217
+
218
+ # Clear to the end of the current line.
219
+ def clr
220
+ CLR
90
221
  end
91
222
 
92
223
  # Clear the screen and move cursor to home.
93
224
  def clear_screen
94
- "\e[2J"
225
+ CLEAR_SCREEN
95
226
  end
96
- alias_method :cls, :clear_screen
97
227
 
98
- # Clear to the end of the current line.
99
- def clear_line
100
- "\e[K"
228
+ # Clear the screen and move cursor to home.
229
+ def cls
230
+ CLS
101
231
  end
102
- alias_method :clr, :clear_line
103
232
 
104
233
  #--
105
234
  #def position
@@ -108,126 +237,131 @@ module ANSI
108
237
  #++
109
238
 
110
239
  # Move curose to line and column.
111
- def move( line, column=0 )
240
+ def move(line, column=0)
112
241
  "\e[#{line.to_i};#{column.to_i}H"
113
242
  end
114
243
 
115
244
  # Move cursor up a specificed number of spaces.
116
- def up( spaces=1 )
245
+ def up(spaces=1)
117
246
  "\e[#{spaces.to_i}A"
118
247
  end
119
248
 
120
249
  # Move cursor down a specificed number of spaces.
121
- def down( spaces=1 )
250
+ def down(spaces=1)
122
251
  "\e[#{spaces.to_i}B"
123
252
  end
124
253
 
125
254
  # Move cursor left a specificed number of spaces.
126
- def left( spaces=1 )
255
+ def left(spaces=1)
127
256
  "\e[#{spaces.to_i}D"
128
257
  end
129
258
 
130
259
  # Move cursor right a specificed number of spaces.
131
- def right( spaces=1 )
260
+ def right(spaces=1)
132
261
  "\e[#{spaces.to_i}C"
133
262
  end
134
263
 
135
- # Like +move+ but returns to original positon
136
- # after yielding block or adding string argument.
137
- def display( line, column=0, string=nil ) #:yield:
264
+ # Like +move+ but returns to original positon after
265
+ # yielding the block.
266
+ def display(line, column=0) #:yield:
138
267
  result = "\e[s"
139
268
  result << "\e[#{line.to_i};#{column.to_i}H"
140
269
  if block_given?
141
270
  result << yield
142
271
  result << "\e[u"
143
- elsif string
144
- result << string
145
- result << "\e[u"
146
- elsif respond_to?(:to_str)
147
- result << self
148
- 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"
149
278
  end
150
- return result
279
+ result
151
280
  end
152
281
 
153
- # Define color codes.
154
- def self.define_ansicolor_method(name,code)
155
- class_eval <<-HERE
156
- def #{name.to_s}(string = nil)
157
- result = "\e[#{code}m"
158
- if block_given?
159
- result << yield
160
- result << "\e[0m"
161
- elsif string
162
- result << string
163
- result << "\e[0m"
164
- elsif respond_to?(:to_str)
165
- result << self
166
- result << "\e[0m"
167
- end
168
- return result
169
- end
170
- HERE
171
- end
172
-
173
- @@colors = [
174
- [ :clear , 0 ],
175
- [ :reset , 0 ], # synonym for :clear
176
- [ :bold , 1 ],
177
- [ :dark , 2 ],
178
- [ :italic , 3 ], # not widely implemented
179
- [ :underline , 4 ],
180
- [ :underscore , 4 ], # synonym for :underline
181
- [ :blink , 5 ],
182
- [ :rapid_blink , 6 ], # not widely implemented
183
- [ :negative , 7 ], # no reverse because of String#reverse
184
- [ :concealed , 8 ],
185
- [ :strikethrough, 9 ], # not widely implemented
186
- [ :black , 30 ],
187
- [ :red , 31 ],
188
- [ :green , 32 ],
189
- [ :yellow , 33 ],
190
- [ :blue , 34 ],
191
- [ :magenta , 35 ],
192
- [ :cyan , 36 ],
193
- [ :white , 37 ],
194
- [ :on_black , 40 ],
195
- [ :on_red , 41 ],
196
- [ :on_green , 42 ],
197
- [ :on_yellow , 43 ],
198
- [ :on_blue , 44 ],
199
- [ :on_magenta , 45 ],
200
- [ :on_cyan , 46 ],
201
- [ :on_white , 47 ],
202
- ]
203
-
204
- @@colors.each do |c, v|
205
- define_ansicolor_method(c, v)
206
- end
207
-
208
- ColoredRegexp = /\e\[([34][0-7]|[0-9])m/
209
-
210
- module_function
282
+ #
283
+ def style(*codes)
284
+ s = ""
285
+ codes.each do |code|
286
+ s << "\e[#{TABLE[code]}m"
287
+ end
288
+ s << yield
289
+ s << CLEAR
290
+ end
291
+
292
+ alias_method :color, :style
211
293
 
212
294
  #
213
- def uncolored(string = nil)
295
+ def unstyle
214
296
  if block_given?
215
- yield.gsub(ColoredRegexp, '')
216
- elsif string
217
- string.gsub(ColoredRegexp, '')
218
- elsif respond_to?(:to_str)
219
- gsub(ColoredRegexp, '')
297
+ yield.gsub(PATTERN, '')
298
+ #elsif string
299
+ # string.gsub(ColoredRegexp, '')
220
300
  else
221
301
  ''
222
302
  end
223
303
  end
224
304
 
225
- #
226
- def colors
227
- @@colors.map { |c| c[0] }
305
+ alias_method :uncolor, :unstyle
306
+
307
+ # This old term will be deprecated.
308
+ def uncolered(string=nil)
309
+ warn "use #uncolor in block form for future version"
310
+ if block_given?
311
+ yield.gsub(PATTERN, '')
312
+ elsif string
313
+ string.gsub(PATTERN, '')
314
+ else
315
+ ''
316
+ end
228
317
  end
229
318
 
319
+ # Regexp for matching style and color codes.
320
+ PATTERN = /\e\[([34][0-7]|[0-9])m/
321
+
322
+ # Table of style and color codes.
323
+ TABLE = {
324
+ :clear => 0,
325
+ :reset => 0,
326
+ :bold => 1,
327
+ :dark => 2,
328
+ :italic => 3,
329
+ :underline => 4,
330
+ :underscore => 4,
331
+ :blink => 5,
332
+ :rapid => 6,
333
+ :reverse => 7,
334
+ :negative => 7,
335
+ :concealed => 8,
336
+ :strike => 9,
337
+ :black => 30,
338
+ :red => 31,
339
+ :green => 32,
340
+ :yellow => 33,
341
+ :blue => 34,
342
+ :magenta => 35,
343
+ :cyan => 36,
344
+ :white => 37,
345
+ :on_black => 40,
346
+ :on_red => 41,
347
+ :on_green => 42,
348
+ :on_yellow => 43,
349
+ :on_blue => 44,
350
+ :on_magenta => 45,
351
+ :on_cyan => 46,
352
+ :on_white => 47
353
+ }
230
354
  end
231
355
 
356
+ extend Code
357
+
358
+ end
359
+
360
+ #
361
+ class String
362
+ #
363
+ def ansi(*codes)
364
+ ANSI::Code.style(*codes){ self }
365
+ end
232
366
  end
233
367