ansi 1.1.0 → 1.2.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.
@@ -0,0 +1,113 @@
1
+ module ANSI
2
+ require 'ansi/code'
3
+
4
+ # This module is designed specifically for mixing into
5
+ # String-like classes or extending String-like objects.
6
+ #
7
+ # Generally speaking the String#ansi method is the more
8
+ # elegant approach to modifying a string with codes
9
+ # via a method call. But in some cases this Mixin's design
10
+ # might be preferable. In particular it was primarily
11
+ # designed to provide a compatability layer for the
12
+ # +colored+ gem.
13
+
14
+ module Mixin
15
+
16
+ def bold ; ANSI::Code.bold { to_s } ; end
17
+ def dark ; ANSI::Code.dark { to_s } ; end
18
+ def italic ; ANSI::Code.italic { to_s } ; end
19
+ def underline ; ANSI::Code.underline { to_s } ; end
20
+ def underscore ; ANSI::Code.underscore { to_s } ; end
21
+ def blink ; ANSI::Code.blink { to_s } ; end
22
+ def rapid ; ANSI::Code.rapid { to_s } ; end
23
+ def reverse ; ANSI::Code.reverse { to_s } ; end
24
+ def negative ; ANSI::Code.negative { to_s } ; end
25
+ def concealed ; ANSI::Code.concealed { to_s } ; end
26
+ def strike ; ANSI::Code.strike { to_s } ; end
27
+
28
+ def black ; ANSI::Code.black { to_s } ; end
29
+ def red ; ANSI::Code.red { to_s } ; end
30
+ def green ; ANSI::Code.green { to_s } ; end
31
+ def yellow ; ANSI::Code.yellow { to_s } ; end
32
+ def blue ; ANSI::Code.blue { to_s } ; end
33
+ def magenta ; ANSI::Code.magenta { to_s } ; end
34
+ def cyan ; ANSI::Code.cyan { to_s } ; end
35
+ def white ; ANSI::Code.white { to_s } ; end
36
+
37
+ def on_black ; ANSI::Code.on_black { to_s } ; end
38
+ def on_red ; ANSI::Code.on_red { to_s } ; end
39
+ def on_green ; ANSI::Code.on_green { to_s } ; end
40
+ def on_yellow ; ANSI::Code.on_yellow { to_s } ; end
41
+ def on_blue ; ANSI::Code.on_blue { to_s } ; end
42
+ def on_magenta ; ANSI::Code.on_magenta { to_s } ; end
43
+ def on_cyan ; ANSI::Code.on_cyan { to_s } ; end
44
+ def on_white ; ANSI::Code.on_white { to_s } ; end
45
+
46
+ def black_on_red ; ANSI::Code.black_on_red { to_s } ; end
47
+ def black_on_green ; ANSI::Code.black_on_green { to_s } ; end
48
+ def black_on_yellow ; ANSI::Code.black_on_yellow { to_s } ; end
49
+ def black_on_blue ; ANSI::Code.black_on_blue { to_s } ; end
50
+ def black_on_magenta ; ANSI::Code.black_on_magenta { to_s } ; end
51
+ def black_on_cyan ; ANSI::Code.black_on_cyan { to_s } ; end
52
+ def black_on_white ; ANSI::Code.black_on_white { to_s } ; end
53
+
54
+ def red_on_black ; ANSI::Code.red_on_black { to_s } ; end
55
+ def red_on_green ; ANSI::Code.red_on_green { to_s } ; end
56
+ def red_on_yellow ; ANSI::Code.red_on_yellow { to_s } ; end
57
+ def red_on_blue ; ANSI::Code.red_on_blue { to_s } ; end
58
+ def red_on_magenta ; ANSI::Code.red_on_magenta { to_s } ; end
59
+ def red_on_cyan ; ANSI::Code.red_on_cyan { to_s } ; end
60
+ def red_on_white ; ANSI::Code.red_on_white { to_s } ; end
61
+
62
+ def green_on_black ; ANSI::Code.green_on_black { to_s } ; end
63
+ def green_on_red ; ANSI::Code.green_on_red { to_s } ; end
64
+ def green_on_yellow ; ANSI::Code.green_on_yellow { to_s } ; end
65
+ def green_on_blue ; ANSI::Code.green_on_blue { to_s } ; end
66
+ def green_on_magenta ; ANSI::Code.green_on_magenta { to_s } ; end
67
+ def green_on_cyan ; ANSI::Code.green_on_cyan { to_s } ; end
68
+ def green_on_white ; ANSI::Code.green_on_white { to_s } ; end
69
+
70
+ def yellow_on_black ; ANSI::Code.yellow_on_black { to_s } ; end
71
+ def yellow_on_red ; ANSI::Code.yellow_on_red { to_s } ; end
72
+ def yellow_on_green ; ANSI::Code.yellow_on_green { to_s } ; end
73
+ def yellow_on_blue ; ANSI::Code.yellow_on_blue { to_s } ; end
74
+ def yellow_on_magenta ; ANSI::Code.yellow_on_magenta { to_s } ; end
75
+ def yellow_on_cyan ; ANSI::Code.yellow_on_cyan { to_s } ; end
76
+ def yellow_on_white ; ANSI::Code.yellow_on_white { to_s } ; end
77
+
78
+ def blue_on_black ; ANSI::Code.blue_on_black { to_s } ; end
79
+ def blue_on_red ; ANSI::Code.blue_on_red { to_s } ; end
80
+ def blue_on_green ; ANSI::Code.blue_on_green { to_s } ; end
81
+ def blue_on_yellow ; ANSI::Code.blue_on_yellow { to_s } ; end
82
+ def blue_on_magenta ; ANSI::Code.blue_on_magenta { to_s } ; end
83
+ def blue_on_cyan ; ANSI::Code.blue_on_cyan { to_s } ; end
84
+ def blue_on_white ; ANSI::Code.blue_on_white { to_s } ; end
85
+
86
+ def magenta_on_black ; ANSI::Code.magenta_on_black { to_s } ; end
87
+ def magenta_on_red ; ANSI::Code.magenta_on_red { to_s } ; end
88
+ def magenta_on_green ; ANSI::Code.magenta_on_green { to_s } ; end
89
+ def magenta_on_yellow ; ANSI::Code.magenta_on_yellow { to_s } ; end
90
+ def magenta_on_blue ; ANSI::Code.magenta_on_blue { to_s } ; end
91
+ def magenta_on_cyan ; ANSI::Code.magenta_on_cyan { to_s } ; end
92
+ def magenta_on_white ; ANSI::Code.magenta_on_white { to_s } ; end
93
+
94
+ def cyan_on_black ; ANSI::Code.cyan_on_black { to_s } ; end
95
+ def cyan_on_red ; ANSI::Code.cyan_on_red { to_s } ; end
96
+ def cyan_on_green ; ANSI::Code.cyan_on_green { to_s } ; end
97
+ def cyan_on_yellow ; ANSI::Code.cyan_on_yellow { to_s } ; end
98
+ def cyan_on_blue ; ANSI::Code.cyan_on_blue { to_s } ; end
99
+ def cyan_on_magenta ; ANSI::Code.cyan_on_magenta { to_s } ; end
100
+ def cyan_on_white ; ANSI::Code.cyan_on_white { to_s } ; end
101
+
102
+ def white_on_black ; ANSI::Code.white_on_black { to_s } ; end
103
+ def white_on_red ; ANSI::Code.white_on_red { to_s } ; end
104
+ def white_on_green ; ANSI::Code.white_on_green { to_s } ; end
105
+ def white_on_yellow ; ANSI::Code.white_on_yellow { to_s } ; end
106
+ def white_on_blue ; ANSI::Code.white_on_blue { to_s } ; end
107
+ def white_on_magenta ; ANSI::Code.white_on_magenta { to_s } ; end
108
+ def white_on_cyan ; ANSI::Code.white_on_cyan { to_s } ; end
109
+
110
+ end
111
+
112
+ end
113
+
@@ -7,264 +7,272 @@
7
7
 
