ansi 1.4.3 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ # ANSI::BBCode
2
+
3
+ The BBCode module provides methods for converting between
4
+ BBCodes, basic HTML and ANSI codes.
5
+
6
+ require 'ansi/bbcode'
7
+
8
+ BBCodes are color and style codes in square brackets, quite
9
+ popular with on line forums.
10
+
11
+ bbcode = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
12
+
13
+ We can convert this to ANSI code simply enough:
14
+
15
+ ansi = ANSI::BBCode.bbcode_to_ansi(bbcode)
16
+
17
+ ansi.assert == "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
18
+
19
+ In addition the BBCode module supports conversion to simple HTML.
20
+
21
+ html = ANSI::BBCode.bbcode_to_html(bbcode)
22
+
23
+ html.assert == "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
24
+
@@ -0,0 +1,8 @@
1
+ # ANSI::Terminal
2
+
3
+ We should be ables to get the terminal width via the `terminal_width` method.
4
+
5
+ width = ANSI::Terminal.terminal_width
6
+
7
+ Fixnum.assert === width
8
+
@@ -1,63 +1,77 @@
1
1
  ---
2
- source:
3
- - var
2
+ revision: 2013
3
+ type: ruby
4
+ sources:
5
+ - INDEX.yml
4
6
  authors:
5
7
  - name: Thomas Sawyer
6
8
  email: transfire@gmail.com
7
9
  - name: Florian Frank
8
- copyrights:
9
- - holder: Rubyworks
10
- year: '2009'
10
+ organizations: []
11
11
  requirements:
12
- - name: detroit
13
- groups:
12
+ - groups:
14
13
  - build
15
14
  development: true
16
- - name: qed
17
- groups:
15
+ name: mast
16
+ - groups:
17
+ - build
18
+ development: true
19
+ name: indexer
20
+ - groups:
21
+ - build
22
+ development: true
23
+ name: ergo
24
+ - groups:
18
25
  - test
19
26
  development: true
20
- - name: lemon
21
- groups:
27
+ name: qed
28
+ - groups:
22
29
  - test
23
30
  development: true
24
- dependencies: []
25
- alternatives: []
31
+ name: ae
32
+ - groups:
33
+ - test
34
+ development: true
35
+ name: lemon
26
36
  conflicts: []
27
- repositories:
28
- - uri: git://github.com/rubyworks/ansi.git
29
- scm: git
30
- name: upstream
37
+ alternatives: []
31
38
  resources:
32
- - uri: http://rubyworks.github.com/ansi
33
- label: Website
34
- type: home
35
- - uri: http://github.com/rubyworks/ansi
36
- label: Source Code
37
- type: code
38
- - uri: http://rubydoc.info/gems/ansi/frames
39
+ - type: home
40
+ uri: http://rubyworks.github.com/ansi
41
+ label: Homepage
42
+ - type: docs
43
+ uri: http://rubydoc.info/gems/ansi/frames
39
44
  label: Documentation
40
- type: docs
41
- - uri: http://groups.google.com/group/rubyworks-mailinglist
45
+ - type: code
46
+ uri: http://github.com/rubyworks/ansi
47
+ label: Source Code
48
+ - type: bugs
49
+ uri: http://github.com/rubyworks/ansi/issues
50
+ label: Issue Tracker
51
+ - type: mail
52
+ uri: http://groups.google.com/group/rubyworks-mailinglist
42
53
  label: Mailing List
43
- type: mail
54
+ repositories:
55
+ - name: upstream
56
+ scm: git
57
+ uri: git://github.com/rubyworks/ansi.git
44
58
  categories: []
45
- extra: {}
46
- load_path:
47
- - lib
48
- revision: 0
59
+ copyrights:
60
+ - holder: Rubyworks
61
+ year: '2009'
62
+ license: BSD-2-Clause
63
+ customs: []
64
+ paths:
65
+ lib:
66
+ - lib
49
67
  name: ansi
50
68
  title: ANSI
51
- created: '2009-08-01'
69
+ version: 1.5.0
52
70
  summary: ANSI at your fingertips!
