Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace colorize with a simple internal implementation #106

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion fasterer.gemspec
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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