Class: Dratools::ProgressReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/dratools/progress_reporter.rb

Overview

対話的な端末のときだけ stderr へ一行進捗を表示する軽量レポーター。 データは stdout に出るため、stdout をパイプしても進捗は混ざらない。 非 TTY(リダイレクト・パイプ・CI)では制御文字を出さず完全に無音にする。

Defined Under Namespace

Classes: ClearingIO

Constant Summary collapse

CLEAR_LINE =
"\r\e[K"

Instance Method Summary collapse

Constructor Details

#initialize(io: $stderr, enabled: nil) ⇒ ProgressReporter

Returns a new instance of ProgressReporter.



51
52
53
54
55
56
# File 'lib/dratools/progress_reporter.rb', line 51

def initialize(io: $stderr, enabled: nil)
  @io = io
  @enabled = enabled.nil? ? interactive?(io) : enabled
  @count = 0
  @active = false
end

Instance Method Details

#clearing_io(io = @io) ⇒ Object



58
59
60
# File 'lib/dratools/progress_reporter.rb', line 58

def clearing_io(io = @io)
  ClearingIO.new(io, self)
end

#finishObject

残っている進捗行を消す。コマンド終了時(成功・失敗どちらでも)に呼ぶ。



73
74
75
76
77
78
79
# File 'lib/dratools/progress_reporter.rb', line 73

def finish
  return unless @active

  @io.print(CLEAR_LINE)
  @io.flush
  @active = false
end

#report(label) ⇒ Object

1 件の進捗を表示する。直前の行を消してから上書きする。



63
64
65
66
67
68
69
70
# File 'lib/dratools/progress_reporter.rb', line 63

def report(label)
  return unless @enabled

  @count += 1
  @io.print("#{CLEAR_LINE}#{label} (#{@count})")
  @io.flush
  @active = true
end