ansi 1.3.0 → 1.4.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.
@@ -1,5 +1,13 @@
1
1
  = ANSI
2
2
 
3
+ {Home}[http://rubyworks.github.com/ansi] |
4
+ {API}[http://rubydoc.info/gems/ansi/frames] |
5
+ {Code}[http://github.com/rubyworks/ansi] |
6
+ {Mail}[http://googlegroups.com/group/rubyworks-mailinglist]
7
+
8
+ {<img src="http://travis-ci.org/rubyworks/ansi.png" />}[http://travis-ci.org/rubyworks/ansi]
9
+
10
+
3
11
  == DESCRIPTION
4
12
 
5
13
  The ANSI project is a collection of ANSI escape code related libraries
@@ -21,12 +29,6 @@ device.
21
29
  * Very Good coverage of standard ANSI codes.
22
30
 
23
31
 
24
- == RESOURCES
25
-
26
- * home: http://rubyworks.github.com/ansi
27
- * code: http://github.com/rubyworks/ansi
28
-
29
-
30
32
  == RELEASE NOTES
31
33
 
32
34
  Please see HISTORY file.
@@ -1,19 +1,4 @@
1
- # ANSI module contains all the ANSI related classes.
2
- module ANSI
3
- # Returns Hash table of project metadata.
4
- def self.meta
5
- @spec ||= (
6
- require 'yaml'
7
- YAML.load(File.new(File.dirname(__FILE__) + '/ansi.yml'))
8
- )
9
- end
10
-
11
- # Check metadata for missing constants.
12
- def self.const_missing(name)
13
- meta[name.to_s.downcase] || super(name)
14
- end
15
- end
16
-
1
+ require 'ansi/version'
17
2
  require 'ansi/core'
18
3
  require 'ansi/code'
19
4
  require 'ansi/bbcode'
@@ -1,47 +1,52 @@
1
- ---
2
- name: ansi
3
- version: 1.3.0
4
- title: ANSI
5
- summary: ANSI codes at your fingertips!
6
- description: The ANSI project is a collection of ANSI escape code related libraries enabling ANSI code based colorization and stylization of output. It is very nice for beautifying shell output.
7
- loadpath:
8
- - lib
9
- manifest: Manifest
10
- requires:
1
+ ---
2
+ source:
3
+ - meta
4
+ authors:
5
+ - name: Thomas Sawyer
6
+ - name: Florian Frank
7
+ copyrights:
8
+ - holder: Rubyworks
9
+ year: '2009'
10
+ replacements: []
11
+ alternatives: []
12
+ requirements:
11
13
  - name: detroit
12
- version: 0+
13
- group:
14
+ groups:
14
15
  - build
16
+ development: true
15
17
  - name: qed
16
- version: 0+
17
- group:
18
+ groups:
18
19
  - test
20
+ development: true
19
21
  - name: lemon
20
- version: 0+
21
- group:
22
+ groups:
22
23
  - test
24
+ development: true
25
+ dependencies: []
23
26
  conflicts: []
24
-
25
- replaces: []
26
-
27
- engine_check: []
28
-
29
- organization: RubyWorks
30
- contact: rubyworks-mailinglist@googlegroups.com
31
- created: 2004-08-01
32
- copyright: Copyright (c) 2009 Thomas Sawyer
33
- licenses:
34
- - Apache 2.0
35
- authors:
36
- - Thomas Sawyer
37
- - Florian Frank
38
- maintainers: []
39
-
40
- resources:
27
+ repositories:
28
+ - uri: git://github.com/rubyworks/ansi.git
29
+ scm: git
30
+ name: upstream
31
+ resources:
41
32
  home: http://rubyworks.github.com/ansi
42
- work: http://github.com/rubyworks/ansi
43
- api: http://rubyworks.github.com/ansi/rdoc
33
+ code: http://github.com/rubyworks/ansi
34
+ docs: http://rubydoc.info/gems/ansi
44
35
  mail: http://groups.google.com/group/rubyworks-mailinglist
45
- repositories:
46
- public: git://github.com/rubyworks/ansi.git
47
- spec_version: 1.0.0
36
+ extra: {}
37
+ load_path:
38
+ - lib
39
+ revision: 0
40
+ created: '2009-08-01'
41
+ summary: ANSI at your fingertips!
42
+ title: ANSI
43
+ version: 1.4.0
44
+ name: ansi
45
+ description: ! 'The ANSI project is a collection of ANSI escape code related libraries
46
+ enabling
47
+
48
+ ANSI code based colorization and stylization of output. It is very nice for
49
+
50
+ beautifying shell output.'
51
+ organization: Rubyworks
52
+ date: '2011-11-05'
@@ -1,5 +1,7 @@
1
1
  module ANSI
2
2
 
3
+ require 'ansi/version'
4
+
3
5
  # Table of codes used throughout the system.
4
6
  #
5
7
  # @see http://en.wikipedia.org/wiki/ANSI_escape_code
@@ -1,6 +1,13 @@
1
1
  module ANSI
2
2
 
3
- require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /(win32|w32)/
3
+ if RUBY_PLATFORM =~ /(win32|w32)/
4
+ begin
5
+ require 'Win32/Console/ANSI'
6
+ rescue
7
+ warn "ansi: 'gem install win32console' to use color on Windows"
8
+ $ansi = false
9
+ end
10
+ end
4
11
 
5
12
  require 'ansi/constants'
6
13
 
@@ -0,0 +1,122 @@
1
+ module ANSI
2
+
3
+ require 'ansi/code'
4
+
5
+ # TODO: split dump method into two parts, the first should create a hex table
6
+ # then the second, #dump method, will print it out.
7
+
8
+ # Create a colorized hex dump of byte string.
9
+ #
10
+ # Output looks something like the following, but colorized.
11
+ #
12
+ # 000352c0: ed 33 8c 85 6e cc f6 f7 72 79 1c e3 3a b4 c2 c6 |.3..n...ry..:...|
13
+ # 000352d0: c8 8d d6 ee 3e 68 a1 a5 ae b2 b7 97 a4 1d 5f a7 |....>h........_.|
14
+ # 000352e0: d8 7d 28 db f6 8a e7 8a 7b 8d 0b bd 35 7d 25 3c |.}(.....{...5}%<|
15
+ # 000352f0: 8b 3c c8 9d ec 04 85 54 92 a0 f7 a8 ed cf 05 7d |.<.....T.......}|
16
+ # 00035300: b5 e3 9e 35 f0 79 9f 51 74 e3 60 ee 0f 03 8e 3f |...5.y.Qt.`....?|
17
+ # 00035310: 05 5b 91 87 e6 48 48 ee a3 77 ae ad 5e 2a 56 a2 |.[...HH..w..^*V.|
18
+ # 00035320: b6 96 86 f3 3c 92 b3 c8 62 4a 6f 96 10 5c 9c bb |....<...bJo..\..|
19
+ #
20
+ # In the future, we will make the colorization more customizable and
21
+ # allow the groupings to be selectable at 2, 4, 8 or 16.
22
+ #
23
+ class HexDump
24
+
25
+ # Printable ASCII codes.
26
+ ASCII_PRINTABLE = (33..126)
27
+
28
+ #
29
+ def initialize(options={})
30
+ @offset = 0
31
+
32
+ options.each do |k,v|
33
+ __send__("#{k}=", v)
34
+ end
35
+
36
+ @color = true if color.nil?
37
+ end
38
+
39
+ # Use color?
40
+ attr_accessor :color
41
+
42
+ # Show index?
43
+ attr_accessor :index
44
+
45
+ # Offset byte count.
46
+ attr_accessor :offset
47
+
48
+ # Dump data string as colorized hex table.
49
+ #
50
+ # @param data [String]
51
+ # String to convert to hex and display.
52
+ #
53
+ def dump(data)
54
+ lines = data.to_s.scan(/.{1,16}/m)
55
+ max_offset = (offset + data.size) / 256 #16 * 16
56
+ max_offset_width = max_offset.to_s.size + 1
57
+ max_hex_width = 49 #3 * 16 + 1
58
+
59
+ out = template()
60
+ off = offset()
61
+
62
+ if index?
63
+ puts((' ' * max_offset_width) + " 0 1 2 3 4 5 6 7 8 9 A B C D E F\n")
64
+ end
65
+
66
+ lines.each_with_index do |line, n|
67
+ offset = off + n * 16
68
+ bytes = line.unpack("C*")
69
+ hex = bytes.map{ |c| "%0.2x" % c }.insert(8, '').join(' ')
70
+
71
+ plain = bytes.map do |c|
72
+ if ASCII_PRINTABLE.include?(c)
73
+ c = c.chr
74
+ else
75
+ color ? Code::WHITE + Code::STRIKE + '.' + Code::CLEAR : '.'
76
+ end
77
+ end.join('')
78
+
79
+ fill = [offset.to_s.rjust(max_offset_width), hex.ljust(max_hex_width), plain]
80
+
81
+ puts(out % fill)
82
+ end
83
+ end
84
+
85
+ # Hex dump a random string.
86
+ #
87
+ def dump_random(size=64)
88
+ data = (0..size).map{ rand(255).chr }.join('')
89
+ dump(data)
90
+ end
91
+
92
+ #
93
+ def index?
94
+ @index
95
+ end
96
+
97
+ private
98
+
99
+ # Hex dump line template.
100
+ #
101
+ # @return [String] hex dump line template
102
+ def template
103
+ if color
104
+ Code::CYAN +
105
+ "%s: " +
106
+ Code::YELLOW +
107
+ "%s " +
108
+ Code::BLUE +
109
+ "|" +
110
+ Code::CLEAR +
111
+ "%s" +
112
+ Code::BLUE +
113
+ "|" +
114
+ Code::CLEAR
115
+ else
116
+ "%s: %s |%s|"
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ end
@@ -1,4 +1,4 @@
1
- require 'ansi'
1
+ require 'ansi/core'
2
2
  require 'ansi/terminal'
3
3
 
4
4
  module ANSI
@@ -70,7 +70,8 @@ module ANSI
70
70
  body_row = []
71
71
  row.each_with_index do |cell, c|
72
72
  t = cell_template(max[c])
73
- body_row << (t % cell.to_s).ansi(*ansi_formating(cell, c, r))
73
+ s = t % cell.to_s
74
+ body_row << apply_format(s, cell, c, r)
74
75
  end
75
76
  body << "| " + body_row.join(' | ') + " |"
76
77
  end
@@ -84,7 +85,7 @@ module ANSI
84
85
  "#{top}\n#{body}\n#{bot}\n"
85
86
  end
86
87
 
87
- private
88
+ private
88
89
 
89
90
  # TODO: look at the lines and figure out how many columns will fit
90
91
  def fit_width
@@ -92,12 +93,15 @@ module ANSI
92
93
  ((width.to_f / column_size) - (padding + 3)).to_i
93
94
  end
94
95
 
96
+ # Calculate the maximun column sizes.
95
97
  #
98
+ # @return [Array] maximum size for each column
96
99
  def max_columns(fit=false)
97
100
  max = Array.new(column_size, 0)
98
101
  table.each do |row|
99
102
  row.each_with_index do |col, index|
100
103
  col = col.to_s
104
+ col = col.unansi
101
105
  if fit
102
106
  max[index] = [max[index], col.size, fit_width].max
103
107
  else
@@ -108,7 +112,9 @@ module ANSI
108
112
  max
109
113
  end
110
114
 
111
- # Number of columns.
115
+ # Number of columns based on the first row of table.
116
+ #
117
+ # @return [Integer] number of columns
112
118
  def column_size
113
119
  table.first.size
114
120
  end
@@ -123,6 +129,30 @@ module ANSI
123
129
  end
124
130
  end
125
131
 
132
+ # TODO: make more efficient
133
+ def dividing_line
134
+ tmp = max_columns(fit).map{ |m| "%#{m}s" }.join(" | ")
135
+ tmp = "| #{tmp} |"
136
+ lin = (tmp % (['-'] * column_size)).gsub(/[^\|]/, '-').gsub('|', '+')
137
+ end
138
+
139
+ #def dividing_line_top
140
+ # dividing_line.gsub('+', '.')
141
+ #end
142
+
143
+ #def dividing_line_bottom
144
+ # dividing_line.gsub('+', "'")
145
+ #end
146
+
147
+ #
148
+ def apply_format(str, cell, col, row)
149
+ if @format
150
+ str.ansi(*ansi_formating(cell, col, row))
151
+ else
152
+ str
153
+ end
154
+ end
155
+
126
156
  #
127
157
  def ansi_formating(cell, col, row)
128
158
  if @format
@@ -142,21 +172,6 @@ module ANSI
142
172
  [f].flatten.compact
143
173
  end
144
174
 
145
- # TODO: make more efficient
146
- def dividing_line
147
- tmp = max_columns(fit).map{ |m| "%#{m}s" }.join(" | ")
148
- tmp = "| #{tmp} |"
149
- lin = (tmp % (['-'] * column_size)).gsub(/[^\|]/, '-').gsub('|', '+')
150
- end
151
-
152
- #def dividing_line_top
153
- # dividing_line.gsub('+', '.')
154
- #end
155
-
156
- #def dividing_line_bottom
157
- # dividing_line.gsub('+', "'")
158
- #end
159
-
160
175
  end
161
176
 
162
177
  end
@@ -0,0 +1,16 @@
1
+ # ANSI module contains all the ANSI related classes.
2
+ module ANSI
3
+ # Returns Hash table of project metadata.
4
+ def self.meta
5
+ @spec ||= (
6
+ require 'yaml'
7
+ YAML.load(File.new(File.dirname(__FILE__) + '/../ansi.yml'))
8
+ )
9
+ end
10
+
11
+ # Check metadata for missing constants.
12
+ def self.const_missing(name)
13
+ meta[name.to_s.downcase] || super(name)
14
+ end
15
+ end
16
+
@@ -1,3 +1,4 @@
1
+ require 'test_helper'
1
2
  require 'ansi/code'
2
3
 
3
4
  testcase ANSI::Code do
@@ -1,27 +1,35 @@
1
+ require 'test_helper'
1
2
  require 'ansi/bbcode'
2
3
 
3
4
  testcase ANSI::BBCode do
4
5
 
5
- meta :bbcode_to_ansi do
6
- str = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
7
- out = "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
8
- out.assert == ANSI::BBCode.bbcode_to_ansi(str)
6
+ class_method :bbcode_to_ansi do
7
+ test do
8
+ str = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
9
+ out = "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
10
+ out.assert == ANSI::BBCode.bbcode_to_ansi(str)
11
+ end
9
12
  end
10
13
 
11
- meta :bbcode_to_html do
12
- str = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
13
- out = "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
14
- out.assert == ANSI::BBCode.bbcode_to_html(str)
14
+ class_method :bbcode_to_html do
15
+ test do
16
+ str = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
17
+ out = "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
18
+ out.assert == ANSI::BBCode.bbcode_to_html(str)
19
+ end
15
20
  end
16
21
 
17
- meta :ansi_to_html do
18
- str = "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n" +
19
- "this is a line without any ansi code\n" +
20
- "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
21
- out = "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n" +
22
- "this is a line without any ansi code<br />\n" +
23
- "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
24
- out.assert == ANSI::BBCode.ansi_to_html(str)
22
+ class_method :ansi_to_html do
23
+ test do
24
+ str = "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n" +
25
+ "this is a line without any ansi code\n" +
26
+ "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
27
+ out = "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n" +
28
+ "this is a line without any ansi code<br />\n" +
29
+ "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
30
+ out.assert == ANSI::BBCode.ansi_to_html(str)
31
+ end
25
32
  end
26
33
 
27
34
  end
35
+