<commit_before>module Cifrado
  class Progressbar
    include Cifrado::Utils

    def initialize(segments, current_segment, options = {})
      @style = (options[:style] || :fancy).to_sym
      @segments = segments
      @current_segment = current_segment
    end

    def block
      if @style == :fancy
        fancy 
      elsif @style == :fast
        fast
      elsif @style == :infinite
        infinite
      else
        nil
      end
    end
    
    private
    def fancy
      require 'ruby-progressbar'
      title = (@segments == 1 ? \
               'Progress' : "Segment [#{@current_segment}/#{@segments}]")
      progressbar = ProgressBar.create :title => title, :total => 100,
                                       :format => '%t: |%B| %p%% [%E ]'
      read = 0
      percentage = 0
      time = Time.now.to_f
      last_time = Time.now.to_f
      Proc.new do |total, bytes| 
        read += bytes
        newt = Time.now.to_f 
        if newt - last_time > 1
          last_time = newt
          percentage = (read*100/total)
          kbs = "%0.2f" % (read*8/((Time.now.to_f - time)*1024*1024))
          progressbar.title = " [#{kbs} Mb/s] #{title}"
          progressbar.progress = percentage unless percentage > 100
        end
        progressbar.finish if (progressbar.progress < 100) and \
          read >= total
      end
    end

    def infinite
      read = 0
      time = Time.now.to_f
      Proc.new do |tbytes, bytes| 
        read += bytes
        kbs = "%0.2f" % (read*8/((Time.now.to_f - time)*1024*1024))
        print "Progress (unknown total size): #{humanize_bytes(read).ljust(10)} read (#{kbs} Mb/s)".ljust(60)
        print "\r"
      end
    end

    def fast
      title = (@segments == 1) ? ' ' : \
              " [#{@current_segment}/#{@segments}]"

      read = 0
      progressbar_finished = false
      time = Time.now.to_f
      Proc.new do |tbytes, bytes| 
        read += bytes
        percentage = ((read*100.0/tbytes))
        kbs = "%0.2f" % (read*8/((Time.now.to_f - time)*1024*1024))
        print "\r"
        print "Progress (#{percentage.round}%) #{kbs} Mb/s#{title}: "
        print "#{humanize_bytes(read)} read"
        if (read + bytes) >= tbytes and !progressbar_finished
          progressbar_finished = true
          percentage = 100
          print "\r"
          print "Progress (#{percentage.round}%) #{title}: "
          puts
        end
      end
    end

  end
end
<commit_msg>Do not show progressbar if upload/download size is 0 bytes<commit_after>module Cifrado
  class Progressbar
    include Cifrado::Utils

    def initialize(segments, current_segment, options = {})
      @style = (options[:style] || :fancy).to_sym
      @segments = segments
      @current_segment = current_segment
    end

    def block
      if @style == :fancy
        fancy 
      elsif @style == :fast
        fast
      elsif @style == :infinite
        infinite
      else
        nil
      end
    end
    
    private
    def fancy
      require 'ruby-progressbar'
      title = (@segments == 1 ? \
               'Progress' : "Segment [#{@current_segment}/#{@segments}]")
      progressbar = ProgressBar.create :title => title, :total => 100,
                                       :format => '%t: |%B| %p%% [%E ]'
      read = 0
      percentage = 0
      time = Time.now.to_f
      last_time = Time.now.to_f
      Proc.new do |total, bytes| 
        next if total == 0
        read += bytes
        newt = Time.now.to_f 
        if newt - last_time > 1
          last_time = newt
          percentage = (read*100/total)
          kbs = "%0.2f" % (read*8/((Time.now.to_f - time)*1024*1024))
          progressbar.title = " [#{kbs} Mb/s] #{title}"
          progressbar.progress = percentage unless percentage > 100
        end
        progressbar.finish if (progressbar.progress < 100) and \
          read >= total
      end
    end

    def infinite
      read = 0
      time = Time.now.to_f
      Proc.new do |tbytes, bytes| 
        read += bytes
        kbs = "%0.2f" % (read*8/((Time.now.to_f - time)*1024*1024))
        print "Progress (unknown total size): #{humanize_bytes(read).ljust(10)} read (#{kbs} Mb/s)".ljust(60)
        print "\r"
      end
    end

    def fast
      title = (@segments == 1) ? ' ' : \
              " [#{@current_segment}/#{@segments}]"

      read = 0
      progressbar_finished = false
      time = Time.now.to_f
      Proc.new do |tbytes, bytes| 
        read += bytes
        percentage = ((read*100.0/tbytes))
        kbs = "%0.2f" % (read*8/((Time.now.to_f - time)*1024*1024))
        print "\r"
        print "Progress (#{percentage.round}%) #{kbs} Mb/s#{title}: "
        print "#{humanize_bytes(read)} read"
        if (read + bytes) >= tbytes and !progressbar_finished
          progressbar_finished = true
          percentage = 100
          print "\r"
          print "Progress (#{percentage.round}%) #{title}: "
          puts
        end
      end
    end

  end
end

{'fraction_non_alphanumeric': 0.1318747490967483, 'fraction_numerical': 0.025692492974708953, 'mean_word_length': 1.771412680756396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6790428', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1569, 'n_words': 393}