ansi 1.2.3 → 1.2.4
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/.ruby +24 -38
- data/{HISTORY → HISTORY.rdoc} +13 -1
- data/{NOTICE → NOTICE.rdoc} +0 -0
- data/README.rdoc +8 -6
- data/Rakefile +4 -0
- data/lib/ansi.rb +12 -2
- data/lib/ansi.yml +44 -0
- data/lib/ansi/columns.rb +56 -31
- data/qed/01_ansicode.rdoc +56 -0
- data/qed/02_bbcode.rdoc +24 -0
- data/qed/03_logger.rdoc +31 -0
- data/qed/04_progressbar.rdoc +63 -0
- data/qed/05_mixin.rdoc +37 -0
- data/qed/06_string.rdoc +56 -0
- data/qed/07_columns.rdoc +90 -0
- data/qed/08_table.rdoc +28 -0
- data/qed/applique/output.rb +5 -0
- metadata +18 -10
- data/PROFILE +0 -28
- data/VERSION +0 -1
- data/lib/ansi/version.rb +0 -4
data/.ruby
CHANGED
@@ -1,8 +1,26 @@
|
|
1
1
|
---
|
2
|
-
name: ansi
|
3
2
|
spec_version: 1.0.0
|
3
|
+
replaces: []
|
4
|
+
|
5
|
+
loadpath:
|
6
|
+
- lib
|
7
|
+
name: ansi
|
8
|
+
repositories: {}
|
9
|
+
|
10
|
+
conflicts: []
|
11
|
+
|
12
|
+
engine_check: []
|
13
|
+
|
4
14
|
title: ANSI
|
5
15
|
contact: rubyworks-mailinglist@googlegroups.com
|
16
|
+
resources:
|
17
|
+
repository: git://github.com/rubyworks/ansi.git
|
18
|
+
api: http://rubyworks.github.com/ansi/rdoc
|
19
|
+
mail: http://groups.google.com/group/rubyworks-mailinglist
|
20
|
+
home: http://rubyworks.github.com/ansi
|
21
|
+
work: http://github.com/rubyworks/ansi
|
22
|
+
maintainers: []
|
23
|
+
|
6
24
|
requires:
|
7
25
|
- group:
|
8
26
|
- build
|
@@ -12,47 +30,15 @@ requires:
|
|
12
30
|
- test
|
13
31
|
name: ko
|
14
32
|
version: 0+
|
15
|
-
|
16
|
-
|
17
|
-
api: http://rubyworks.github.com/ansi/rdoc
|
18
|
-
mail: http://groups.google.com/group/rubyworks-mailinglist
|
19
|
-
home: http://rubyworks.github.com/ansi
|
20
|
-
work: http://github.com/rubyworks/ansi
|
21
|
-
manifest:
|
22
|
-
- .ruby
|
23
|
-
- lib/ansi/bbcode.rb
|
24
|
-
- lib/ansi/code.rb
|
25
|
-
- lib/ansi/columns.rb
|
26
|
-
- lib/ansi/logger.rb
|
27
|
-
- lib/ansi/mixin.rb
|
28
|
-
- lib/ansi/progressbar.rb
|
29
|
-
- lib/ansi/string.rb
|
30
|
-
- lib/ansi/table.rb
|
31
|
-
- lib/ansi/terminal/curses.rb
|
32
|
-
- lib/ansi/terminal/stty.rb
|
33
|
-
- lib/ansi/terminal/termios.rb
|
34
|
-
- lib/ansi/terminal/win32.rb
|
35
|
-
- lib/ansi/terminal.rb
|
36
|
-
- lib/ansi/version.rb
|
37
|
-
- lib/ansi.rb
|
38
|
-
- test/case_ansicode.rb
|
39
|
-
- test/case_bbcode.rb
|
40
|
-
- test/case_mixin.rb
|
41
|
-
- test/case_progressbar.rb
|
42
|
-
- PROFILE
|
43
|
-
- LICENSE
|
44
|
-
- README.rdoc
|
45
|
-
- HISTORY
|
46
|
-
- NOTICE
|
47
|
-
- VERSION
|
48
|
-
version: 1.2.3
|
33
|
+
manifest: MANIFEST
|
34
|
+
version: 1.2.4
|
49
35
|
licenses:
|
50
36
|
- Apache 2.0
|
51
37
|
copyright: Copyright (c) 2009 Thomas Sawyer
|
52
|
-
description: ANSI codes at your fingertips!
|
53
|
-
summary: ANSI codes at your fingertips!
|
54
|
-
organization: RubyWorks
|
55
38
|
authors:
|
56
39
|
- Thomas Sawyer
|
57
40
|
- Florian Frank
|
41
|
+
organization: RubyWorks
|
42
|
+
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.
|
43
|
+
summary: ANSI codes at your fingertips!
|
58
44
|
created: 2004-08-01
|
data/{HISTORY → HISTORY.rdoc}
RENAMED
@@ -1,6 +1,18 @@
|
|
1
1
|
= RELEASE HISTORY
|
2
2
|
|
3
|
-
== 1.2.
|
3
|
+
== 1.2.4 // 2011-04-09
|
4
|
+
|
5
|
+
This release improves to the ANSI::Columns class. In particular the
|
6
|
+
layout is more consitent with intended functionality.
|
7
|
+
|
8
|
+
Changes:
|
9
|
+
|
10
|
+
* Improved ANSI::Columns to give more consitant output.
|
11
|
+
* ANSI::Columns#to_s can override number of columns.
|
12
|
+
* ANSI::Columns can take a String or Array list.
|
13
|
+
|
14
|
+
|
15
|
+
== 1.2.3 // 2011-04-08
|
4
16
|
|
5
17
|
Minor release to add #clear method to ProgressBar and provide bug
|
6
18
|
fix to BBCode.ansi_to_bbcode. Big thanks goes to Junegunn Choi
|
data/{NOTICE → NOTICE.rdoc}
RENAMED
File without changes
|
data/README.rdoc
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
= ANSI
|
2
2
|
|
3
|
-
* http://rubyworks.github.com/ansi
|
4
|
-
* http://github.com/rubyworks/ansi
|
5
|
-
|
6
|
-
|
7
3
|
== DESCRIPTION
|
8
4
|
|
9
5
|
The ANSI project is a collection of ANSI escape code related libraries
|
@@ -11,13 +7,13 @@ enabling ANSI code based colorization and stylization of output.
|
|
11
7
|
It is very nice for beautifying shell output.
|
12
8
|
|
13
9
|
This collection is based on a set of scripts spun-off from
|
14
|
-
Ruby Facets.
|
10
|
+
Ruby Facets. Included are Code (used to be ANSICode), Logger,
|
15
11
|
ProgressBar and String. In addition the library includes
|
16
12
|
Terminal which provides information about the current output
|
17
13
|
device.
|
18
14
|
|
19
15
|
|
20
|
-
== FEATURES
|
16
|
+
== FEATURES
|
21
17
|
|
22
18
|
* ANSI::Code provides ANSI codes as module functions.
|
23
19
|
* String#ansi makes common usage very easy and elegant.
|
@@ -25,6 +21,12 @@ device.
|
|
25
21
|
* Very Good coverage of standard ANSI codes.
|
26
22
|
|
27
23
|
|
24
|
+
== RESOURCES
|
25
|
+
|
26
|
+
* home: http://rubyworks.github.com/ansi
|
27
|
+
* code: http://github.com/rubyworks/ansi
|
28
|
+
|
29
|
+
|
28
30
|
== RELEASE NOTES
|
29
31
|
|
30
32
|
Please see HISTORY file.
|
data/Rakefile
ADDED
data/lib/ansi.rb
CHANGED
@@ -1,10 +1,20 @@
|
|
1
1
|
# ANSI module contains all the ANSI related classes.
|
2
2
|
module ANSI
|
3
|
+
#
|
4
|
+
def self.meta
|
5
|
+
@spec ||= (
|
6
|
+
require 'yaml'
|
7
|
+
YAML.load(File.new(File.dirname(__FILE__) + '/ansi.yml'))
|
8
|
+
)
|
9
|
+
end
|
10
|
+
#
|
11
|
+
def self.const_missing(name)
|
12
|
+
meta[name.to_s.downcase] || super(name)
|
13
|
+
end
|
3
14
|
end
|
4
15
|
|
5
|
-
require 'ansi/version'
|
6
16
|
require 'ansi/code'
|
7
17
|
require 'ansi/logger'
|
8
18
|
require 'ansi/progressbar'
|
9
19
|
require 'ansi/string'
|
10
|
-
|
20
|
+
require 'ansi/columns'
|
data/lib/ansi.yml
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
---
|
2
|
+
spec_version: 1.0.0
|
3
|
+
replaces: []
|
4
|
+
|
5
|
+
loadpath:
|
6
|
+
- lib
|
7
|
+
name: ansi
|
8
|
+
repositories: {}
|
9
|
+
|
10
|
+
conflicts: []
|
11
|
+
|
12
|
+
engine_check: []
|
13
|
+
|
14
|
+
title: ANSI
|
15
|
+
contact: rubyworks-mailinglist@googlegroups.com
|
16
|
+
resources:
|
17
|
+
repository: git://github.com/rubyworks/ansi.git
|
18
|
+
api: http://rubyworks.github.com/ansi/rdoc
|
19
|
+
mail: http://groups.google.com/group/rubyworks-mailinglist
|
20
|
+
home: http://rubyworks.github.com/ansi
|
21
|
+
work: http://github.com/rubyworks/ansi
|
22
|
+
maintainers: []
|
23
|
+
|
24
|
+
requires:
|
25
|
+
- group:
|
26
|
+
- build
|
27
|
+
name: syckle
|
28
|
+
version: 0+
|
29
|
+
- group:
|
30
|
+
- test
|
31
|
+
name: ko
|
32
|
+
version: 0+
|
33
|
+
manifest: MANIFEST
|
34
|
+
version: 1.2.4
|
35
|
+
licenses:
|
36
|
+
- Apache 2.0
|
37
|
+
copyright: Copyright (c) 2009 Thomas Sawyer
|
38
|
+
authors:
|
39
|
+
- Thomas Sawyer
|
40
|
+
- Florian Frank
|
41
|
+
organization: RubyWorks
|
42
|
+
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.
|
43
|
+
summary: ANSI codes at your fingertips!
|
44
|
+
created: 2004-08-01
|
data/lib/ansi/columns.rb
CHANGED
@@ -7,77 +7,102 @@ module ANSI
|
|
7
7
|
|
8
8
|
# Create a column-based layout.
|
9
9
|
#
|
10
|
-
#
|
10
|
+
# list - Multiline String or Array of strings to columnize
|
11
11
|
#
|
12
12
|
# options[:columns] - number of columns
|
13
13
|
# options[:align] - align :left or :right
|
14
14
|
# options[:padding] - space to add to each cell
|
15
15
|
#
|
16
|
-
# The +format+
|
17
|
-
def initialize(
|
18
|
-
|
19
|
-
|
20
|
-
@
|
16
|
+
# The +format+ block MUST return ANSI codes.
|
17
|
+
def initialize(list, options={}, &format)
|
18
|
+
self.list = list
|
19
|
+
|
20
|
+
@columns = options[:columns]
|
21
|
+
@padding = options[:padding] || 1
|
21
22
|
@align = options[:align]
|
22
23
|
#@ansi = [options[:ansi]].flatten
|
23
24
|
@format = format
|
25
|
+
|
26
|
+
@columns = nil if @columns == 0
|
24
27
|
end
|
25
28
|
|
26
|
-
#
|
27
|
-
|
29
|
+
# List layout into columns. Each new line is taken to be
|
30
|
+
# a row-column cell.
|
31
|
+
attr_accessor :list
|
28
32
|
|
29
|
-
|
33
|
+
def list=(list)
|
34
|
+
case list
|
35
|
+
when ::String
|
36
|
+
@list = list.lines.to_a.map{ |e| e.chomp("\n") }
|
37
|
+
when ::Array
|
38
|
+
@list = list.map{ |e| e.to_s }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Default number of columns to display. If nil then the number
|
43
|
+
# of coumns is estimated from the size of the terminal.
|
30
44
|
attr_accessor :columns
|
31
45
|
|
32
|
-
#
|
46
|
+
# Padding size to apply to cells.
|
33
47
|
attr_accessor :padding
|
34
48
|
|
35
|
-
#
|
49
|
+
# Alignment to apply to cells.
|
36
50
|
attr_accessor :align
|
37
51
|
|
38
|
-
#
|
52
|
+
# Formating to apply to cells.
|
39
53
|
attr_accessor :format
|
40
54
|
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
55
|
+
# Return string in column layout. The number of columns is determined
|
56
|
+
# by the `columns` property or overriden by +cols+ argument.
|
57
|
+
#--
|
58
|
+
# TODO: Allow #to_s to take options and formating block?
|
59
|
+
#++
|
60
|
+
def to_s(cols=nil)
|
61
|
+
to_s_columns(cols || columns)
|
48
62
|
end
|
49
63
|
|
50
64
|
private
|
51
65
|
|
66
|
+
# Layout string lines into columns.
|
52
67
|
#
|
53
68
|
# TODO: put in empty strings for blank cells
|
54
|
-
def to_s_columns(columns)
|
55
|
-
lines =
|
69
|
+
def to_s_columns(columns=nil)
|
70
|
+
lines = list.to_a
|
56
71
|
count = lines.size
|
57
|
-
|
58
|
-
|
72
|
+
max = lines.map{ |l| l.size }.max
|
73
|
+
if columns.nil?
|
74
|
+
width = Terminal.terminal_width
|
75
|
+
columns = (width / (max + padding)).to_i
|
76
|
+
end
|
77
|
+
cols = []
|
78
|
+
mod = (count / columns.to_f).to_i
|
79
|
+
mod += 1 if count % columns != 0
|
80
|
+
|
59
81
|
lines.each_with_index do |line, index|
|
60
82
|
(cols[index % mod] ||=[]) << line.strip
|
61
83
|
end
|
62
|
-
|
84
|
+
|
63
85
|
pad = " " * padding
|
64
86
|
tmp = template(max, pad)
|
65
87
|
str = ""
|
66
88
|
cols.each_with_index do |row, c|
|
67
89
|
row.each_with_index do |cell, r|
|
68
|
-
|
90
|
+
ansi_codes = ansi_formating(cell, c, r)
|
91
|
+
if ansi_codes.empty?
|
92
|
+
str << (tmp % cell)
|
93
|
+
else
|
94
|
+
str << (tmp % cell).ansi(*ansi_codes)
|
95
|
+
end
|
69
96
|
end
|
97
|
+
str.rstrip!
|
70
98
|
str << "\n"
|
71
99
|
end
|
72
100
|
str
|
73
101
|
end
|
74
102
|
|
75
|
-
#
|
76
|
-
def to_s_auto
|
77
|
-
width = Terminal.terminal_width
|
78
|
-
end
|
79
|
-
|
103
|
+
# Aligns the cell left or right.
|
80
104
|
#
|
105
|
+
# TODO: Handle centered alignment.
|
81
106
|
def template(max, pad)
|
82
107
|
case align
|
83
108
|
when :right, 'right'
|
@@ -87,7 +112,7 @@ module ANSI
|
|
87
112
|
end
|
88
113
|
end
|
89
114
|
|
90
|
-
#
|
115
|
+
# Used to apply ANSI formating to each cell.
|
91
116
|
def ansi_formating(cell, col, row)
|
92
117
|
if @format
|
93
118
|
case @format.arity
|
@@ -0,0 +1,56 @@
|
|
1
|
+
= ANSI::Code
|
2
|
+
|
3
|
+
Require the library.
|
4
|
+
|
5
|
+
require 'ansi/code'
|
6
|
+
|
7
|
+
ANSI::Code can be used as a functions module.
|
8
|
+
|
9
|
+
str = ANSI::Code.red + "Hello" + ANSI::Code.blue + "World"
|
10
|
+
str.assert == "\e[31mHello\e[34mWorld"
|
11
|
+
|
12
|
+
If a block is supplied to each method then yielded value will
|
13
|
+
be wrapped in the ANSI code and clear code.
|
14
|
+
|
15
|
+
str = ANSI::Code.red{ "Hello" } + ANSI::Code.blue{ "World" }
|
16
|
+
str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
17
|
+
|
18
|
+
More conveniently the ANSI::Code module extends ANSI itself.
|
19
|
+
|
20
|
+
str = ANSI.red + "Hello" + ANSI.blue + "World"
|
21
|
+
str.assert == "\e[31mHello\e[34mWorld"
|
22
|
+
|
23
|
+
str = ANSI.red{ "Hello" } + ANSI.blue{ "World" }
|
24
|
+
str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
25
|
+
|
26
|
+
In the appropriate context the ANSI::Code module can also be
|
27
|
+
included, making its methods directly accessible.
|
28
|
+
|
29
|
+
include ANSI::Code
|
30
|
+
|
31
|
+
str = red + "Hello" + blue + "World"
|
32
|
+
str.assert == "\e[31mHello\e[34mWorld"
|
33
|
+
|
34
|
+
str = red{ "Hello" } + blue{ "World" }
|
35
|
+
str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
36
|
+
|
37
|
+
Along with the single font colors, the library include background colors.
|
38
|
+
|
39
|
+
str = on_red + "Hello"
|
40
|
+
str.assert == "\e[41mHello"
|
41
|
+
|
42
|
+
As well as combined color methods.
|
43
|
+
|
44
|
+
str = white_on_red + "Hello"
|
45
|
+
str.assert == "\e[37m\e[41mHello"
|
46
|
+
|
47
|
+
In addition the library offers an extension to String class
|
48
|
+
called #ansi, which allows some of the ANSI::Code methods
|
49
|
+
to be called in a more object-oriented fashion.
|
50
|
+
|
51
|
+
str = "Hello".ansi(:red) + "World".ansi(:blue)
|
52
|
+
str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
53
|
+
|
54
|
+
The ANSI::Code module supports most standard ANSI codes, though
|
55
|
+
not all platforms support every code, so YMMV.
|
56
|
+
|
data/qed/02_bbcode.rdoc
ADDED
@@ -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
|
+
|
data/qed/03_logger.rdoc
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= ANSI::Logger
|
2
|
+
|
3
|
+
Require the ANSI::Logger library.
|
4
|
+
|
5
|
+
require 'ansi/logger'
|
6
|
+
|
7
|
+
Create a new ANSI::Logger
|
8
|
+
|
9
|
+
log = ANSI::Logger.new(STDOUT)
|
10
|
+
|
11
|
+
Info logging appears normal.
|
12
|
+
|
13
|
+
log.info{"Info logs are green.\n"}
|
14
|
+
|
15
|
+
Warn logging appears yellow.
|
16
|
+
|
17
|
+
log.warn{"Warn logs are yellow.\n"}
|
18
|
+
|
19
|
+
Debug logging appears cyan.
|
20
|
+
|
21
|
+
log.debug{"Debug logs are cyan.\n"}
|
22
|
+
|
23
|
+
Error logging appears red.
|
24
|
+
|
25
|
+
log.error{"Error logs are red.\n"}
|
26
|
+
|
27
|
+
Fatal logging appears bright red.
|
28
|
+
|
29
|
+
log.fatal{"Fatal logs are bold red!\n"}
|
30
|
+
|
31
|
+
QED.
|
@@ -0,0 +1,63 @@
|
|
1
|
+
= ANSI::Progressbar
|
2
|
+
|
3
|
+
Pretty progress bars are easy to construct.
|
4
|
+
|
5
|
+
require 'ansi/progressbar'
|
6
|
+
|
7
|
+
pbar = ANSI::Progressbar.new("Test Bar", 100)
|
8
|
+
|
9
|
+
Running the bar simply requires calling the #inc method during
|
10
|
+
a loop and calling #finish when done.
|
11
|
+
|
12
|
+
100.times do |i|
|
13
|
+
sleep 0.01
|
14
|
+
pbar.inc
|
15
|
+
end
|
16
|
+
pbar.finish
|
17
|
+
|
18
|
+
We will use this same rountine in all the examples below, so lets
|
19
|
+
make a quick macro for it. Notice we have to use #reset first
|
20
|
+
before reusing the same progress bar.
|
21
|
+
|
22
|
+
def run(pbar)
|
23
|
+
pbar.reset
|
24
|
+
100.times do |i|
|
25
|
+
sleep 0.01
|
26
|
+
pbar.inc
|
27
|
+
end
|
28
|
+
pbar.finish
|
29
|
+
puts
|
30
|
+
end
|
31
|
+
|
32
|
+
The progress bar can be stylized in almost any way.
|
33
|
+
The #format setter provides control over the parts
|
34
|
+
that appear on the line. For example, by default the
|
35
|
+
format is:
|
36
|
+
|
37
|
+
pbar.format("%-14s %3d%% %s %s", :title, :percentage, :bar, :stat)
|
38
|
+
|
39
|
+
So lets vary it up to demonstrate the case.
|
40
|
+
|
41
|
+
pbar.format("%-14s %3d%% %s %s", :title, :percentage, :stat, :bar)
|
42
|
+
run(pbar)
|
43
|
+
|
44
|
+
The progress bar has an extra build in format intended for use with
|
45
|
+
file downloads called #transer_mode.
|
46
|
+
|
47
|
+
pbar.transfer_mode
|
48
|
+
run(pbar)
|
49
|
+
|
50
|
+
Calling this methods is the same as calling:
|
51
|
+
|
52
|
+
pbar.format("%-14s %3d%% %s %s",:title, :percentage, :bar, :stat_for_file_transfer)
|
53
|
+
run(pbar)
|
54
|
+
|
55
|
+
The #style setter allows each part of the line be modified with ANSI codes. And the
|
56
|
+
#bar_mark writer can be used to change the character used to make the bar.
|
57
|
+
|
58
|
+
pbar.standard_mode
|
59
|
+
pbar.style(:title => [:red], :bar=>[:blue])
|
60
|
+
pbar.bar_mark = "="
|
61
|
+
run(pbar)
|
62
|
+
|
63
|
+
QED.
|
data/qed/05_mixin.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= ANSI::Mixin
|
2
|
+
|
3
|
+
The ANSI::Mixin module is design for including into
|
4
|
+
String-like classes. It will support any class that defines
|
5
|
+
a #to_s method.
|
6
|
+
|
7
|
+
require 'ansi/mixin'
|
8
|
+
|
9
|
+
In this demonstration we will simply include it in the
|
10
|
+
core String class.
|
11
|
+
|
12
|
+
class ::String
|
13
|
+
include ANSI::Mixin
|
14
|
+
end
|
15
|
+
|
16
|
+
Now all strings will have access to ANSI's style and color
|
17
|
+
codes via simple method calls.
|
18
|
+
|
19
|
+
"roses".red.assert == "\e[31mroses\e[0m"
|
20
|
+
|
21
|
+
"violets".blue.assert == "\e[34mviolets\e[0m"
|
22
|
+
|
23
|
+
"sugar".italic.assert == "\e[3msugar\e[0m"
|
24
|
+
|
25
|
+
The method can be combined, of course.
|
26
|
+
|
27
|
+
"you".italic.bold.assert == "\e[1m\e[3myou\e[0m\e[0m"
|
28
|
+
|
29
|
+
The mixin also supports background methods.
|
30
|
+
|
31
|
+
"envy".on_green.assert == "\e[42menvy\e[0m"
|
32
|
+
|
33
|
+
And it also supports the combined foreground-on-background
|
34
|
+
methods.
|
35
|
+
|
36
|
+
"b&w".white_on_black.assert == "\e[37m\e[40mb&w\e[0m"
|
37
|
+
|
data/qed/06_string.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
= ANSI::String
|
2
|
+
|
3
|
+
The ANSI::String class is a very sophisticated implementation
|
4
|
+
of Ruby's standard String class, but one that can handle
|
5
|
+
ANSI codes seamlessly.
|
6
|
+
|
7
|
+
require 'ansi/string'
|
8
|
+
|
9
|
+
flower1 = ANSI::String.new("Roses")
|
10
|
+
flower2 = ANSI::String.new("Violets")
|
11
|
+
|
12
|
+
Like any other string.
|
13
|
+
|
14
|
+
flower1.to_s.assert == "Roses"
|
15
|
+
flower2.to_s.assert == "Violets"
|
16
|
+
|
17
|
+
Bet now we can add color.
|
18
|
+
|
19
|
+
flower1.red!
|
20
|
+
flower2.blue!
|
21
|
+
|
22
|
+
flower1.to_s.assert == "\e[31mRoses\e[0m"
|
23
|
+
flower2.to_s.assert == "\e[34mViolets\e[0m"
|
24
|
+
|
25
|
+
Despite that the string representation now contains ANSI codes,
|
26
|
+
we can still manipulate the string in much the same way that
|
27
|
+
we manipulate an ordinary string.
|
28
|
+
|
29
|
+
flower1.size.assert == 5
|
30
|
+
flower2.size.assert == 7
|
31
|
+
|
32
|
+
Like ordinary strings we can concatenate the two strings
|
33
|
+
|
34
|
+
flowers = flower1 + ' ' + flower2
|
35
|
+
flowers.to_s.assert == "\e[31mRoses\e[0m \e[34mViolets\e[0m"
|
36
|
+
|
37
|
+
flowers.size.assert == 13
|
38
|
+
|
39
|
+
Standard case conversion such as #upcase and #downcase work.
|
40
|
+
|
41
|
+
flower1.upcase.to_s.assert == "\e[31mROSES\e[0m"
|
42
|
+
flower1.downcase.to_s.assert == "\e[31mroses\e[0m"
|
43
|
+
|
44
|
+
Some of the most difficult methods to re-implement were the
|
45
|
+
substitution methods such as #sub and #gsub. They are still
|
46
|
+
somewhat more limited than the original string methods, but
|
47
|
+
their primary functionality should work.
|
48
|
+
|
49
|
+
flower1.gsub('s', 'z').to_s.assert == "\e[31mRozez\e[0m"
|
50
|
+
|
51
|
+
There are still a number of methods that need implementation.
|
52
|
+
ANSI::String is currently a very partial implementation. But
|
53
|
+
as you can see from the methods it does currently support,
|
54
|
+
is it already useful.
|
55
|
+
|
56
|
+
|
data/qed/07_columns.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
= ANSI::Columns
|
2
|
+
|
3
|
+
The +Columns+ class makes it easy to create nice looking text columns,
|
4
|
+
sorted from top to bottom, right to left (as opposed to the other way
|
5
|
+
around).
|
6
|
+
|
7
|
+
require 'ansi/columns'
|
8
|
+
|
9
|
+
list = %w{a b c d e f g h i j k l}
|
10
|
+
|
11
|
+
columns = ANSI::Columns.new(list)
|
12
|
+
|
13
|
+
columns.to_s(4)
|
14
|
+
|
15
|
+
The output will be:
|
16
|
+
|
17
|
+
a d g j
|
18
|
+
b e h k
|
19
|
+
c f i l
|
20
|
+
|
21
|
+
Besides and array of elements, Columns.new can take a string in which
|
22
|
+
the elements are divided by newlines characters. The default column
|
23
|
+
size can also be given to the initializer.
|
24
|
+
|
25
|
+
list = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl"
|
26
|
+
|
27
|
+
columns = ANSI::Columns.new(list, :columns=>6)
|
28
|
+
|
29
|
+
columns.to_s
|
30
|
+
|
31
|
+
The output will be:
|
32
|
+
|
33
|
+
a c e g i k
|
34
|
+
b d f h j l
|
35
|
+
|
36
|
+
If the column count is +nil+, then the number of columns will be calculated
|
37
|
+
as a best fit for the current terminal window.
|
38
|
+
|
39
|
+
== Padding
|
40
|
+
|
41
|
+
Columns can adjsut the padding between cells.
|
42
|
+
|
43
|
+
list = %w{a b c d e f g h i j k l}
|
44
|
+
|
45
|
+
columns = ANSI::Columns.new(list, :padding=>2)
|
46
|
+
|
47
|
+
columns.to_s(4)
|
48
|
+
|
49
|
+
The output will be:
|
50
|
+
|
51
|
+
a d g j
|
52
|
+
b e h k
|
53
|
+
c f i l
|
54
|
+
|
55
|
+
== Alignment
|
56
|
+
|
57
|
+
Columns can also be aligned either left or right.
|
58
|
+
|
59
|
+
list = %w{xx xx xx yy y yy z zz z}
|
60
|
+
|
61
|
+
columns = ANSI::Columns.new(list, :align=>:right)
|
62
|
+
|
63
|
+
columns.to_s(3)
|
64
|
+
|
65
|
+
The output will be:
|
66
|
+
|
67
|
+
xx yy z
|
68
|
+
xx y zz
|
69
|
+
xx yy z
|
70
|
+
|
71
|
+
== Format
|
72
|
+
|
73
|
+
Lastly, columns can be aguamented with ANSI codes. This is done through
|
74
|
+
a formating block. The block can take upto three parameters, the cell
|
75
|
+
content, the row and column numbers, or the cell and the row and column
|
76
|
+
numbers.
|
77
|
+
|
78
|
+
list = %w{a b c d e f g h i j k l}
|
79
|
+
|
80
|
+
columns = ANSI::Columns.new(list){ |r,c| c % 2 == 0 ? :red : :blue }
|
81
|
+
|
82
|
+
out = columns.to_s(4)
|
83
|
+
|
84
|
+
out.assert == (
|
85
|
+
"\e[31ma \e[0m\e[31md \e[0m\e[31mg \e[0m\e[31mj \e[0m\n" +
|
86
|
+
"\e[34mb \e[0m\e[34me \e[0m\e[34mh \e[0m\e[34mk \e[0m\n" +
|
87
|
+
"\e[31mc \e[0m\e[31mf \e[0m\e[31mi \e[0m\e[31ml \e[0m\n"
|
88
|
+
)
|
89
|
+
|
90
|
+
QED.
|
data/qed/08_table.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= ANSI::Table
|
2
|
+
|
3
|
+
The ANSI::Table class can be used to output tabular data with nicely
|
4
|
+
formated ASCII cell borders.
|
5
|
+
|
6
|
+
require 'ansi/table'
|
7
|
+
|
8
|
+
The constructor takes an 2-dimensional array.
|
9
|
+
|
10
|
+
data = [
|
11
|
+
[ 10, 20, 30 ],
|
12
|
+
[ 20, 10, 20 ],
|
13
|
+
[ 50, 40, 20 ]
|
14
|
+
]
|
15
|
+
|
16
|
+
table = ANSI::Table.new(data)
|
17
|
+
|
18
|
+
table.to_s
|
19
|
+
|
20
|
+
The output will be:
|
21
|
+
|
22
|
+
+----+----+----+
|
23
|
+
| 10 | 20 | 30 |
|
24
|
+
| 20 | 10 | 20 |
|
25
|
+
| 50 | 40 | 20 |
|
26
|
+
+----+----+----+
|
27
|
+
|
28
|
+
|
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 4
|
10
|
+
version: 1.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thomas Sawyer
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-05-01 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -47,7 +47,7 @@ dependencies:
|
|
47
47
|
version: "0"
|
48
48
|
type: :development
|
49
49
|
version_requirements: *id002
|
50
|
-
description: ANSI
|
50
|
+
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.
|
51
51
|
email: rubyworks-mailinglist@googlegroups.com
|
52
52
|
executables: []
|
53
53
|
|
@@ -70,18 +70,26 @@ files:
|
|
70
70
|
- lib/ansi/terminal/termios.rb
|
71
71
|
- lib/ansi/terminal/win32.rb
|
72
72
|
- lib/ansi/terminal.rb
|
73
|
-
- lib/ansi/version.rb
|
74
73
|
- lib/ansi.rb
|
74
|
+
- lib/ansi.yml
|
75
|
+
- qed/01_ansicode.rdoc
|
76
|
+
- qed/02_bbcode.rdoc
|
77
|
+
- qed/03_logger.rdoc
|
78
|
+
- qed/04_progressbar.rdoc
|
79
|
+
- qed/05_mixin.rdoc
|
80
|
+
- qed/06_string.rdoc
|
81
|
+
- qed/07_columns.rdoc
|
82
|
+
- qed/08_table.rdoc
|
83
|
+
- qed/applique/output.rb
|
75
84
|
- test/case_ansicode.rb
|
76
85
|
- test/case_bbcode.rb
|
77
86
|
- test/case_mixin.rb
|
78
87
|
- test/case_progressbar.rb
|
79
|
-
-
|
88
|
+
- Rakefile
|
89
|
+
- HISTORY.rdoc
|
80
90
|
- LICENSE
|
81
91
|
- README.rdoc
|
82
|
-
-
|
83
|
-
- NOTICE
|
84
|
-
- VERSION
|
92
|
+
- NOTICE.rdoc
|
85
93
|
has_rdoc: true
|
86
94
|
homepage: http://rubyworks.github.com/ansi
|
87
95
|
licenses:
|
data/PROFILE
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title : ANSI
|
3
|
-
summary: ANSI codes at your fingertips!
|
4
|
-
license: Apache 2.0
|
5
|
-
contact: rubyworks-mailinglist@googlegroups.com
|
6
|
-
created: 2004-08-01
|
7
|
-
|
8
|
-
requires:
|
9
|
-
- syckle (build)
|
10
|
-
- ko (test)
|
11
|
-
|
12
|
-
authors:
|
13
|
-
- Thomas Sawyer
|
14
|
-
- Florian Frank
|
15
|
-
|
16
|
-
description:
|
17
|
-
ANSI codes at your fingertips!
|
18
|
-
|
19
|
-
resources:
|
20
|
-
homepage : http://rubyworks.github.com/ansi
|
21
|
-
development : http://github.com/rubyworks/ansi
|
22
|
-
reference : http://rubyworks.github.com/ansi/rdoc
|
23
|
-
mailinglist : http://groups.google.com/group/rubyworks-mailinglist
|
24
|
-
repository : git://github.com/rubyworks/ansi.git
|
25
|
-
|
26
|
-
organization : RubyWorks
|
27
|
-
copyright : Copyright (c) 2009 Thomas Sawyer
|
28
|
-
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.2.3
|
data/lib/ansi/version.rb
DELETED