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:
commit
b9fdc58da2
3 changed files with 41 additions and 41 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -23,6 +23,9 @@ pkg
|
||||||
.ruby-version
|
.ruby-version
|
||||||
gemfiles/*.gemfile.lock
|
gemfiles/*.gemfile.lock
|
||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
|
/tmp
|
||||||
|
/.bundle
|
||||||
|
/gemfiles/.bundle
|
||||||
|
|
||||||
## PROJECT::RVM
|
## PROJECT::RVM
|
||||||
.rvmrc
|
.rvmrc
|
||||||
|
|
|
@ -3,16 +3,15 @@
|
||||||
# Awesome Print is freely distributable under the terms of MIT license.
|
# Awesome Print is freely distributable under the terms of MIT license.
|
||||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
require_relative "indentator"
|
require_relative 'indentator'
|
||||||
|
|
||||||
module AwesomePrint
|
module AwesomePrint
|
||||||
|
|
||||||
class << self # Class accessors for custom defaults.
|
class << self # Class accessors for custom defaults.
|
||||||
attr_accessor :defaults, :force_colors
|
attr_accessor :defaults, :force_colors
|
||||||
|
|
||||||
# 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
|
||||||
|
@ -22,12 +21,12 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
|
|
||||||
def rails_console?
|
def rails_console?
|
||||||
console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"])
|
console? && !!(defined?(Rails::Console) || ENV['RAILS_ENV'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def irb!
|
def irb!
|
||||||
return unless defined?(IRB)
|
return unless defined?(IRB)
|
||||||
unless IRB.version.include?("DietRB")
|
unless IRB.version.include?('DietRB')
|
||||||
IRB::Irb.class_eval do
|
IRB::Irb.class_eval do
|
||||||
def output_value
|
def output_value
|
||||||
ap @context.last_value
|
ap @context.last_value
|
||||||
|
@ -45,9 +44,7 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
|
|
||||||
def pry!
|
def pry!
|
||||||
if defined?(Pry)
|
Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
|
||||||
Pry.print = proc { |output, value| output.puts value.ai }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -108,7 +105,7 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
|
|
||||||
# Dispatcher that detects data nesting and invokes object-aware formatter.
|
# Dispatcher that detects data nesting and invokes object-aware formatter.
|
||||||
#------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
def awesome(object)
|
def awesome(object)
|
||||||
if Thread.current[AP].include?(object.object_id)
|
if Thread.current[AP].include?(object.object_id)
|
||||||
nested(object)
|
nested(object)
|
||||||
|
@ -123,7 +120,7 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return true if we are to colorize the output.
|
# Return true if we are to colorize the output.
|
||||||
#------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
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']))
|
||||||
|
@ -136,24 +133,24 @@ module AwesomePrint
|
||||||
# => [1,2, [...]]
|
# => [1,2, [...]]
|
||||||
# hash = { :a => 1 }; hash[:b] = hash
|
# hash = { :a => 1 }; hash[:b] = hash
|
||||||
# => { :a => 1, :b => {...} }
|
# => { :a => 1, :b => {...} }
|
||||||
#------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
def nested(object)
|
def nested(object)
|
||||||
case printable(object)
|
case printable(object)
|
||||||
when :array then @formatter.colorize("[...]", :array)
|
when :array then @formatter.colorize('[...]', :array)
|
||||||
when :hash then @formatter.colorize("{...}", :hash)
|
when :hash then @formatter.colorize('{...}', :hash)
|
||||||
when :struct then @formatter.colorize("{...}", :struct)
|
when :struct then @formatter.colorize('{...}', :struct)
|
||||||
else @formatter.colorize("...#{object.class}...", :class)
|
else @formatter.colorize("...#{object.class}...", :class)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
def unnested(object)
|
def unnested(object)
|
||||||
@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 that
|
||||||
# inherit from Array, Hash, File, Dir, and Struct are treated as the base class.
|
# inherit from Array, Hash, File, Dir, and Struct are treated as the base class.
|
||||||
#------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
def printable(object)
|
def printable(object)
|
||||||
case object
|
case object
|
||||||
when Array then :array
|
when Array then :array
|
||||||
|
@ -161,19 +158,19 @@ module AwesomePrint
|
||||||
when File then :file
|
when File then :file
|
||||||
when Dir then :dir
|
when Dir then :dir
|
||||||
when Struct then :struct
|
when Struct then :struct
|
||||||
else object.class.to_s.gsub(/:+/, "_").downcase.to_sym
|
else object.class.to_s.gsub(/:+/, '_').downcase.to_sym
|
||||||
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) || {})
|
||||||
@options.merge!(options)
|
@options.merge!(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Load ~/.aprc file with custom defaults that override default options.
|
# Load ~/.aprc file with custom defaults that override default options.
|
||||||
#------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
def merge_custom_defaults!
|
def merge_custom_defaults!
|
||||||
dotfile = File.join(ENV['HOME'], '.aprc')
|
dotfile = File.join(ENV['HOME'], '.aprc')
|
||||||
load dotfile if File.readable?(dotfile)
|
load dotfile if File.readable?(dotfile)
|
||||||
|
|
|
@ -96,24 +96,24 @@ RSpec.describe "AwesomePrint" do
|
||||||
|
|
||||||
it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
|
it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
|
||||||
markup = [ 1, :two, "three" ]
|
markup = [ 1, :two, "three" ]
|
||||||
expect(markup.ai(:html => true)).to eq <<-EOS.strip
|
expect(markup.ai(:html => true)).to eq <<-EOS.strip_heredoc.strip
|
||||||
<pre>[
|
<pre>[
|
||||||
<kbd style="color:white">[0] </kbd><kbd style="color:blue">1</kbd>,
|
<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">[1] </kbd><kbd style="color:darkcyan">:two</kbd>,
|
||||||
<kbd style="color:white">[2] </kbd><kbd style="color:brown">"three"</kbd>
|
<kbd style="color:white">[2] </kbd><kbd style="color:brown">"three"</kbd>
|
||||||
]</pre>
|
]</pre>
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
it "wraps hash ap output with only an outer <pre> tag" do
|
it "wraps hash ap output with only an outer <pre> tag" do
|
||||||
markup = [ { "hello" => "world" } ]
|
markup = [ { "hello" => "world" } ]
|
||||||
expect(markup.ai(:html => true)).to eq <<-EOS.strip
|
expect(markup.ai(:html => true)).to eq <<-EOS.strip_heredoc.strip
|
||||||
<pre>[
|
<pre>[
|
||||||
<kbd style="color:white">[0] </kbd>{
|
<kbd style="color:white">[0] </kbd>{
|
||||||
"hello"<kbd style="color:slategray"> => </kbd><kbd style="color:brown">"world"</kbd>
|
"hello"<kbd style="color:slategray"> => </kbd><kbd style="color:brown">"world"</kbd>
|
||||||
}
|
}
|
||||||
]</pre>
|
]</pre>
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
it "encodes HTML entities (plain)" do
|
it "encodes HTML entities (plain)" do
|
||||||
|
@ -142,13 +142,13 @@ EOS
|
||||||
AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
|
AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
|
||||||
hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
||||||
out = hash.ai(:plain => true)
|
out = hash.ai(:plain => true)
|
||||||
expect(out).to eq <<-EOS.strip
|
expect(out).to eq <<-EOS.strip_heredoc.strip
|
||||||
{
|
{
|
||||||
[ 0, 0, 255 ] => :yellow,
|
[ 0, 0, 255 ] => :yellow,
|
||||||
"magenta" => "rgb(255, 0, 255)",
|
"magenta" => "rgb(255, 0, 255)",
|
||||||
:red => "rgb(255, 0, 0)"
|
:red => "rgb(255, 0, 0)"
|
||||||
}
|
}
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue