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

Merge pull request #256 from gerrywastaken/quick-ruby-style-guide-adjustments

Quick ruby style guide adjustments
This commit is contained in:
Gerry 2016-07-05 09:57:32 +10:00 committed by GitHub
commit b9fdc58da2
3 changed files with 41 additions and 41 deletions

3
.gitignore vendored
View file

@ -23,6 +23,9 @@ pkg
.ruby-version
gemfiles/*.gemfile.lock
Gemfile.lock
/tmp
/.bundle
/gemfiles/.bundle
## PROJECT::RVM
.rvmrc

View file

@ -3,16 +3,15 @@
# Awesome Print is freely distributable under the terms of MIT license.
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
require_relative "indentator"
require_relative 'indentator'
module AwesomePrint
class << self # Class accessors for custom defaults.
attr_accessor :defaults, :force_colors
# Class accessor to force colorized output (ex. forked subprocess where TERM
# might be dumb).
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def force_colors!(value = true)
@force_colors = value
end
@ -22,12 +21,12 @@ module AwesomePrint
end
def rails_console?
console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"])
console? && !!(defined?(Rails::Console) || ENV['RAILS_ENV'])
end
def irb!
return unless defined?(IRB)
unless IRB.version.include?("DietRB")
unless IRB.version.include?('DietRB')
IRB::Irb.class_eval do
def output_value
ap @context.last_value
@ -45,9 +44,7 @@ module AwesomePrint
end
def pry!
if defined?(Pry)
Pry.print = proc { |output, value| output.puts value.ai }
end
Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
end
end
@ -108,7 +105,7 @@ module AwesomePrint
end
# Dispatcher that detects data nesting and invokes object-aware formatter.
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def awesome(object)
if Thread.current[AP].include?(object.object_id)
nested(object)
@ -123,7 +120,7 @@ module AwesomePrint
end
# Return true if we are to colorize the output.
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def colorize?
AwesomePrint.force_colors ||= false
AwesomePrint.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
@ -136,24 +133,24 @@ module AwesomePrint
# => [1,2, [...]]
# hash = { :a => 1 }; hash[:b] = hash
# => { :a => 1, :b => {...} }
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def nested(object)
case printable(object)
when :array then @formatter.colorize("[...]", :array)
when :hash then @formatter.colorize("{...}", :hash)
when :struct then @formatter.colorize("{...}", :struct)
else @formatter.colorize("...#{object.class}...", :class)
when :array then @formatter.colorize('[...]', :array)
when :hash then @formatter.colorize('{...}', :hash)
when :struct then @formatter.colorize('{...}', :struct)
else @formatter.colorize("...#{object.class}...", :class)
end
end
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def unnested(object)
@formatter.format(object, printable(object))
end
# Turn class name into symbol, ex: Hello::World => :hello_world. Classes that
# inherit from Array, Hash, File, Dir, and Struct are treated as the base class.
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def printable(object)
case object
when Array then :array
@ -161,19 +158,19 @@ module AwesomePrint
when File then :file
when Dir then :dir
when Struct then :struct
else object.class.to_s.gsub(/:+/, "_").downcase.to_sym
else object.class.to_s.gsub(/:+/, '_').downcase.to_sym
end
end
# Update @options by first merging the :color hash and then the remaining keys.
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def merge_options!(options = {})
@options[:color].merge!(options.delete(:color) || {})
@options.merge!(options)
end
# Load ~/.aprc file with custom defaults that override default options.
#------------------------------------------------------------------------------
#---------------------------------------------------------------------------
def merge_custom_defaults!
dotfile = File.join(ENV['HOME'], '.aprc')
load dotfile if File.readable?(dotfile)

View file

@ -96,24 +96,24 @@ RSpec.describe "AwesomePrint" do
it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
markup = [ 1, :two, "three" ]
expect(markup.ai(:html => true)).to eq <<-EOS.strip
<pre>[
<kbd style="color:white">[0] </kbd><kbd style="color:blue">1</kbd>,
<kbd style="color:white">[1] </kbd><kbd style="color:darkcyan">:two</kbd>,
<kbd style="color:white">[2] </kbd><kbd style="color:brown">&quot;three&quot;</kbd>
]</pre>
EOS
expect(markup.ai(:html => true)).to eq <<-EOS.strip_heredoc.strip
<pre>[
<kbd style="color:white">[0] </kbd><kbd style="color:blue">1</kbd>,
<kbd style="color:white">[1] </kbd><kbd style="color:darkcyan">:two</kbd>,
<kbd style="color:white">[2] </kbd><kbd style="color:brown">&quot;three&quot;</kbd>
]</pre>
EOS
end
it "wraps hash ap output with only an outer <pre> tag" do
markup = [ { "hello" => "world" } ]
expect(markup.ai(:html => true)).to eq <<-EOS.strip
<pre>[
<kbd style="color:white">[0] </kbd>{
&quot;hello&quot;<kbd style="color:slategray"> =&gt; </kbd><kbd style="color:brown">&quot;world&quot;</kbd>
}
]</pre>
EOS
expect(markup.ai(:html => true)).to eq <<-EOS.strip_heredoc.strip
<pre>[
<kbd style="color:white">[0] </kbd>{
&quot;hello&quot;<kbd style="color:slategray"> =&gt; </kbd><kbd style="color:brown">&quot;world&quot;</kbd>
}
]</pre>
EOS
end
it "encodes HTML entities (plain)" do
@ -142,13 +142,13 @@ EOS
AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
out = hash.ai(:plain => true)
expect(out).to eq <<-EOS.strip
{
[ 0, 0, 255 ] => :yellow,
"magenta" => "rgb(255, 0, 255)",
:red => "rgb(255, 0, 0)"
}
EOS
expect(out).to eq <<-EOS.strip_heredoc.strip
{
[ 0, 0, 255 ] => :yellow,
"magenta" => "rgb(255, 0, 255)",
:red => "rgb(255, 0, 0)"
}
EOS
end
end