Module: RunKit::CoreExt::Enumerator

Defined in:
lib/run_kit/core_ext.rb

Overview

each.with_progresbar

Instance Method Summary collapse

Instance Method Details

#with_progressbar(options = {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/run_kit/core_ext.rb', line 36

def with_progressbar(options = {}, &block)
  defaults = {
    format: "%j%% %B #{RunKit::Term.paint_ansi("%c/%u %e", RunKit::Term.ansi256_fg(242))}",
    progress_mark: RunKit::Term.paint_ansi("", RunKit::Term.ansi256_fg(46)),
    remainder_mark: RunKit::Term.paint_ansi("", RunKit::Term.ansi256_fg(237)),
    output: $stdout.isatty ? $stdout : $stderr,
    total: size,
    length: 72,
  }
  options = defaults.merge(options)

  return enum_for(__method__) if !block

  if !options[:hide]
    bar = ProgressBar.create(options)
  end
  RunKit::Term.with_hidden_cursor(options[:output]) do
    each do
      bar&.increment
      yield(_1)
    end
  end
end