ansi 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +12 -0
- data/LICENSE +789 -0
- data/MANIFEST +36 -0
- data/README +72 -0
- data/demo/logger.rd +31 -0
- data/demo/progressbar.rd +63 -0
- data/lib/ansi.rb +11 -0
- data/lib/ansi/code.rb +229 -0
- data/lib/ansi/logger.rb +209 -0
- data/lib/ansi/progressbar.rb +268 -0
- data/lib/ansi/string.rb +249 -0
- data/lib/ansi/terminal.rb +43 -0
- data/lib/ansi/terminal/curses.rb +27 -0
- data/lib/ansi/terminal/stty.rb +55 -0
- data/lib/ansi/terminal/termios.rb +63 -0
- data/lib/ansi/terminal/win32.rb +107 -0
- data/meta/abstract +3 -0
- data/meta/authors +3 -0
- data/meta/created +1 -0
- data/meta/homepage +1 -0
- data/meta/license +1 -0
- data/meta/package +1 -0
- data/meta/project +1 -0
- data/meta/released +1 -0
- data/meta/repository +1 -0
- data/meta/summary +1 -0
- data/meta/title +1 -0
- data/meta/version +1 -0
- data/test/test_ansicode.rb +20 -0
- data/test/test_progressbar.rb +18 -0
- metadata +93 -0
data/MANIFEST
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
HISTORY
|
2
|
+
LICENSE
|
3
|
+
MANIFEST
|
4
|
+
README
|
5
|
+
demo
|
6
|
+
demo/logger.rd
|
7
|
+
demo/progressbar.rd
|
8
|
+
lib
|
9
|
+
lib/ansi
|
10
|
+
lib/ansi.rb
|
11
|
+
lib/ansi/code.rb
|
12
|
+
lib/ansi/logger.rb
|
13
|
+
lib/ansi/progressbar.rb
|
14
|
+
lib/ansi/string.rb
|
15
|
+
lib/ansi/terminal
|
16
|
+
lib/ansi/terminal.rb
|
17
|
+
lib/ansi/terminal/curses.rb
|
18
|
+
lib/ansi/terminal/stty.rb
|
19
|
+
lib/ansi/terminal/termios.rb
|
20
|
+
lib/ansi/terminal/win32.rb
|
21
|
+
meta
|
22
|
+
meta/abstract
|
23
|
+
meta/authors
|
24
|
+
meta/created
|
25
|
+
meta/homepage
|
26
|
+
meta/license
|
27
|
+
meta/package
|
28
|
+
meta/project
|
29
|
+
meta/released
|
30
|
+
meta/repository
|
31
|
+
meta/summary
|
32
|
+
meta/title
|
33
|
+
meta/version
|
34
|
+
test
|
35
|
+
test/test_ansicode.rb
|
36
|
+
test/test_progressbar.rb
|
data/README
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
= ANSI
|
2
|
+
|
3
|
+
* http://death.rubyforge.org
|
4
|
+
* http://death.rubyforge.org/ansi
|
5
|
+
|
6
|
+
|
7
|
+
== DESCRIPTION
|
8
|
+
|
9
|
+
The ANSI project is a collection of ANSI 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 include
|
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 as module functions.
|
23
|
+
* Good coverage of standard ANSI codes.
|
24
|
+
* Windows support needs some TLC :(
|
25
|
+
|
26
|
+
|
27
|
+
== RELEASE NOTES
|
28
|
+
|
29
|
+
Please see HISTORY file.
|
30
|
+
|
31
|
+
|
32
|
+
== SYNOPSIS
|
33
|
+
|
34
|
+
The ANSI::Code module defines ANSI codes as methods.
|
35
|
+
|
36
|
+
include ANSICode
|
37
|
+
|
38
|
+
p red, "Hello", blue, "World"
|
39
|
+
=> "\e[31mHello\e[34mWorld"
|
40
|
+
|
41
|
+
p red { "Hello" } + blue { "World" }
|
42
|
+
=> "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
43
|
+
|
44
|
+
Thes mehods can also be called as module methods, eg. 'ANSI::Code.red'.
|
45
|
+
|
46
|
+
Please see the online documentation for more information on using
|
47
|
+
the other libraries.
|
48
|
+
|
49
|
+
|
50
|
+
== HOW TO INSTALL
|
51
|
+
|
52
|
+
To install with RubyGems simply open a console and type:
|
53
|
+
|
54
|
+
gem install ansi
|
55
|
+
|
56
|
+
Local installation requires Setup.rb (gem install setup),
|
57
|
+
then download the tarball package and type:
|
58
|
+
|
59
|
+
tar -xvzf ansi-1.0.0.tgz
|
60
|
+
cd ansi-1.0.0
|
61
|
+
sudo setup.rb all
|
62
|
+
|
63
|
+
Windows users use 'ruby setup.rb all'.
|
64
|
+
|
65
|
+
|
66
|
+
== LICENSE/COPYRIGHT
|
67
|
+
|
68
|
+
Copyright (c) 2004 Coding Dead
|
69
|
+
|
70
|
+
This program is ditributed unser the terms of the LGPLv3 license.
|
71
|
+
|
72
|
+
See LICENSE file for details.
|
data/demo/logger.rd
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.
|
data/demo/progressbar.rd
ADDED
@@ -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/lib/ansi.rb
ADDED
data/lib/ansi/code.rb
ADDED
@@ -0,0 +1,229 @@
|
|
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?
|
10
|
+
|
11
|
+
module ANSI
|
12
|
+
|
13
|
+
# Currently Windows is not supported.
|
14
|
+
SUPPORTED = !(PLATFORM =~ /win/)
|
15
|
+
|
16
|
+
# Ansi::Code module makes it very easy to use ANSI codes.
|
17
|
+
# These are esspecially nice for beautifying shell output.
|
18
|
+
#
|
19
|
+
# include Ansi::Code
|
20
|
+
#
|
21
|
+
# p red, "Hello", blue, "World"
|
22
|
+
# => "\e[31mHello\e[34mWorld"
|
23
|
+
#
|
24
|
+
# p red { "Hello" } + blue { "World" }
|
25
|
+
# => "\e[31mHello\e[0m\e[34mWorld\e[0m"
|
26
|
+
#
|
27
|
+
# == Supported ANSI Commands
|
28
|
+
#
|
29
|
+
# The following is a list of supported codes.
|
30
|
+
#
|
31
|
+
# save
|
32
|
+
# restore
|
33
|
+
# clear_screen
|
34
|
+
# cls # synonym for :clear_screen
|
35
|
+
# clear_line
|
36
|
+
# clr # synonym for :clear_line
|
37
|
+
# move
|
38
|
+
# up
|
39
|
+
# down
|
40
|
+
# left
|
41
|
+
# right
|
42
|
+
# display
|
43
|
+
#
|
44
|
+
# clear
|
45
|
+
# reset # synonym for :clear
|
46
|
+
# bold
|
47
|
+
# dark
|
48
|
+
# italic # not widely implemented
|
49
|
+
# underline
|
50
|
+
# underscore # synonym for :underline
|
51
|
+
# blink
|
52
|
+
# rapid_blink # not widely implemented
|
53
|
+
# negative # no reverse because of String#reverse
|
54
|
+
# concealed
|
55
|
+
# strikethrough # not widely implemented
|
56
|
+
#
|
57
|
+
# black
|
58
|
+
# red
|
59
|
+
# green
|
60
|
+
# yellow
|
61
|
+
# blue
|
62
|
+
# magenta
|
63
|
+
# cyan
|
64
|
+
# white
|
65
|
+
#
|
66
|
+
# on_black
|
67
|
+
# on_red
|
68
|
+
# on_green
|
69
|
+
# on_yellow
|
70
|
+
# on_blue
|
71
|
+
# on_magenta
|
72
|
+
# on_cyan
|
73
|
+
# on_white
|
74
|
+
#
|
75
|
+
module Code
|
76
|
+
|
77
|
+
extend self
|
78
|
+
|
79
|
+
# Save current cursor positon.
|
80
|
+
def save
|
81
|
+
"\e[s"
|
82
|
+
end
|
83
|
+
|
84
|
+
# Restore saved cursor positon.
|
85
|
+
def restore
|
86
|
+
"\e[u"
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clear the screen and move cursor to home.
|
90
|
+
def clear_screen
|
91
|
+
"\e[2J"
|
92
|
+
end
|
93
|
+
alias_method :cls, :clear_screen
|
94
|
+
|
95
|
+
# Clear to the end of the current line.
|
96
|
+
def clear_line
|
97
|
+
"\e[K"
|
98
|
+
end
|
99
|
+
alias_method :clr, :clear_line
|
100
|
+
|
101
|
+
#--
|
102
|
+
#def position
|
103
|
+
# "\e[#;#R"
|
104
|
+
#end
|
105
|
+
#++
|
106
|
+
|
107
|
+
# Move curose to line and column.
|
108
|
+
def move( line, column=0 )
|
109
|
+
"\e[#{line.to_i};#{column.to_i}H"
|
110
|
+
end
|
111
|
+
|
112
|
+
# Move cursor up a specificed number of spaces.
|
113
|
+
def up( spaces=1 )
|
114
|
+
"\e[#{spaces.to_i}A"
|
115
|
+
end
|
116
|
+
|
117
|
+
# Move cursor down a specificed number of spaces.
|
118
|
+
def down( spaces=1 )
|
119
|
+
"\e[#{spaces.to_i}B"
|
120
|
+
end
|
121
|
+
|
122
|
+
# Move cursor left a specificed number of spaces.
|
123
|
+
def left( spaces=1 )
|
124
|
+
"\e[#{spaces.to_i}D"
|
125
|
+
end
|
126
|
+
|
127
|
+
# Move cursor right a specificed number of spaces.
|
128
|
+
def right( spaces=1 )
|
129
|
+
"\e[#{spaces.to_i}C"
|
130
|
+
end
|
131
|
+
|
132
|
+
# Like +move+ but returns to original positon
|
133
|
+
# after yielding block or adding string argument.
|
134
|
+
def display( line, column=0, string=nil ) #:yield:
|
135
|
+
result = "\e[s"
|
136
|
+
result << "\e[#{line.to_i};#{column.to_i}H"
|
137
|
+
if block_given?
|
138
|
+
result << yield
|
139
|
+
result << "\e[u"
|
140
|
+
elsif string
|
141
|
+
result << string
|
142
|
+
result << "\e[u"
|
143
|
+
elsif respond_to?(:to_str)
|
144
|
+
result << self
|
145
|
+
result << "\e[u"
|
146
|
+
end
|
147
|
+
return result
|
148
|
+
end
|
149
|
+
|
150
|
+
# Define color codes.
|
151
|
+
def self.define_ansicolor_method(name,code)
|
152
|
+
class_eval <<-HERE
|
153
|
+
def #{name.to_s}(string = nil)
|
154
|
+
result = "\e[#{code}m"
|
155
|
+
if block_given?
|
156
|
+
result << yield
|
157
|
+
result << "\e[0m"
|
158
|
+
elsif string
|
159
|
+
result << string
|
160
|
+
result << "\e[0m"
|
161
|
+
elsif respond_to?(:to_str)
|
162
|
+
result << self
|
163
|
+
result << "\e[0m"
|
164
|
+
end
|
165
|
+
return result
|
166
|
+
end
|
167
|
+
HERE
|
168
|
+
end
|
169
|
+
|
170
|
+
@@colors = [
|
171
|
+
[ :clear , 0 ],
|
172
|
+
[ :reset , 0 ], # synonym for :clear
|
173
|
+
[ :bold , 1 ],
|
174
|
+
[ :dark , 2 ],
|
175
|
+
[ :italic , 3 ], # not widely implemented
|
176
|
+
[ :underline , 4 ],
|
177
|
+
[ :underscore , 4 ], # synonym for :underline
|
178
|
+
[ :blink , 5 ],
|
179
|
+
[ :rapid_blink , 6 ], # not widely implemented
|
180
|
+
[ :negative , 7 ], # no reverse because of String#reverse
|
181
|
+
[ :concealed , 8 ],
|
182
|
+
[ :strikethrough, 9 ], # not widely implemented
|
183
|
+
[ :black , 30 ],
|
184
|
+
[ :red , 31 ],
|
185
|
+
[ :green , 32 ],
|
186
|
+
[ :yellow , 33 ],
|
187
|
+
[ :blue , 34 ],
|
188
|
+
[ :magenta , 35 ],
|
189
|
+
[ :cyan , 36 ],
|
190
|
+
[ :white , 37 ],
|
191
|
+
[ :on_black , 40 ],
|
192
|
+
[ :on_red , 41 ],
|
193
|
+
[ :on_green , 42 ],
|
194
|
+
[ :on_yellow , 43 ],
|
195
|
+
[ :on_blue , 44 ],
|
196
|
+
[ :on_magenta , 45 ],
|
197
|
+
[ :on_cyan , 46 ],
|
198
|
+
[ :on_white , 47 ],
|
199
|
+
]
|
200
|
+
|
201
|
+
@@colors.each do |c, v|
|
202
|
+
define_ansicolor_method(c, v)
|
203
|
+
end
|
204
|
+
|
205
|
+
ColoredRegexp = /\e\[([34][0-7]|[0-9])m/
|
206
|
+
|
207
|
+
module_function
|
208
|
+
|
209
|
+
#
|
210
|
+
def uncolored(string = nil)
|
211
|
+
if block_given?
|
212
|
+
yield.gsub(ColoredRegexp, '')
|
213
|
+
elsif string
|
214
|
+
string.gsub(ColoredRegexp, '')
|
215
|
+
elsif respond_to?(:to_str)
|
216
|
+
gsub(ColoredRegexp, '')
|
217
|
+
else
|
218
|
+
''
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
#
|
223
|
+
def colors
|
224
|
+
@@colors.map { |c| c[0] }
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|