ansi 1.2.4 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.ruby +1 -1
- data/HISTORY.rdoc +21 -9
- data/README.rdoc +4 -4
- data/lib/ansi.rb +10 -3
- data/lib/ansi.yml +1 -1
- data/lib/ansi/columns.rb +8 -7
- data/lib/ansi/diff.rb +113 -0
- data/qed/07_columns.rdoc +6 -6
- data/qed/09_diff.rb +47 -0
- metadata +7 -5
data/.ruby
CHANGED
data/HISTORY.rdoc
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
= RELEASE HISTORY
|
2
2
|
|
3
|
-
== 1.2.
|
3
|
+
== 1.2.5 // 2011-05-03
|
4
|
+
|
5
|
+
This release introduces a preliminary rendition of a Diff class
|
6
|
+
for getting colorized comparisons of strings and other objects.
|
7
|
+
It's not officially supported yet, so this is only a point release.
|
8
|
+
|
9
|
+
Changes:
|
10
|
+
|
11
|
+
* Added Diff class for colorized comparisons.
|
12
|
+
* Fixed minor issue with Columns format block; col comes before row.
|
13
|
+
|
14
|
+
|
15
|
+
== 1.2.4 // 2011-04-29
|
4
16
|
|
5
17
|
This release improves to the ANSI::Columns class. In particular the
|
6
|
-
layout is more
|
18
|
+
layout is more consistent with intended functionality.
|
7
19
|
|
8
20
|
Changes:
|
9
21
|
|
10
|
-
* Improved ANSI::Columns to give more
|
22
|
+
* Improved ANSI::Columns to give more consistent output.
|
11
23
|
* ANSI::Columns#to_s can override number of columns.
|
12
24
|
* ANSI::Columns can take a String or Array list.
|
13
25
|
|
@@ -37,12 +49,12 @@ Changes:
|
|
37
49
|
|
38
50
|
* Remove string argument warnings.
|
39
51
|
* Add String#unansi and String#unansi!
|
40
|
-
* Add
|
52
|
+
* Add ANSI::Mixin#display.
|
41
53
|
|
42
54
|
|
43
55
|
== 1.2.1 // 2010-05-10
|
44
56
|
|
45
|
-
This release was simply a quick fix to remove the
|
57
|
+
This release was simply a quick fix to remove the incorrect embedded
|
46
58
|
version number, until it gets fixed.
|
47
59
|
|
48
60
|
|
@@ -51,7 +63,7 @@ version number, until it gets fixed.
|
|
51
63
|
This release entails numerous improvements. First and foremost
|
52
64
|
the Code module is transitioning to a block interface only
|
53
65
|
and phasing out the string argument interface. Admittedly this
|
54
|
-
is mildly
|
66
|
+
is mildly unconventional, but it allows the arguments to be used
|
55
67
|
as options with common defaults more elegantly.
|
56
68
|
|
57
69
|
Another important change is that ANSI::Code no longer provides
|
@@ -59,10 +71,10 @@ String extension methods when included. For this use the new
|
|
59
71
|
ANSI::Mixin.
|
60
72
|
|
61
73
|
Other improvements include a String extension, #ansi, added to
|
62
|
-
code.rb, which makes it even
|
74
|
+
code.rb, which makes it even easier to apply ANSI codes to strings.
|
63
75
|
Also, the ANSI::String class has been fixed (a few bugs crept
|
64
76
|
it with the last release) and continues to improve. On top of all
|
65
|
-
this testing has
|
77
|
+
this testing has substantially improved thanks to QED.
|
66
78
|
|
67
79
|
Changes:
|
68
80
|
|
@@ -75,7 +87,7 @@ Changes:
|
|
75
87
|
== 1.1.0 // 2009-10-04
|
76
88
|
|
77
89
|
This release is the first toward making the ANSI library
|
78
|
-
more widely
|
90
|
+
more widely usable.
|
79
91
|
|
80
92
|
Changes:
|
81
93
|
|
data/README.rdoc
CHANGED
@@ -35,12 +35,12 @@ Please see HISTORY file.
|
|
35
35
|
== SYNOPSIS
|
36
36
|
|
37
37
|
There are a number of modules and classes provided by the ANSI
|
38
|
-
package. To get a good
|
38
|
+
package. To get a good understanding of them it is best to pursue
|
39
39
|
the QED documents[http://github.com/rubyworks/ansi/tree/master/qed/]
|
40
40
|
or the API documentation[http://rubyworks.github.com/ansi/rdoc/].
|
41
41
|
|
42
42
|
At the heart of all the provided libraries lies the ANSI::Code module
|
43
|
-
which defines ANSI codes as constants and methods. For
|
43
|
+
which defines ANSI codes as constants and methods. For example:
|
44
44
|
|
45
45
|
require 'ansi/code'
|
46
46
|
|
@@ -54,7 +54,7 @@ Or in block form.
|
|
54
54
|
red{ "Hello" } + blue{ "World" }
|
55
55
|
=> "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
56
56
|
|
57
|
-
Rather than include +ANSI::Code+, these
|
57
|
+
Rather than include +ANSI::Code+, these methods can also be called as module
|
58
58
|
methods. Either as <code>ANSI::Code.red</code> or just <code>ANSI.red</code>.
|
59
59
|
|
60
60
|
The methods defined by this module are used throughout the rest of
|
@@ -81,7 +81,7 @@ Windows users use 'ruby setup.rb all'.
|
|
81
81
|
|
82
82
|
Copyright (c) 2004 Thomas Sawyer
|
83
83
|
|
84
|
-
This program is
|
84
|
+
This program is distributed under the terms of the Apache 2 license.
|
85
85
|
|
86
86
|
See LICENSE file for details.
|
87
87
|
|
data/lib/ansi.rb
CHANGED
@@ -1,20 +1,27 @@
|
|
1
1
|
# ANSI module contains all the ANSI related classes.
|
2
2
|
module ANSI
|
3
|
-
#
|
3
|
+
# Returns Hash table of project metadata.
|
4
4
|
def self.meta
|
5
5
|
@spec ||= (
|
6
6
|
require 'yaml'
|
7
7
|
YAML.load(File.new(File.dirname(__FILE__) + '/ansi.yml'))
|
8
8
|
)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
|
+
# Check metadata for missing constants.
|
11
12
|
def self.const_missing(name)
|
12
13
|
meta[name.to_s.downcase] || super(name)
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
16
17
|
require 'ansi/code'
|
18
|
+
require 'ansi/bbcode'
|
19
|
+
require 'ansi/columns'
|
20
|
+
require 'ansi/diff'
|
17
21
|
require 'ansi/logger'
|
22
|
+
require 'ansi/mixin'
|
18
23
|
require 'ansi/progressbar'
|
19
24
|
require 'ansi/string'
|
20
|
-
require 'ansi/
|
25
|
+
require 'ansi/table'
|
26
|
+
require 'ansi/terminal'
|
27
|
+
|
data/lib/ansi.yml
CHANGED
data/lib/ansi/columns.rb
CHANGED
@@ -74,20 +74,21 @@ module ANSI
|
|
74
74
|
width = Terminal.terminal_width
|
75
75
|
columns = (width / (max + padding)).to_i
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
|
+
rows = []
|
78
79
|
mod = (count / columns.to_f).to_i
|
79
80
|
mod += 1 if count % columns != 0
|
80
81
|
|
81
82
|
lines.each_with_index do |line, index|
|
82
|
-
(
|
83
|
+
(rows[index % mod] ||=[]) << line.strip
|
83
84
|
end
|
84
85
|
|
85
86
|
pad = " " * padding
|
86
87
|
tmp = template(max, pad)
|
87
88
|
str = ""
|
88
|
-
|
89
|
-
row.each_with_index do |cell,
|
90
|
-
ansi_codes = ansi_formating(cell,
|
89
|
+
rows.each_with_index do |row, ri|
|
90
|
+
row.each_with_index do |cell, ci|
|
91
|
+
ansi_codes = ansi_formating(cell, ci, ri)
|
91
92
|
if ansi_codes.empty?
|
92
93
|
str << (tmp % cell)
|
93
94
|
else
|
@@ -121,9 +122,9 @@ module ANSI
|
|
121
122
|
when 1
|
122
123
|
f = @format[cell]
|
123
124
|
when 2
|
124
|
-
f = @format[
|
125
|
+
f = @format[col, row]
|
125
126
|
else
|
126
|
-
f = @format[cell,
|
127
|
+
f = @format[cell, col, row]
|
127
128
|
end
|
128
129
|
else
|
129
130
|
f = nil
|
data/lib/ansi/diff.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'ansi/code'
|
2
|
+
|
3
|
+
module ANSI
|
4
|
+
|
5
|
+
# Diff can produced a colorized difference of two string or objects.
|
6
|
+
#
|
7
|
+
# IMPORTANT! This class is still a very much a work in progress.
|
8
|
+
class Diff
|
9
|
+
|
10
|
+
#
|
11
|
+
def initialize(object1, object2, options={})
|
12
|
+
@object1 = convert(object1)
|
13
|
+
@object2 = convert(object2)
|
14
|
+
|
15
|
+
@diff1, @diff2 = diff_string(@object1, @object2)
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
def diff1
|
20
|
+
@diff1
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
def diff2
|
25
|
+
@diff2
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
def to_s
|
30
|
+
"#{@diff1}\n#{@diff2}"
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Take two plain strings and produce colorized
|
36
|
+
# versions of each highlighting their differences.
|
37
|
+
#
|
38
|
+
# TODO: I am sure there are better ways to do this,
|
39
|
+
# but for now this suffices.
|
40
|
+
def diff_string(str1, str2)
|
41
|
+
i1, i2 = 0, 0
|
42
|
+
m1, m2 = nil, nil
|
43
|
+
s1, s2 = "", ""
|
44
|
+
t1, t2 = "", ""
|
45
|
+
c = 0
|
46
|
+
|
47
|
+
loop do
|
48
|
+
if str1[i1,1] == str2[i2,1]
|
49
|
+
s1 << red(t1); t1 = "" unless t1 == ""
|
50
|
+
s2 << red(t2); t2 = "" unless t2 == ""
|
51
|
+
s1 << str1[i1,1].to_s
|
52
|
+
s2 << str2[i2,1].to_s
|
53
|
+
i1 += 1; i2 += 1
|
54
|
+
#m1 += 1; m2 += 1
|
55
|
+
else
|
56
|
+
if m1 && str1[m1,1] == str2[i2,1]
|
57
|
+
s2 << color1(t2)
|
58
|
+
t1, t2 = "", ""
|
59
|
+
i1 = m1
|
60
|
+
m1, m2 = nil, nil
|
61
|
+
elsif m2 && str1[i1,1] == str2[m2,1]
|
62
|
+
s1 << color2(t1)
|
63
|
+
t1, t2 = "", ""
|
64
|
+
i2 = m2
|
65
|
+
m1, m2 = nil, nil
|
66
|
+
else
|
67
|
+
t1 << str1[i1,1].to_s
|
68
|
+
t2 << str2[i2,1].to_s
|
69
|
+
m1, m2 = i1, i2 if m1 == nil
|
70
|
+
i1 += 1; i2 += 1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
break if i1 >= str1.size && i2 >= str2.size
|
74
|
+
end
|
75
|
+
s1 << red(t1); t1 = "" unless t1 == ""
|
76
|
+
s2 << red(t2); t2 = "" unless t2 == ""
|
77
|
+
#s1 << ANSI::Code::CLEAR
|
78
|
+
#s2 << ANSI::Code::CLEAR
|
79
|
+
|
80
|
+
return s1, s2
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
def red(str)
|
85
|
+
ANSI.color(:red){ str }
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
def color1(str)
|
90
|
+
ANSI.color(:blue){ str }
|
91
|
+
end
|
92
|
+
|
93
|
+
#
|
94
|
+
def color2(str)
|
95
|
+
ANSI.color(:green){ str }
|
96
|
+
end
|
97
|
+
|
98
|
+
# Ensure the object of comparison is a string. If +object+ is not
|
99
|
+
# an instance of String then it wll be converted to one by calling
|
100
|
+
# either #to_str, if the object responds to it, or #inspect.
|
101
|
+
def convert(object)
|
102
|
+
if String === object
|
103
|
+
object
|
104
|
+
elsif object.respond_to?(:to_str)
|
105
|
+
object.to_str
|
106
|
+
else
|
107
|
+
object.inspect
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
data/qed/07_columns.rdoc
CHANGED
@@ -18,7 +18,7 @@ The output will be:
|
|
18
18
|
b e h k
|
19
19
|
c f i l
|
20
20
|
|
21
|
-
Besides
|
21
|
+
Besides an array of elements, Columns.new can take a string in which
|
22
22
|
the elements are divided by newlines characters. The default column
|
23
23
|
size can also be given to the initializer.
|
24
24
|
|
@@ -38,7 +38,7 @@ as a best fit for the current terminal window.
|
|
38
38
|
|
39
39
|
== Padding
|
40
40
|
|
41
|
-
Columns can
|
41
|
+
Columns can adjust the padding between cells.
|
42
42
|
|
43
43
|
list = %w{a b c d e f g h i j k l}
|
44
44
|
|
@@ -70,14 +70,14 @@ The output will be:
|
|
70
70
|
|
71
71
|
== Format
|
72
72
|
|
73
|
-
Lastly, columns can be
|
74
|
-
a
|
75
|
-
content, the
|
73
|
+
Lastly, columns can be augmented with ANSI codes. This is done through
|
74
|
+
a formatting block. The block can take up to three parameters, the cell
|
75
|
+
content, the column and row numbers, or the cell and the column and row
|
76
76
|
numbers.
|
77
77
|
|
78
78
|
list = %w{a b c d e f g h i j k l}
|
79
79
|
|
80
|
-
columns = ANSI::Columns.new(list){ |r
|
80
|
+
columns = ANSI::Columns.new(list){ |c,r| r % 2 == 0 ? :red : :blue }
|
81
81
|
|
82
82
|
out = columns.to_s(4)
|
83
83
|
|
data/qed/09_diff.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= ANSI::Diff
|
2
|
+
|
3
|
+
require 'ansi/diff'
|
4
|
+
|
5
|
+
a = 'abcYefg'
|
6
|
+
b = 'abcXefg'
|
7
|
+
|
8
|
+
diff = ANSI::Diff.new(a,b)
|
9
|
+
|
10
|
+
puts diff.to_s
|
11
|
+
|
12
|
+
Try another.
|
13
|
+
|
14
|
+
a = 'abc'
|
15
|
+
b = 'abcdef'
|
16
|
+
|
17
|
+
diff = ANSI::Diff.new(a,b)
|
18
|
+
|
19
|
+
puts diff.to_s
|
20
|
+
|
21
|
+
And another.
|
22
|
+
|
23
|
+
a = 'abcXXXghi'
|
24
|
+
b = 'abcdefghi'
|
25
|
+
|
26
|
+
diff = ANSI::Diff.new(a,b)
|
27
|
+
|
28
|
+
puts diff.to_s
|
29
|
+
|
30
|
+
And another.
|
31
|
+
|
32
|
+
a = 'abcXXXdefghi'
|
33
|
+
b = 'abcdefghi'
|
34
|
+
|
35
|
+
diff = ANSI::Diff.new(a,b)
|
36
|
+
|
37
|
+
puts diff.to_s
|
38
|
+
|
39
|
+
Comparison that is mostly different.
|
40
|
+
|
41
|
+
a = 'abcpppz123'
|
42
|
+
b = 'abcxyzzz43'
|
43
|
+
|
44
|
+
diff = ANSI::Diff.new(a,b)
|
45
|
+
|
46
|
+
puts diff.to_s
|
47
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ansi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 5
|
10
|
+
version: 1.2.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thomas Sawyer
|
@@ -16,11 +16,11 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-05-
|
19
|
+
date: 2011-05-05 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
23
|
-
name:
|
23
|
+
name: redline
|
24
24
|
prerelease: false
|
25
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
26
|
none: false
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- lib/ansi/bbcode.rb
|
61
61
|
- lib/ansi/code.rb
|
62
62
|
- lib/ansi/columns.rb
|
63
|
+
- lib/ansi/diff.rb
|
63
64
|
- lib/ansi/logger.rb
|
64
65
|
- lib/ansi/mixin.rb
|
65
66
|
- lib/ansi/progressbar.rb
|
@@ -80,6 +81,7 @@ files:
|
|
80
81
|
- qed/06_string.rdoc
|
81
82
|
- qed/07_columns.rdoc
|
82
83
|
- qed/08_table.rdoc
|
84
|
+
- qed/09_diff.rb
|
83
85
|
- qed/applique/output.rb
|
84
86
|
- test/case_ansicode.rb
|
85
87
|
- test/case_bbcode.rb
|