53
- description: ! 'The ANSI project is a superlative collection of ANSI escape code related
54
- libraries
55
-
56
- enabling ANSI colorization and stylization of console output. Byte for byte
57
-
58
- ANSI is the best ANSI code library available for the Ruby programming
59
-
60
- language.'
61
- organization: Rubyworks
62
- version: 1.4.3
63
- date: '2012-06-28'
71
+ description: The ANSI project is a superlative collection of ANSI escape code related
72
+ libraries eabling ANSI colorization and stylization of console output. Byte for
73
+ byte ANSI is the best ANSI code library available for the Ruby programming language.
74
+ orgranizations:
75
+ - Rubyworks
76
+ created: '2009-08-01'
77
+ date: '2015-01-16'
@@ -1,7 +1,5 @@
1
1
  module ANSI
2
2
 
3
- require 'ansi/version'
4
-
5
3
  # Table of codes used throughout the system.
6
4
  #
7
5
  # @see http://en.wikipedia.org/wiki/ANSI_escape_code
@@ -9,7 +7,7 @@ module ANSI
9
7
  :clear => 0,
10
8
  :reset => 0,
11
9
  :bright => 1,
12
- :bold => 1,
10
+ :bold => 1,
13
11
  :faint => 2,
14
12
  :dark => 2,
15
13
  :italic => 3,
@@ -23,7 +21,6 @@ module ANSI
23
21
  :inverse => 7,
24
22
  :reverse => 7,
25
23
  :negative => 7,
26
- :concealed => 8,
27
24
  :swap => 7,
28
25
  :conceal => 8,
29
26
  :concealed => 8,
@@ -87,12 +84,17 @@ module ANSI
87
84
 
88
85
  #
89
86
  SPECIAL_CHART = {
90
- :save => "\e[s", # Save current cursor positon.
91
- :restore => "\e[u", # Restore saved cursor positon.
92
- :clear_line => "\e[K", # Clear to the end of the current line.
93
- :clr => "\e[K", # Clear to the end of the current line.
94
- :clear_screen => "\e[2J", # Clear the screen and move cursor to home.
95
- :cls => "\e[2J", # Clear the screen and move cursor to home.
87
+ :save => "\e[s", # Save current cursor positon.
88
+ :restore => "\e[u", # Restore saved cursor positon.
89
+ :clear_eol => "\e[K", # Clear to the end of the current line.
90
+ :clr => "\e[K", # Clear to the end of the current line.
91
+ :clear_right => "\e[0K", # Clear to the end of the current line.
92
+ :clear_left => "\e[1K", # Clear to the start of the current line.
93
+ :clear_line => "\e[2K", # Clear the entire current line.
94
+ :clear_screen => "\e[2J", # Clear the screen and move cursor to home.
95
+ :cls => "\e[2J", # Clear the screen and move cursor to home.
96
+ :cursor_hide => "\e[?25l", # Hide the cursor.
97
+ :cursor_show => "\e[?25h" # Show the cursor.
96
98
  }
97
99
 
98
100
  end
@@ -1,5 +1,11 @@
1
1
  module ANSI
2
2
 
3
+ # Global variable can be used to prevent ANSI codes
4
+ # from being used in ANSI's methods that do so to string.
5
+ #
6
+ # NOTE: This has no effect on methods that return ANSI codes.
7
+ $ansi = true
8
+
3
9
  if RUBY_PLATFORM =~ /(win32|w32)/
4
10
  begin
5
11
  require 'Win32/Console/ANSI'
@@ -11,18 +17,12 @@ module ANSI
11
17
 
12
18
  require 'ansi/constants'
13
19
 
14
- # Global variialbe can be used to prevent ANSI codes
15
- # from being used in ANSI's methods that do so to string.
16
- #
17
- # NOTE: This has no effect of methods that return ANSI codes.
18
- $ansi = true
19
-
20
20
  # TODO: up, down, right, left, etc could have yielding methods too?
21
21
 
22
22
  # ANSI Codes
23
23
  #
24
24
  # Ansi::Code module makes it very easy to use ANSI codes.
25
- # These are esspecially nice for beautifying shell output.
25
+ # These are especially nice for beautifying shell output.
26
26
  #
27
27
  # Ansi::Code.red + "Hello" + Ansi::Code.blue + "World"
28
28
  # => "\e[31mHello\e[34mWorld"
@@ -32,7 +32,7 @@ module ANSI
32
32
  #
33
33
  # IMPORTANT! Do not mixin Ansi::Code, instead use {ANSI::Mixin}.
34
34
  #
35
- # See {ANSI::Code::CHART} for list of all supported codes.
35
+ # See {ANSI::CHART} for list of all supported codes.
36
36
  #
37
37
  module Code
