Skip to content

Commit

Permalink
Add progress bar to offense formatter (#5675)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewpterry authored and bbatsov committed Mar 18, 2018
1 parent f7f2104 commit 3f81a25
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* [#5637](https://github.com/bbatsov/rubocop/issues/5637): Fix error for `Layout/SpaceInsideArrayLiteralBrackets` when contains an array literal as an argument after a heredoc is started. ([@koic][])
* [#5498](https://github.com/bbatsov/rubocop/issues/5498): Correct IndentHeredoc message for Ruby 2.3 when using `<<~` operator with invalid indentation. ([@hamada14][])
* [#5610](https://github.com/bbatsov/rubocop/issues/5610): Use `gems.locked` or `Gemfile.lock` to determine the best `TargetRubyVersion` when it is not specified in the config. ([@roberts1000][])
* Add progress bar to offenses formatter. ([@drewpterry][])

## 0.53.0 (2018-03-05)

Expand Down Expand Up @@ -3265,3 +3266,4 @@
[@anthony-robin]: https://github.com/anthony-robin
[@YukiJikumaru]: https://github.com/YukiJikumaru
[@jlfaber]: https://github.com/jlfaber
[@drewpterry]: https://github.com/drewpterry
17 changes: 17 additions & 0 deletions lib/rubocop/formatter/offense_count_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@ class OffenseCountFormatter < BaseFormatter
def started(target_files)
super
@offense_counts = Hash.new(0)

return unless output.tty?

file_phrase = target_files.count == 1 ? 'file' : 'files'

# 185/407 files |====== 45 ======> | ETA: 00:00:04
# %c / %C | %w > %i | %e
bar_format = " %c/%C #{file_phrase} |%w>%i| %e "

@progressbar = ProgressBar.create(
output: output,
total: target_files.count,
format: bar_format,
autostart: false
)
@progressbar.start
end

def file_finished(_file, offenses)
offenses.each { |o| @offense_counts[o.cop_name] += 1 }
@progressbar.increment if instance_variable_defined?(:@progressbar)
end

def finished(_inspected_files)
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/formatter/offense_count_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
end

before do
allow(output).to receive(:tty?).and_return(false)
formatter.started(files)
finish
end
Expand All @@ -70,5 +71,22 @@
OUTPUT
end
end

context 'when output tty is true' do
let(:offenses) do
%w[CopB CopA CopC CopC].map { |c| double('offense', cop_name: c) }
end

before do
allow(output).to receive(:tty?).and_return(true)
formatter.started(files)
finish
end

it 'has a progresbar' do
formatter.finished(files)
expect(formatter.instance_variable_get(:@progressbar).progress).to eq 1
end
end
end
end

0 comments on commit 3f81a25

Please sign in to comment.