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.
- data/LICENSE +23 -0
- data/README.rdoc +85 -0
- data/Syckfile +77 -0
- data/lib/ansi/code.rb +239 -105
- data/lib/ansi/mixin.rb +113 -0
- data/lib/ansi/progressbar.rb +228 -220
- data/lib/ansi/string.rb +7 -5
- data/lib/ansi/terminal.rb +4 -3
- data/meta/{project → collection} +0 -0
- data/meta/copyright +1 -0
- data/meta/description +1 -0
- data/meta/{package → name} +0 -0
- data/meta/released +1 -1
- data/meta/sites/development +1 -0
- data/meta/sites/documentation +1 -0
- data/meta/{homepage → sites/homepage} +0 -0
- data/meta/sites/mailinglist +1 -0
- data/meta/{repository → sites/repository} +0 -0
- data/meta/version +1 -1
- data/test/test_ansicode.rb +2 -2
- data/test/test_bbcode.rb +2 -2
- metadata +30 -35
- data/COPYING +0 -789
- data/MANIFEST +0 -39
- data/README +0 -72
- data/demo/logger.rd +0 -31
- data/demo/progressbar.rd +0 -63
- data/meta/abstract +0 -3
data/lib/ansi/mixin.rb
ADDED
@@ -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
|
+
|
data/lib/ansi/progressbar.rb
CHANGED
@@ -7,264 +7,272 @@
|
|
7
7
|
|
8
8
|
require 'ansi/code'
|
9
9
|
|
10
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
39
|
-
|
22
|
+
def initialize(title, total, out=STDERR)
|
23
|
+
@title = title
|
24
|
+
@total = total
|
25
|
+
@out = out
|
40
26
|
|
41
|
-
|
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
|
-
|
44
|
-
attr_accessor :format_arguments
|
45
|
-
attr_accessor :styles
|
43
|
+
public
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
68
|
+
# Set ANSI styling options.
|
69
|
+
def style(options)
|
70
|
+
@styles = options
|
71
|
+
end
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
73
|
+
#
|
74
|
+
def standard_mode
|
75
|
+
@format = "%-14s %3d%% %s %s"
|
76
|
+
@format_arguments = [:title, :percentage, :bar, :stat]
|
77
|
+
end
|
82
78
|
|
83
|
-
|
84
|
-
|
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
|
-
|
87
|
-
|
88
|
-
@is_finished = true
|
89
|
-
show_progress
|
90
|
-
end
|
85
|
+
# For backward compatability
|
86
|
+
alias_method :file_transfer_mode, :transfer_mode
|
91
87
|
|
92
|
-
|
93
|
-
|
94
|
-
|
88
|
+
def finish
|
89
|
+
@current = @total
|
90
|
+
@is_finished = true
|
91
|
+
show_progress
|
92
|
+
end
|
95
93
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
94
|
+
def flush
|
95
|
+
@out.flush
|
96
|
+
end
|
100
97
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
raise "invalid 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
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
118
|
+
#
|
119
|
+
def reset
|
120
|
+
@current = 0
|
121
|
+
@is_finished = false
|
122
|
+
end
|
121
123
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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
|
-
|
130
|
-
|
131
|
-
|
131
|
+
def inspect
|
132
|
+
"(ProgressBar: #{@current}/#{@total})"
|
133
|
+
end
|
132
134
|
|
133
|
-
|
135
|
+
private
|
134
136
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
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
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
-
|
172
|
-
sprintf("ETA: %s", format_time(eta))
|
180
|
+
sprintf("Time: %s", format_time(elapsed))
|
173
181
|
end
|
174
|
-
|
175
|
-
|
176
|
-
|
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
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
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
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
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
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
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
|
-
|
260
|
-
|
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
|
-
|
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
|
+
|