Permalink
Browse files

vendor progress

  • Loading branch information...
1 parent 27b04f1 commit 62ce00964349d34c099349ab0f0e7b7faae8b333 @ddollar committed Aug 21, 2012
View
@@ -5,6 +5,7 @@ task :revendor do
vendor "anvil", "https://github.com/ddollar/anvil-cli.git"
vendor "distributor", "https://github.com/ddollar/distributor.git"
vendor "listen", "https://github.com/guard/listen.git"
+ vendor "progress", "https://github.com/toy/progress.git"
end
def root
View
@@ -0,0 +1,12 @@
+/pkg/
+/*.gem
+
+/doc/
+/rdoc/
+/.yardoc/
+/coverage/
+
+Makefile
+*.o
+*.bundle
+/tmp/
@@ -0,0 +1,20 @@
+Copyright (c) 2010-2011 Ivan Kuchin
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,134 @@
+# progress
+
+Show progress during console script run.
+
+## Installation
+
+ gem install progress
+
+## Usage
+
+ 1000.times_with_progress('Counting to 1000') do |i|
+ # do something with i
+ end
+
+or without title:
+
+ 1000.times_with_progress do |i|
+ # do something with i
+ end
+
+With array:
+
+ [1, 2, 3].with_progress('1…2…3').each do |i|
+ # still counting
+ end
+
+`.each` is optional:
+
+ [1, 2, 3].with_progress('1…2…3') do |i|
+ # =||=
+ end
+
+Nested progress
+
+ (1..10).with_progress('Outer').map do |a|
+ (1..10).with_progress('Middle').map do |b|
+ (1..10).with_progress('Inner').map do |c|
+ # do something with a, b and c
+ end
+ end
+ end
+
+You can also show note:
+
+ [1, 2, 3].with_progress do |i|
+ Progress.note = i
+ sleep 5
+ end
+
+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 good
+ end
+
+ Dir.new('.').with_progress do |path|
+ # check path
+ end
+
+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) do
+ while symbols.length < 100
+ input = gets.scan(/\S/)
+ symbols += input
+ Progress.step input.length
+ end
+ end
+
+or just
+
+ symbols = []
+ Progress('Input 100 symbols', 100) do
+ while symbols.length < 100
+ input = gets.scan(/\S/)
+ symbols += input
+ Progress.step input.length
+ end
+ end
+
+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
+ # code
+ end
+ 10.times_with_progress('C') do
+ # code
+ end
+ end
+
+But you can use this:
+
+ 10.times_with_progress('A') do |time|
+ Progress.step 1, 2 do
+ 10.times_with_progress('B') do
+ # code
+ end
+ end
+ Progress.step 1, 2 do
+ 10.times_with_progress('C') do
+ # code
+ end
+ end
+ end
+
+Or if you know that B runs 10 times faster than C:
+
+ 10.times_with_progress('A') do |time|
+ Progress.step 1, 11 do
+ 10.times_with_progress('B') do
+ # code
+ end
+ end
+ Progress.step 10, 11 do
+ 10.times_with_progress('C') do
+ # code
+ end
+ end
+ end
+
+## Copyright
+
+Copyright (c) 2010-2011 Ivan Kuchin. See LICENSE.txt for details.
Oops, something went wrong.

0 comments on commit 62ce009

Please sign in to comment.