8
8
  require 'ansi/code'
9
9
 
10
- # = Progressbar
11
- #
12
- # Progressbar is a text-based progressbar library.
13
- #
14
- # pbar = Progressbar.new( "Demo", 100 )
15
- # 100.times { pbar.inc }
16
- # pbar.finish
17
- #
18
- class ANSI::Progressbar
19
- #
20
- def initialize(title, total, out=STDERR)
21
- @title = title
22
- @total = total
23
- @out = out
10
+ module ANSI
24
11
 
25
- @bar_length = 80
26
- @bar_mark = "o"
27
- @total_overflow = true
28
- @current = 0
29
- @previous = 0
30
- @is_finished = false
31
- @start_time = Time.now
32
- @format = "%-14s %3d%% %s %s"
33
- @format_arguments = [:title, :percentage, :bar, :stat]
34
- @styles = {}
35
- #
36
- yield self if block_given?
12
+ # = Progressbar
13
+ #
14
+ # Progressbar is a text-based progressbar library.
15
+ #
16
+ # pbar = Progressbar.new( "Demo", 100 )
17
+ # 100.times { pbar.inc }
18
+ # pbar.finish
19
+ #
20
+ class ProgressBar
37
21
  #
38
- show_progress
39
- end
22
+ def initialize(title, total, out=STDERR)
23
+ @title = title
24
+ @total = total
25
+ @out = out
40
26
 
41
- public
27
+ @bar_length = 80
28
+ @bar_mark = "o"
29
+ @total_overflow = true
30
+ @current = 0
31
+ @previous = 0
32
+ @is_finished = false
33
+ @start_time = Time.now
34
+ @format = "%-14s %3d%% %s %s"
35
+ @format_arguments = [:title, :percentage, :bar, :stat]
36
+ @styles = {}
37
+ #
38
+ yield self if block_given?
39
+ #
40
+ show_progress
41
+ end
42
42
 
43
- attr_accessor :format
44
- attr_accessor :format_arguments
45
- attr_accessor :styles
43
+ public
46
44
 
47
- #
48
- def title=(str)
49
- @title = str
50
- end
51
- #
52
- def bar_mark=(mark)
53
- @bar_mark = String(mark)[0..0]
54
- end
45
+ attr_accessor :format
46
+ attr_accessor :format_arguments
47
+ attr_accessor :styles
55
48
 
56
- def total_overflow=(boolv)
57
- @total_overflow = boolv ? true : false
58
- end
49
+ #
50
+ def title=(str)
51
+ @title = str
52
+ end
53
+ #
54
+ def bar_mark=(mark)
55
+ @bar_mark = String(mark)[0..0]
56
+ end
59
57
 
60
- # Set format and format arguments.
61
- def format(format, *arguments)
62
- @format = format
63
- @format_arguments = *arguments unless arguments.empty?
64
- end
58
+ def total_overflow=(boolv)
59
+ @total_overflow = boolv ? true : false
60
+ end
65
61
 
66
- # Set ANSI styling options.
67
- def style(options)
68
- @styles = options
69
- end
62
+ # Set format and format arguments.
63
+ def format(format, *arguments)
64
+ @format = format
65
+ @format_arguments = *arguments unless arguments.empty?
66
+ end
70
67
 
71
- #
72
- def standard_mode
73
- @format = "%-14s %3d%% %s %s"
74
- @format_arguments = [:title, :percentage, :bar, :stat]
75
- end
68
+ # Set ANSI styling options.
69
+ def style(options)
70
+ @styles = options
71
+ end
76
72
 
77
- #
78
- def transfer_mode
79
- @format = "%-14s %3d%% %s %s"
80
- @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
81
- end
73
+ #
74
+ def standard_mode
75
+ @format = "%-14s %3d%% %s %s"
76
+ @format_arguments = [:title, :percentage, :bar, :stat]
77
+ end
82
78
 
83
- # For backward compatability
84
- alias_method :file_transfer_mode, :transfer_mode
79
+ #
80
+ def transfer_mode
81
+ @format = "%-14s %3d%% %s %s"
82
+ @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
83
+ end
85
84
 
86
- def finish
87
- @current = @total
88
- @is_finished = true
89
- show_progress
90
- end
85
+ # For backward compatability
86
+ alias_method :file_transfer_mode, :transfer_mode
91
87
 
92
- def flush
93
- @out.flush
94
- end
88
+ def finish
89
+ @current = @total
90
+ @is_finished = true
91
+ show_progress
92
+ end
95
93
 
96
- def halt
97
- @is_finished = true
98
- show_progress
99
- end
94
+ def flush
95
+ @out.flush
96
+ end
100
97
 
101
- def set(count)
102
- if count < 0
103
- raise "invalid count less than zero: #{count}"
104
- elsif count > @total
105
- if @total_overflow
106
- @total = count + 1
107
- else
108
- raise "invalid count greater than total: #{count}"
98
+ def halt
99
+ @is_finished = true
100
+ show_progress
101
+ end
102
+
103
+ def set(count)
104
+ if count < 0
105
+ raise "invalid count less than zero: #{count}"
106
+ elsif count > @total
107
+ if @total_overflow
108
+ @total = count + 1
109
+ else
110
+ raise "invalid count greater than total: #{count}"
111
+ end
109
112
  end
113
+ @current = count
114
+ show_progress
115
+ @previous = @current
110
116
  end
111
- @current = count
112
- show_progress
113
- @previous = @current
114
- end
115
117
 
116
- #
117
- def reset
118
- @current = 0
119
- @is_finished = false
120
- end
118
+ #
119
+ def reset
120
+ @current = 0
121
+ @is_finished = false
122
+ end
121
123
 
122
- def inc(step = 1)
123
- @current += step
124
- @current = @total if @current > @total
125
- show_progress
126
- @previous = @current
127
- end
124
+ def inc(step = 1)
125
+ @current += step
126
+ @current = @total if @current > @total
127
+ show_progress
128
+ @previous = @current
129
+ end
128
130
 
129
- def inspect
130
- "(ProgressBar: #{@current}/#{@total})"
131
- end
131
+ def inspect
132
+ "(ProgressBar: #{@current}/#{@total})"
133
+ end
132
134
 
133
- private
135
+ private
134
136
 
135
- #
136
- def convert_bytes(bytes)
137
- if bytes < 1024
138
- sprintf("%6dB", bytes)
139
- elsif bytes < 1024 * 1000 # 1000kb
140
- sprintf("%5.1fKB", bytes.to_f / 1024)
141
- elsif bytes < 1024 * 1024 * 1000 # 1000mb
142
- sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
143
- else
144
- sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
137
+ #
138
+ def convert_bytes(bytes)
139
+ if bytes < 1024
140
+ sprintf("%6dB", bytes)
141
+ elsif bytes < 1024 * 1000 # 1000kb
142
+ sprintf("%5.1fKB", bytes.to_f / 1024)
143
+ elsif bytes < 1024 * 1024 * 1000 # 1000mb
144
+ sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
145
+ else
146
+ sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
147
+ end
145
148
  end
146
- end
147
- #
148
- def transfer_rate
149
- bytes_per_second = @current.to_f / (Time.now - @start_time)
150
- sprintf("%s/s", convert_bytes(bytes_per_second))
151
- end
152
- #
153
- def bytes
154
- convert_bytes(@current)
155
- end
156
- #
157
- def format_time(t)
158
- t = t.to_i
159
- sec = t % 60
160
- min = (t / 60) % 60
161
- hour = t / 3600
162
- sprintf("%02d:%02d:%02d", hour, min, sec);
163
- end
164
- #
165
- # ETA stands for Estimated Time of Arrival.
166
- def eta
167
- if @current == 0
168
- "ETA: --:--:--"
169
- else
149
+ #
150
+ def transfer_rate
151
+ bytes_per_second = @current.to_f / (Time.now - @start_time)
152
+ sprintf("%s/s", convert_bytes(bytes_per_second))
153
+ end
154
+ #
155
+ def bytes
156
+ convert_bytes(@current)
157
+ end
158
+ #
159
+ def format_time(t)
160
+ t = t.to_i
161
+ sec = t % 60
162
+ min = (t / 60) % 60
163
+ hour = t / 3600
164
+ sprintf("%02d:%02d:%02d", hour, min, sec);
165
+ end
166
+ #
167
+ # ETA stands for Estimated Time of Arrival.
168
+ def eta
169
+ if @current == 0
170
+ "ETA: --:--:--"
171
+ else
172
+ elapsed = Time.now - @start_time
173
+ eta = elapsed * @total / @current - elapsed;
174
+ sprintf("ETA: %s", format_time(eta))
175
+ end
176
+ end
177
+ #
178
+ def elapsed
170
179
  elapsed = Time.now - @start_time
171
- eta = elapsed * @total / @current - elapsed;
172
- sprintf("ETA: %s", format_time(eta))
180
+ sprintf("Time: %s", format_time(elapsed))
173
181
  end
174
- end
175
- #
176
- def elapsed
177
- elapsed = Time.now - @start_time
178
- sprintf("Time: %s", format_time(elapsed))
179
- end
180
- #
181
- def stat
182
- if @is_finished then elapsed else eta end
183
- end
184
- #
185
- def stat_for_file_transfer
186
- if @is_finished then
187
- sprintf("%s %s %s", bytes, transfer_rate, elapsed)
188
- else
189
- sprintf("%s %s %s", bytes, transfer_rate, eta)
182
+ #
183
+ def stat
184
+ if @is_finished then elapsed else eta end
190
185
  end
191
- end
192
- #
193
- def eol
194
- if @is_finished then "\n" else "\r" end
195
- end
196
- #
197
- def bar
198
- len = percentage * @bar_length / 100
199
- sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len))
200
- end
201
- #
202
- def percentage
203
- if @total.zero?
204
- 100
205
- else
206
- @current * 100 / @total
186
+ #
187
+ def stat_for_file_transfer
188
+ if @is_finished then
189
+ sprintf("%s %s %s", bytes, transfer_rate, elapsed)
190
+ else
191
+ sprintf("%s %s %s", bytes, transfer_rate, eta)
192
+ end
207
193
  end
208
- end
209
- #
210
- def title
211
- @title[0,13] + ":"
212
- end
213
- #
214
- def get_width
215
- # FIXME: I don't know how portable it is.
216
- default_width = 80
217
- begin
218
- tiocgwinsz = 0x5413
219
- data = [0, 0, 0, 0].pack("SSSS")
220
- if @out.ioctl(tiocgwinsz, data) >= 0 then
221
- rows, cols, xpixels, ypixels = data.unpack("SSSS")
222
- if cols >= 0 then cols else default_width end
194
+ #
195
+ def eol
196
+ if @is_finished then "\n" else "\r" end
197
+ end
198
+ #
199
+ def bar
200
+ len = percentage * @bar_length / 100
201
+ sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len))
202
+ end
203
+ #
204
+ def percentage
205
+ if @total.zero?
206
+ 100
223
207
  else
208
+ @current * 100 / @total
209
+ end
210
+ end
211
+ #
212
+ def title
213
+ @title[0,13] + ":"
214
+ end
215
+ #
216
+ def get_width
217
+ # FIXME: I don't know how portable it is.
218
+ default_width = 80
219
+ begin
220
+ tiocgwinsz = 0x5413
221
+ data = [0, 0, 0, 0].pack("SSSS")
222
+ if @out.ioctl(tiocgwinsz, data) >= 0 then
223
+ rows, cols, xpixels, ypixels = data.unpack("SSSS")
224
+ if cols >= 0 then cols else default_width end
225
+ else
226
+ default_width
227
+ end
228
+ rescue Exception
224
229
  default_width
225
230
  end
226
- rescue Exception
227
- default_width
228
231
  end
229
- end
230
232
 
231
- #
232
- def show
233
- arguments = @format_arguments.map do |method|
234
- colorize(send(method), styles[method])
235
- end
236
- line = sprintf(@format, *arguments)
237
- width = get_width
238
- length = ANSI::Code.uncolored(line).length
239
- if length == width - 1
240
- @out.print(line + eol)
241
- elsif length >= width
242
- @bar_length = [@bar_length - (length - width + 1), 0].max
243
- @bar_length == 0 ? @out.print(line + eol) : show
244
- else #line.length < width - 1
245
- @bar_length += width - length + 1
246
- show
233
+ #
234
+ def show
235
+ arguments = @format_arguments.map do |method|
236
+ colorize(send(method), styles[method])
237
+ end
238
+ line = sprintf(@format, *arguments)
239
+ width = get_width
240
+ length = ANSI::Code.uncolor{line}.length
241
+ if length == width - 1
242
+ @out.print(line + eol)
243
+ elsif length >= width
244
+ @bar_length = [@bar_length - (length - width + 1), 0].max
245
+ @bar_length == 0 ? @out.print(line + eol) : show
246
+ else #line.length < width - 1
247
+ @bar_length += width - length + 1
248
+ show
249
+ end
247
250
  end
248
- end
249
251
 
250
- #
251
- def show_progress
252
- if @total.zero?
253
- cur_percentage = 100
254
- prev_percentage = 0
255
- else
256
- cur_percentage = (@current * 100 / @total).to_i
257
- prev_percentage = (@previous * 100 / @total).to_i
252
+ #
253
+ def show_progress
254
+ if @total.zero?
255
+ cur_percentage = 100
256
+ prev_percentage = 0
257
+ else
258
+ cur_percentage = (@current * 100 / @total).to_i
259
+ prev_percentage = (@previous * 100 / @total).to_i
260
+ end
261
+ if cur_percentage > prev_percentage || @is_finished
262
+ show
263
+ end
258
264
  end
259
- if cur_percentage > prev_percentage || @is_finished
260
- show
265
+
266
+ #
267
+ def colorize(part, style)
268
+ return part unless style
269
+ [style].flatten.inject(part){ |pt, st| ANSI::Code.send(st){pt} }
261
270
  end
271
+
262
272
  end
263
273
 
264
274
  #
265
- def colorize(part, style)
266
- return part unless style
267
- [style].flatten.inject(part){ |pt, st| ANSI::Code.send(st){ pt } }
268
- end
275
+ Progressbar = ProgressBar
269
276
 
270
277
  end
278
+