Skip to content

Commit

Permalink
Merge pull request #106 from DamirSvrtan/replace-colorize
Browse files Browse the repository at this point in the history
Replace colorize with a simple internal implementation
  • Loading branch information
DamirSvrtan committed Nov 13, 2023
2 parents 3bc3392 + 4152d09 commit 346e79c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
1 change: 0 additions & 1 deletion fasterer.gemspec
Expand Up @@ -20,7 +20,6 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.3'

spec.add_dependency 'colorize', '~> 0.7'
spec.add_dependency 'ruby_parser', '>= 3.19.1'

spec.add_development_dependency 'bundler', '>= 1.6'
Expand Down
16 changes: 7 additions & 9 deletions lib/fasterer/file_traverser.rb
@@ -1,9 +1,9 @@
require 'pathname'
require 'colorize'
require 'English'

require_relative 'analyzer'
require_relative 'config'
require_relative 'painter'

module Fasterer
class FileTraverser
Expand Down Expand Up @@ -83,7 +83,7 @@ def output(analyzer)
offenses_grouped_by_type(analyzer).each do |error_group_name, error_occurences|
error_occurences.map(&:line_number).each do |line|
file_and_line = "#{analyzer.file_path}:#{line}"
print "#{file_and_line.colorize(:red)} #{Fasterer::Offense::EXPLANATIONS[error_group_name]}.\n"
print "#{Painter.paint(file_and_line, :red)} #{Fasterer::Offense::EXPLANATIONS[error_group_name]}.\n"
end
end

Expand Down Expand Up @@ -112,7 +112,7 @@ def output_statistics
end

def output_unable_to_find_file(path)
puts "No such file or directory - #{path}".colorize(:red)
puts Painter.paint("No such file or directory - #{path}", :red)
end

def ignored_speedups
Expand Down Expand Up @@ -150,21 +150,19 @@ def to_s
end

def inspected_files_output
"#{@files_inspected_count} #{pluralize(@files_inspected_count, 'file')} inspected"
.colorize(:green)
Painter.paint("#{@files_inspected_count} #{pluralize(@files_inspected_count, 'file')} inspected", :green)
end

def offenses_found_output
color = @offenses_found_count.zero? ? :green : :red
"#{@offenses_found_count} #{pluralize(@offenses_found_count, 'offense')} detected"
.colorize(color)

Painter.paint("#{@offenses_found_count} #{pluralize(@offenses_found_count, 'offense')} detected", color)
end

def unparsable_files_output
return if @unparsable_files_count.zero?

"#{@unparsable_files_count} unparsable #{pluralize(@unparsable_files_count, 'file')} found"
.colorize(:red)
Painter.paint("#{@unparsable_files_count} unparsable #{pluralize(@unparsable_files_count, 'file')} found", :red)
end

def pluralize(n, singular, plural = nil)
Expand Down
20 changes: 20 additions & 0 deletions lib/fasterer/painter.rb
@@ -0,0 +1,20 @@
module Fasterer
module Painter
COLOR_CODES = {
red: 31,
green: 32,
}

def self.paint(string, color)
color_code = COLOR_CODES[color.to_sym]
if color_code.nil?
raise ArgumentError, "Color #{color} is not supported. Allowed colors are #{COLOR_CODES.keys.join(', ')}"
end
paint_with_code(string, color_code)
end

def self.paint_with_code(string, color_code)
"\e[#{color_code}m#{string}\e[0m"
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/fasterer/file_traverser_spec.rb
Expand Up @@ -348,7 +348,7 @@
let(:explanation) { Fasterer::Offense::EXPLANATIONS[:for_loop_vs_each] }

it 'should print offense' do
match = "\e[0;31;49m#{test_file_path}:1\e[0m #{explanation}.\n\n"
match = "\e[31m#{test_file_path}:1\e[0m #{explanation}.\n\n"

expect { file_traverser.send(:output, analyzer) }.to output(match).to_stdout
end
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/fasterer/statistics_spec.rb
@@ -0,0 +1,20 @@
require 'spec_helper'

describe Fasterer::Statistics do
let(:traverser_mock) do
traverser = OpenStruct.new
traverser.scannable_files = []
traverser.offenses_total_count = 0
traverser.parse_error_paths = []
traverser
end

let(:statistics) { Fasterer::Statistics.new(traverser_mock) }

describe 'inspected_files_output' do
it 'should be green' do
expect(statistics.inspected_files_output)
.to eq("\e[32m0 files inspected\e[0m")
end
end
end

0 comments on commit 346e79c

Please sign in to comment.