<commit_before>class ProgressBar
  def initialize(caption)
    @cur = 0
    @width = 40
    print caption
    print("[" + " "*40 + "]" + "\x08"*41)
    $stdout.flush
  end

  def fill_to(pos)
    dots = pos - @cur
    print "."*dots
    $stdout.flush
    @cur = pos
  end

  def update(cur, max)
    fill_to(@width*cur/max)
  end

  def finish(error=false)
    return unless @cur >= 0
    fill_to(@width) unless error
    puts
    @cur = -1
  end

  def self.with_progress(caption)
    bar = ProgressBar.new(caption)
    begin
      yield bar
    rescue => e
      bar.finish(true)
      raise e
    else
      bar.finish(false)
    end
  end
end

# vim: ts=2 sw=2 et :<commit_msg>Properly handle progressbar with zero max.  (Occurs when you've already synced all available data.)<commit_after>class ProgressBar
  def initialize(caption)
    @cur = 0
    @width = 40
    print caption
    print("[" + " "*40 + "]" + "\x08"*41)
    $stdout.flush
  end

  def fill_to(pos)
    dots = pos - @cur
    print "."*dots
    $stdout.flush
    @cur = pos
  end

  def update(cur, max)
    if max > 0
      fill_to(@width*cur/max)
    else
      fill_to(@width)
    end
  end

  def finish(error=false)
    return unless @cur >= 0
    fill_to(@width) unless error
    puts
    @cur = -1
  end

  def self.with_progress(caption)
    bar = ProgressBar.new(caption)
    begin
      yield bar
    rescue => e
      bar.finish(true)
      raise e
    else
      bar.finish(false)
    end
  end
end

# vim: ts=2 sw=2 et :
{'fraction_non_alphanumeric': 0.11502659574468085, 'fraction_numerical': 0.01795212765957447, 'mean_word_length': 2.161764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5178273', 'n_tokens_mistral': 564, 'n_tokens_neox': 535, 'n_words': 149}