Show progress during console script run.
gem install progress
1000.times_with_progress('Counting to 1000')do |i|
# do something with iendor without title:
1000.times_with_progressdo |i|
# do something with iendWith array:
[1,2,3].with_progress('1…2…3').eachdo |i|
# still countingend.each is optional:
[1,2,3].with_progress('1…2…3')do |i|
# =||=endNested progress
(1..10).with_progress('Outer').mapdo |a|
(1..10).with_progress('Middle').mapdo |b|
(1..10).with_progress('Inner').mapdo |c|
# do something with a, b and cendendendYou can also show note:
[1,2,3].with_progressdo |i|
Progress.note=isleep5endYou can use any enumerable method:
[1,2,3].with_progress.map{ |i| 'do stuff'}[1,2,3].with_progress.each_cons(3){ |i| 'do stuff'}[1,2,3].with_progress.each_slice(2){ |i| 'do stuff'}# …Any enumerable will work:
(1..100).with_progress('Wait')do |i|
# ranges are goodendDir.new('.').with_progressdo |path|
# check pathendNOTE: progress gets number of objects using length, size, to_a.length or just inject and if used on objects which needs rewind (like opened File), cycle itself will not work.
Use simple blocks:
symbols=[]Progress.start('Input 100 symbols',100)dowhilesymbols.length < 100input=gets.scan(/\S/)symbols += inputProgress.stepinput.lengthendendor just
symbols=[]Progress('Input 100 symbols',100)dowhilesymbols.length < 100input=gets.scan(/\S/)symbols += inputProgress.stepinput.lengthendendNOTE: you will get WRONG progress if you use something like this:
10.times_with_progress('A')do |time|
10.times_with_progress('B')do# codeend10.times_with_progress('C')do# codeendendBut you can use this:
10.times_with_progress('A')do |time|
Progress.step5do10.times_with_progress('B')do# codeendendProgress.step5do10.times_with_progress('C')do# codeendendendOr if you know that B runs 9 times faster than C:
10.times_with_progress('A')do |time|
Progress.step1do10.times_with_progress('B')do# codeendendProgress.step9do10.times_with_progress('C')do# codeendendendCopyright (c) 2008-2021 Ivan Kuchin. See LICENSE.txt for details.