Skip to content

Repository files navigation

Gem VersionCheckRubocopDepfuInch CIZizmor

progress

Show progress during console script run.

Installation

gem install progress

Usage

1000.times_with_progress('Counting to 1000')do |i|
# do something with iend

or without title:

1000.times_with_progressdo |i|
# do something with iend

With 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|
# =||=end

Nested 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 cendendend

You can also show note:

[1,2,3].with_progressdo |i|
Progress.note=isleep5end

You 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 pathend

NOTE: 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.lengthendend

or just

symbols=[]Progress('Input 100 symbols',100)dowhilesymbols.length < 100input=gets.scan(/\S/)symbols += inputProgress.stepinput.lengthendend

NOTE: 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# codeendend

But 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# codeendendend

Or 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# codeendendend

Copyright

Copyright (c) 2008-2021 Ivan Kuchin. See LICENSE.txt for details.

About

Class to show progress during script run

Topics

Resources

Stars

68 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages