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

Remove dependency on colorize #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 4 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ PATH
specs:
capybara-chromedriver-logger (0.3.0)
capybara
colorize

GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
capybara (3.31.0)
capybara (3.35.3)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (0.8.0)
ffi (~> 1.0, >= 1.0.11)
coderay (1.1.2)
colorize (0.8.1)
diff-lcs (1.3)
ffi (1.12.2)
gem-release (2.0.1)
Expand All @@ -33,12 +31,12 @@ GEM
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
public_suffix (4.0.6)
rack (2.0.8)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rake (13.0.1)
regexp_parser (1.7.0)
regexp_parser (2.1.1)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
Expand Down
1 change: 0 additions & 1 deletion capybara-chromedriver-logger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "capybara"
spec.add_dependency "colorize"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "gem-release"
Expand Down
37 changes: 33 additions & 4 deletions lib/capybara/chromedriver/logger/message.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'colorize'

module Capybara
module Chromedriver
module Logger
Expand Down Expand Up @@ -33,13 +31,27 @@ def error?

private

COLOR_CODES = {
default: 49,
light_red: 101,
light_green: 102,
light_blue: 103,
light_magenta: 105,
light_cyan: 106,
}.freeze

COLORS = {
'SEVERE' => :light_red,
'INFO' => :light_green,
'WARNING' => :light_cyan,
'DEBUG' => :light_blue
}.freeze

COLOR_MODES = {
default: 0,
bold: 1,
}.freeze

LEADING_SPACES = ' ' * 5

def formatted_message
Expand All @@ -56,13 +68,20 @@ def level_color
end

def log_level
" #{level.downcase} ".colorize(color: :white, background: level_color).bold
colorize(
" #{level.downcase} ",
mode: :bold,
background: level_color,
)
end

def file_and_location
return unless file && location

"#{file} #{location}".colorize(color: :white, background: :light_magenta)
colorize(
"#{file} #{location}",
background: :light_magenta
)
end

def extract_file_and_location!
Expand All @@ -73,6 +92,16 @@ def extract_file_and_location!
_, @file, @location, message = match.to_a
@message = message.gsub(/^"(.+?)"$/, '\1')
end

def colorize(msg, background: :default, mode: :default)
mode_code = COLOR_MODES.fetch(mode)
color_code = 97 # white
background_code = COLOR_CODES.fetch(background)

str = "\033[#{mode_code};#{color_code};#{background_code}m"
str += msg
str + "\033[0m"
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/capybara/chromedriver/logger/collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
expect_to_have_inserted_element
expect { logger.flush_and_check_errors! }
.to raise_error(Capybara::Chromedriver::Logger::JsError, /A console error/)

expect_log_message("A console error")
end

Expand All @@ -52,6 +53,7 @@

expect_to_have_inserted_element
logger.flush_and_check_errors!

expect_log_message("A console log")
end

Expand Down