1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Fix some Rubocop/Codeclimate issues

This commit is contained in:
Gerard Caulfield 2016-07-05 16:46:14 +10:00
parent 83b0ca63e0
commit b25b16484e
No known key found for this signature in database
GPG key ID: 623B327128A9BEC3
3 changed files with 86 additions and 67 deletions

View file

@ -33,35 +33,35 @@ ap object, options = {}
Default options: Default options:
```ruby ```ruby
:indent => 4, # Indent using 4 spaces. indent: 4, # Number of spaces for indenting.
:index => true, # Display array indices. index: true, # Display array indices.
:html => false, # Use ANSI color codes rather than HTML. html: false, # Use ANSI color codes rather than HTML.
:multiline => true, # Display in multiple lines. multiline: true, # Display in multiple lines.
:plain => false, # Use colors. plain: false, # Use colors.
:raw => false, # Do not recursively format object instance variables. raw: false, # Do not recursively format instance variables.
:sort_keys => false, # Do not sort hash keys. sort_keys: false, # Do not sort hash keys.
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. limit: false, # Limit arrays & hashes. Accepts bool or int.
:new_hash_syntax => false, # Use the JSON like syntax { foo: 'bar' }, when the key is a symbol new_hash_syntax: false, # Use Ruby 1.9 hash syntax in output.
:color => { color: {
:args => :pale, args: :pale,
:array => :white, array: :white,
:bigdecimal => :blue, bigdecimal: :blue,
:class => :yellow, class: :yellow,
:date => :greenish, date: :greenish,
:falseclass => :red, falseclass: :red,
:fixnum => :blue, fixnum: :blue,
:float => :blue, float: :blue,
:hash => :pale, hash: :pale,
:keyword => :cyan, keyword: :cyan,
:method => :purpleish, method: :purpleish,
:nilclass => :red, nilclass: :red,
:rational => :blue, rational: :blue,
:string => :yellowish, string: :yellowish,
:struct => :pale, struct: :pale,
:symbol => :cyanish, symbol: :cyanish,
:time => :greenish, time: :greenish,
:trueclass => :green, trueclass: :green,
:variable => :cyanish variable: :cyanish
} }
``` ```

View file

@ -4,17 +4,17 @@ module AwesomePrint
# Class accessor to force colorized output (ex. forked subprocess where TERM # Class accessor to force colorized output (ex. forked subprocess where TERM
# might be dumb). # might be dumb).
#------------------------------------------------------------------------------ #---------------------------------------------------------------------------
def force_colors!(value = true) def force_colors!(value = true)
@force_colors = value @force_colors = value
end end
def console? def console?
!!(defined?(IRB) || defined?(Pry)) boolean(defined?(IRB) || defined?(Pry))
end end
def rails_console? def rails_console?
console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) console? && boolean(defined?(Rails::Console) || ENV['RAILS_ENV'])
end end
def diet_rb def diet_rb
@ -29,7 +29,7 @@ module AwesomePrint
IRB::Irb.class_eval do IRB::Irb.class_eval do
def output_value def output_value
ap @context.last_value ap @context.last_value
rescue NoMethodError rescue NoMethodError
puts "(Object doesn't support #ai)" puts "(Object doesn't support #ai)"
end end
end end
@ -38,11 +38,20 @@ module AwesomePrint
def irb! def irb!
return unless defined?(IRB) return unless defined?(IRB)
IRB.version.include?("DietRB") ? diet_rb : usual_rb IRB.version.include?('DietRB') ? diet_rb : usual_rb
end end
def pry! def pry!
Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
end end
private
# Takes a value and returns true unless it is false or nil
# This is an alternative to the less readable !!(value)
# https://github.com/bbatsov/ruby-style-guide#no-bang-bang
def boolean(value)
value ? true : false
end
end end
end end

View file

@ -13,35 +13,35 @@ module AwesomePrint
def initialize(options = {}) def initialize(options = {})
@options = { @options = {
:indent => 4, # Indent using 4 spaces. indent: 4, # Number of spaces for indenting.
:index => true, # Display array indices. index: true, # Display array indices.
:html => false, # Use ANSI color codes rather than HTML. html: false, # Use ANSI color codes rather than HTML.
:multiline => true, # Display in multiple lines. multiline: true, # Display in multiple lines.
:plain => false, # Use colors. plain: false, # Use colors.
:raw => false, # Do not recursively format object instance variables. raw: false, # Do not recursively format instance variables.
:sort_keys => false, # Do not sort hash keys. sort_keys: false, # Do not sort hash keys.
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. limit: false, # Limit arrays & hashes. Accepts bool or int.
:new_hash_syntax => false, # Use the JSON like syntax { foo: 'bar' }, when the key is a symbol new_hash_syntax: false, # Use Ruby 1.9 hash syntax in output.
:color => { color: {
:args => :pale, args: :pale,
:array => :white, array: :white,
:bigdecimal => :blue, bigdecimal: :blue,
:class => :yellow, class: :yellow,
:date => :greenish, date: :greenish,
:falseclass => :red, falseclass: :red,
:fixnum => :blue, fixnum: :blue,
:float => :blue, float: :blue,
:hash => :pale, hash: :pale,
:keyword => :cyan, keyword: :cyan,
:method => :purpleish, method: :purpleish,
:nilclass => :red, nilclass: :red,
:rational => :blue, rational: :blue,
:string => :yellowish, string: :yellowish,
:struct => :pale, struct: :pale,
:symbol => :cyanish, symbol: :cyanish,
:time => :greenish, time: :greenish,
:trueclass => :green, trueclass: :green,
:variable => :cyanish variable: :cyanish
} }
} }
@ -81,7 +81,15 @@ module AwesomePrint
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def colorize? def colorize?
AwesomePrint.force_colors ||= false AwesomePrint.force_colors ||= false
AwesomePrint.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON'])) AwesomePrint.force_colors || (
STDOUT.tty? && (
(
ENV['TERM'] &&
ENV['TERM'] != 'dumb'
) ||
ENV['ANSICON']
)
)
end end
private private
@ -106,8 +114,9 @@ module AwesomePrint
@formatter.format(object, printable(object)) @formatter.format(object, printable(object))
end end
# Turn class name into symbol, ex: Hello::World => :hello_world. Classes that # Turn class name into symbol, ex: Hello::World => :hello_world. Classes
# inherit from Array, Hash, File, Dir, and Struct are treated as the base class. # that inherit from Array, Hash, File, Dir, and Struct are treated as the
# base class.
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def printable(object) def printable(object)
case object case object
@ -120,7 +129,8 @@ module AwesomePrint
end end
end end
# Update @options by first merging the :color hash and then the remaining keys. # Update @options by first merging the :color hash and then the remaining
# keys.
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def merge_options!(options = {}) def merge_options!(options = {})
@options[:color].merge!(options.delete(:color) || {}) @options[:color].merge!(options.delete(:color) || {})