Skip to content

Commit

Permalink
Merge pull request #1 from vicvega/master
Browse files Browse the repository at this point in the history
Custom bar mark
  • Loading branch information
peleteiro committed Jun 8, 2011
2 parents 3ba7eaa + e34fd9a commit 7136b84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
18 changes: 9 additions & 9 deletions lib/progressbar.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -233,4 +234,3 @@ def do_percentage
100 - super
end
end

16 changes: 13 additions & 3 deletions test/test.rb
Expand Up @@ -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)
}
Expand Down Expand Up @@ -95,11 +95,21 @@ 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
def do_make_progress_bar (title, total)
ReversedProgressBar.new(title, total)
end
end

0 comments on commit 7136b84

Please sign in to comment.