Skip to content

Loading…

Custom bar mark #1

Merged
merged 2 commits into from

2 participants

@vicvega

Hi, I've just added a simple function to allow custom bar_mark in the progress bar.
Thanks for your gem

@peleteiro peleteiro merged commit 7136b84 into peleteiro:master
@peleteiro
Owner

Thanks

@vicvega

Hi,
I've just found a project (https://github.com/nex3/ruby-progressbar) which provides the same progress bar and seems to be more active...
See this commit (a couple of year ago)
nex3/ruby-progressbar@e2b1edd#lib/progressbar.rb
;-)

@peleteiro
Owner

Good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 8, 2011
  1. @vicvega
  2. @vicvega

    test added

    vicvega committed
Showing with 22 additions and 12 deletions.
  1. +9 −9 lib/progressbar.rb
  2. +13 −3 test/test.rb
View
18 lib/progressbar.rb
@@ -33,12 +33,13 @@ def initialize (title, total, out = STDERR)
attr_reader :current
attr_reader :total
attr_accessor :start_time
+ attr_writer :bar_mark
private
def fmt_bar
bar_width = do_percentage * @terminal_width / 100
- sprintf("|%s%s|",
- @bar_mark * bar_width,
+ sprintf("|%s%s|",
+ @bar_mark * bar_width,
" " * (@terminal_width - bar_width))
end
@@ -51,9 +52,9 @@ def fmt_stat
end
def fmt_stat_for_file_transfer
- if @finished_p then
+ if @finished_p then
sprintf("%s %s %s", bytes, transfer_rate, elapsed)
- else
+ else
sprintf("%s %s %s", bytes, transfer_rate, eta)
end
end
@@ -106,7 +107,7 @@ def elapsed
elapsed = Time.now - @start_time
sprintf("Time: %s", format_time(elapsed))
end
-
+
def eol
if @finished_p then "\n" else "\r" end
end
@@ -137,14 +138,14 @@ def get_width
end
def show
- arguments = @format_arguments.map {|method|
+ arguments = @format_arguments.map {|method|
method = sprintf("fmt_%s", method)
send(method)
}
line = sprintf(@format, *arguments)
width = get_width
- if line.length == width - 1
+ if line.length == width - 1
@out.print(line + eol)
@out.flush
elsif line.length >= width
@@ -167,7 +168,7 @@ def show_if_needed
end
# Use "!=" instead of ">" to support negative changes
- if cur_percentage != prev_percentage ||
+ if cur_percentage != prev_percentage ||
Time.now - @previous_time >= 1 || @finished_p
show
end
@@ -233,4 +234,3 @@ def do_percentage
100 - super
end
end
-
View
16 test/test.rb
@@ -51,9 +51,9 @@ def test_inc
end
def test_inc_x
- total = File.size("../lib/progressbar.rb")
+ total = File.size("lib/progressbar.rb")
pbar = do_make_progress_bar("test(inc(x))", total)
- File.new("../lib/progressbar.rb").each {|line|
+ File.new("lib/progressbar.rb").each {|line|
sleep(SleepUnit)
pbar.inc(line.length)
}
@@ -95,6 +95,17 @@ def test_total_zero
pbar = do_make_progress_bar("test(total=0)", total)
pbar.finish
end
+
+ def test_custom_bar_mark
+ total = 100
+ pbar = do_make_progress_bar("test(custom)", total)
+ pbar.bar_mark = '='
+ total.times {
+ sleep(SleepUnit)
+ pbar.inc
+ }
+ pbar.finish
+ end
end
class ReversedProgressBarTest < ProgressBarTest
@@ -102,4 +113,3 @@ def do_make_progress_bar (title, total)
ReversedProgressBar.new(title, total)
end
end
-
Something went wrong with that request. Please try again.