Sha256: 30e61911851db5bb575cb5c4b587b293f65c5847c0ddc7dc8cd900faf90d0828

Contents?: true

Size: 1.07 KB

Versions: 32

Compression:

Stored size: 1.07 KB

Contents

module Log
  class ProgressBar
    BAR_MUTEX = Mutex.new
    BARS = []
    REMOVE = []

    def self.new_bar(max, options = {})
      cleanup_bars
      BAR_MUTEX.synchronize do
        Log::LAST.replace "new_bar" if Log::LAST == "progress"
        options = Misc.add_defaults options, :depth => BARS.length
        BARS << (bar = ProgressBar.new(max, options))
        bar
      end
    end

    def self.cleanup_bars
      BAR_MUTEX.synchronize do
        REMOVE.each do |bar|
          index = BARS.index bar
          if index
            BARS.delete_at index
            BARS.each_with_index do |bar,i|
              bar.depth = i
            end
          end
        end
        REMOVE.clear
        BARS.length
      end
    end

    def self.remove_bar(bar)
      bar.done 
      BAR_MUTEX.synchronize do
        REMOVE << bar
      end
    end

    def self.with_bar(max, options = {})
      bar = new_bar(max, options)
      begin
        yield bar
        keep = false
      rescue KeepBar
        keep = true
      ensure
        remove_bar(bar) if bar
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
rbbt-util-5.13.37 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.36 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.35 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.34 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.33 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.32 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.31 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.30 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.29 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.28 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.27 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.26 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.25 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.24 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.23 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.22 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.21 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.20 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.19 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.13.18 lib/rbbt/util/log/progress/util.rb