38
38
  extend self
@@ -56,59 +56,6 @@ module ANSI
56
56
  %w{black red green yellow blue magenta cyan white}
57
57
  end
58
58
 
59
- =begin
60
- styles.each do |style|
61
- module_eval <<-END, __FILE__, __LINE__
62
- def #{style}(string=nil)
63
- if string
64
- return string unless $ansi
65
- #warn "use ANSI block notation for future versions"
66
- return "\#{#{style.upcase}}\#{string}\#{ENDCODE}"
67
- end
68
- if block_given?
69
- return yield unless $ansi
70
- return "\#{#{style.upcase}}\#{yield}\#{ENDCODE}"
71
- end
72
- #{style.upcase}
73
- end
74
- END
75
- end
76
- =end
77
-
78
- =begin
79
- # Dynamically create color methods.
80
-
81
- colors.each do |color|
82
- module_eval <<-END, __FILE__, __LINE__
83
- def #{color}(string=nil)
84
- if string
85
- return string unless $ansi
86
- #warn "use ANSI block notation for future versions"
87
- return "\#{#{color.upcase}}\#{string}\#{ENDCODE}"
88
- end
89
- if block_given?
90
- return yield unless $ansi
91
- return "\#{#{color.upcase}}\#{yield}\#{ENDCODE}"
92
- end
93
- #{color.upcase}
94
- end
95
-
96
- def on_#{color}(string=nil)
97
- if string
98
- return string unless $ansi
99
- #warn "use ANSI block notation for future versions"
100
- return "\#{ON_#{color.upcase}}\#{string}\#{ENDCODE}"
101
- end
102
- if block_given?
103
- return yield unless $ansi
104
- return "\#{ON_#{color.upcase}}\#{yield}\#{ENDCODE}"
105
- end
106
- ON_#{color.upcase}
107
- end
108
- END
109
- end
110
- =end
111
-
112
59
  # Return ANSI code given a list of symbolic names.
113
60
  def [](*codes)
114
61
  code(*codes)
@@ -165,10 +112,10 @@ module ANSI
165
112
  end
166
113
 
167
114
  # TODO: How to deal with position codes when $ansi is false?
168
- # Should we reaise an error or just not push the codes?
115
+ # Should we raise an error or just not push the codes?
169
116
  # For now, we will leave this it as is.
170
117
 
171
- # Like +move+ but returns to original positon after
118
+ # Like +move+ but returns to original position after
172
119
  # yielding the block.
173
120
  def display(line, column=0) #:yield:
174
121
  result = "\e[s"
@@ -188,25 +135,27 @@ module ANSI
188
135
  "\e[#{line.to_i};#{column.to_i}H"
189
136
  end
190
137
 
191
- # Move cursor up a specificed number of spaces.
138
+ # Move cursor up a specified number of spaces.
192
139
  def up(spaces=1)
193
140
  "\e[#{spaces.to_i}A"
194
141
  end
195
142
 
196
- # Move cursor down a specificed number of spaces.
143
+ # Move cursor down a specified number of spaces.
197
144
  def down(spaces=1)
198
145
  "\e[#{spaces.to_i}B"
199
146
  end
200
147
 
201
- # Move cursor left a specificed number of spaces.
148
+ # Move cursor left a specified number of spaces.
202
149
  def left(spaces=1)
203
150
  "\e[#{spaces.to_i}D"
204
151
  end
152
+ alias :back :left
205
153
 
206
- # Move cursor right a specificed number of spaces.
154
+ # Move cursor right a specified number of spaces.
207
155
  def right(spaces=1)
208
156
  "\e[#{spaces.to_i}C"
209
157
  end
158
+ alias :forward :right
210
159
 
211
160
  ##
212
161
  #def position
@@ -228,6 +177,7 @@ module ANSI
228
177
  if block_given?
229
178
  string = yield.to_s
230
179
  else
180
+ # first argument must be the string
231
181
  string = codes.shift.to_s
232
182
  end
233
183
 
@@ -260,7 +210,7 @@ module ANSI
260
210
  # Alias for #ansi method.
261
211
  #
262
212
  # @deprecated
263
- # Here for backward scompatibility.
213
+ # Here for backward compatibility.
264
214
  alias_method :style, :ansi
265
215
 
266
216
  # Alias for #unansi method.
@@ -285,7 +235,7 @@ module ANSI
285
235
  # Also resolves :random and :on_random.
286
236
  #
287
237
  # @param codes [Array<Symbol,Integer]
288
- # Symbols or integers to covnert to ANSI code.
238
+ # Symbols or integers to convert to ANSI code.
289
239
  #
290
240
  # @return [String] ANSI code
291
241
  def code(*codes)
@@ -296,11 +246,18 @@ module ANSI
296
246
  when Integer
297
247
  code
298
248
  when Array
299
- rgb(*code)
300
- when :random
249
+ rgb_code(*code)
250
+ when :random, 'random'
301
251
  random
302
- when :on_random
252
+ when :on_random, 'on_random'
303
253
  random(true)
254
+ when String
255
+ # TODO: code =~ /\d\d\d\d\d\d/ ?
256
+ if code.start_with?('#')
257
+ hex_code(code)
258
+ else
259
+ CHART[code.to_sym]
260
+ end
304
261
  else
305
262
  CHART[code.to_sym]
306
263
  end
@@ -318,30 +275,69 @@ module ANSI
318
275
  (background ? 40 : 30) + rand(8)
319
276
  end
320
277
 
278
+ # Creates an XTerm 256 color escape code from RGB value(s). The
279
+ # RGB value can be three arguments red, green and blue respectively
280
+ # each from 0 to 255, or the RGB value can be a single CSS-style
281
+ # hex string.
282
+ #
283
+ # @param background [Boolean]
284
+ # Use `true` for background color, otherwise foreground color.
285
+ #
286
+ def rgb(*args)
287
+ case args.size
288
+ when 1, 2
289
+ hex, background = *args
290
+ esc = "\e[" + hex_code(hex, background) + "m"
291
+ when 3, 4
292
+ red, green, blue, background = *args
293
+ esc = "\e[" + rgb_code(red, green, blue, background) + "m"
294
+ else
295
+ raise ArgumentError
296
+ end
297
+
298
+ if block_given?
299
+ return yield.to_s unless $ansi
300
+ return "#{esc}#{yield}#{ENDCODE}"
301
+ else
302
+ return esc
303
+ end
304
+ end
305
+
306
+ #private
307
+
321
308
  # Creates an xterm-256 color from rgb value.
322
309
  #
323
310
  # @param background [Boolean]
324
311
  # Use `true` for background color, otherwise foreground color.
325
312
  #
326
- def rgb(red, green, blue, background=false)
327
- "#{background ? 48 : 38};5;#{rgb_value(red, green, blue)}"
313
+ def rgb_code(red, green, blue, background=false)
314
+ "#{background ? 48 : 38};5;#{rgb_256(red, green, blue)}"
328
315
  end
329
316
 
330
- # Creates an xterm-256 color from a CSS-style color string.
331
- def hex(string, background=false)
317
+ # Creates an xterm-256 color code from a CSS-style color string.
318
+ #
319
+ # @param string [String]
320
+ # Hex string in CSS style, .e.g. `#5FA0C2`.
321
+ #
322
+ # @param background [Boolean]
323
+ # Use `true` for background color, otherwise foreground color.
324
+ #
325
+ def hex_code(string, background=false)
332
326
  string.tr!('#','')
333
327
  x = (string.size == 6 ? 2 : 1)
334
328
  r, g, b = [0,1,2].map{ |i| string[i*x,2].to_i(16) }
335
- rgb(r, g, b, background)
329
+ rgb_code(r, g, b, background)
336
330
  end
337
331
 
338
- private
339
-
340
- # Gets closest xterm-256 color.
332
+ # Given red, green and blue values between 0 and 255, this method
333
+ # returns the closest XTerm 256 color value.
334
+ #
341
335
  def rgb_256(r, g, b)
342
- r, g, b = [r, g, b].map{ |c| rgb_valid(c); (6 * (c.to_f / 256.0)).to_i }
336
+ # TODO: what was rgb_valid for?
337
+ #r, g, b = [r, g, b].map{ |c| rgb_valid(c); (6 * (c.to_f / 256.0)).to_i }
338
+ r, g, b = [r, g, b].map{ |c| (6 * (c.to_f / 256.0)).to_i }
343
339
  v = (r * 36 + g * 6 + b + 16).abs
344
- raise ArgumentError, "RGB value outside 0-255 range" if v > 255
340
+ raise ArgumentError, "RGB value is outside 0-255 range -- #{v}" if v > 255
345
341
  v
346
342
  end
347
343