Sha256: e1cf6bb83a482dc875868657c65368ed7e3569e9c2cc35c51e7880c473a74d8d

Contents?: true

Size: 1.65 KB

Versions: 12

Compression:

Stored size: 1.65 KB

Contents

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

    def self.add_offset
      @@offset = offset + 1
      @@offset = 0 if @@offset < 0
      @@offset
    end

    def self.remove_offset
      @@offset = offset - 1
      @@offset = 0 if @@offset < 0
      @@offset
    end


    def self.offset
      @@offset ||= 0
      @@offset = 0 if @@offset < 0
      @@offset
    end

    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 + Log::ProgressBar.offset
        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
          index = SILENCED.index bar
          if index
            SILENCED.delete_at index
            SILENCED.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 if bar.respond_to? :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

12 entries across 12 versions & 1 rubygems

Version Path
rbbt-util-5.20.14 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.13 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.12 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.11 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.10 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.9 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.8 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.7 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.6 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.5 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.4 lib/rbbt/util/log/progress/util.rb
rbbt-util-5.20.3 lib/rbbt/util/log/progress/util.rb