Skip to content

Commit

Permalink
Run c5-conventions/rubocop on this project
Browse files Browse the repository at this point in the history
Problem
-------

Shouldn't we eat our own dog food?

This repo was way out of sync with our current rubocop conventions.

Solution
--------

Bring it up to speed.

Because we don't have `rubocop` in the project, this can be seen as
a one-time cleanup.  We can decide later if we want it more persistent.
But for now I did the following:

* Copy `.rubocop.yml` from `carbonfive/c5-conventions`
  ```bash
  curl https://raw.githubusercontent.com/carbonfive/c5-conventions/master/rubocop/rubocop.yml > .rubocop.yml
  ```
* Locally add `rubocop` and `rubocop-performance` gems
  ```bash
  gem install rubocop
  gem install rubocop-performance

  ```
* run it
  ```bash
  rubocop -a
  ```
  • Loading branch information
Jon Rogers committed Jan 24, 2020
1 parent e352410 commit 7c0aeca
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in raygun.gemspec
gemspec
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'bundler/gem_tasks'
require "bundler/gem_tasks"
94 changes: 45 additions & 49 deletions lib/colorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,54 @@
# want to add a gem dependency.

class String

#
# Colors Hash
#
COLORS = {
:black => 0,
:red => 1,
:green => 2,
:yellow => 3,
:blue => 4,
:magenta => 5,
:cyan => 6,
:white => 7,
:default => 9,

:light_black => 10,
:light_red => 11,
:light_green => 12,
:light_yellow => 13,
:light_blue => 14,
:light_magenta => 15,
:light_cyan => 16,
:light_white => 17
black: 0,
red: 1,
green: 2,
yellow: 3,
blue: 4,
magenta: 5,
cyan: 6,
white: 7,
default: 9,

light_black: 10,
light_red: 11,
light_green: 12,
light_yellow: 13,
light_blue: 14,
light_magenta: 15,
light_cyan: 16,
light_white: 17
}

#
# Modes Hash
#
MODES = {
:default => 0, # Turn off all attributes
default: 0, # Turn off all attributes
#:bright => 1, # Set bright mode
:underline => 4, # Set underline mode
:blink => 5, # Set blink mode
:swap => 7, # Exchange foreground and background colors
:hide => 8 # Hide text (foreground color would be the same as background)
underline: 4, # Set underline mode
blink: 5, # Set blink mode
swap: 7, # Exchange foreground and background colors
hide: 8 # Hide text (foreground color would be the same as background)
}

protected

#
# Set color values in new string intance
#
def set_color_parameters( params )
if (params.instance_of?(Hash))
def set_color_parameters(params)
if params.instance_of?(Hash)
@color = params[:color]
@background = params[:background]
@mode = params[:mode]
@uncolorized = params[:uncolorized]
self
else
nil
end
end

Expand All @@ -77,37 +74,37 @@ def set_color_parameters( params )
# puts "This is blue text on red".blue.on_red.blink
# puts "This is uncolorized".blue.on_red.uncolorize
#
def colorize( params )
def colorize(params)
return self unless STDOUT.isatty

begin
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
require "Win32/Console/ANSI" if RUBY_PLATFORM.match?(/win32/)
rescue LoadError
raise 'You must gem install win32console to use colorize on Windows'
raise "You must gem install win32console to use colorize on Windows"
end

color_parameters = {}

if (params.instance_of?(Hash))
if params.instance_of?(Hash)
color_parameters[:color] = COLORS[params[:color]]
color_parameters[:background] = COLORS[params[:background]]
color_parameters[:mode] = MODES[params[:mode]]
elsif (params.instance_of?(Symbol))
elsif params.instance_of?(Symbol)
color_parameters[:color] = COLORS[params]
end

color_parameters[:color] ||= @color ||= COLORS[:default]
color_parameters[:background] ||= @background ||= COLORS[:default]
color_parameters[:mode] ||= @mode ||= MODES[:default]

color_parameters[:uncolorized] ||= @uncolorized ||= self.dup
color_parameters[:uncolorized] ||= @uncolorized ||= dup

# calculate bright mode
color_parameters[:color] += 50 if color_parameters[:color] > 10

color_parameters[:background] += 50 if color_parameters[:background] > 10

"\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
"\033[#{color_parameters[:mode]};#{color_parameters[:color] + 30};#{color_parameters[:background] + 40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters(color_parameters)
end

#
Expand All @@ -127,37 +124,36 @@ def colorized?
#
# Make some color and on_color methods
#
COLORS.each_key do | key |
COLORS.each_key do |key|
next if key == :default

define_method key do
self.colorize( :color => key )
colorize(color: key)
end

define_method "on_#{key}" do
self.colorize( :background => key )
colorize(background: key)
end
end

#
# Methods for modes
#
MODES.each_key do | key |
MODES.each_key do |key|
next if key == :default

define_method key do
self.colorize( :mode => key )
colorize(mode: key)
end
end

class << self

#
# Return array of available modes used by colorize method
#
def modes
keys = []
MODES.each_key do | key |
MODES.each_key do |key|
keys << key
end
keys
Expand All @@ -168,7 +164,7 @@ def modes
#
def colors
keys = []
COLORS.each_key do | key |
COLORS.each_key do |key|
keys << key
end
keys
Expand All @@ -177,16 +173,16 @@ def colors
#
# Display color matrix with color names.
#
def color_matrix( txt = "[X]" )
def color_matrix(txt = "[X]")
size = String.colors.length
String.colors.each do | color |
String.colors.each do | back |
print txt.colorize( :color => color, :background => back )
String.colors.each do |color|
String.colors.each do |back|
print txt.colorize(color: color, background: back)
end
puts " < #{color}"
end
String.colors.reverse.each_with_index do | back, index |
puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
String.colors.reverse.each_with_index do |back, index|
puts "#{"|".rjust(txt.length) * (size - index)} < #{back}"
end
""
end
Expand Down
Loading

0 comments on commit 7c0aeca

Please sign in to comment.