awesome_print 1.0.2 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -1
  3. data/Appraisals +52 -0
  4. data/CHANGELOG.md +152 -0
  5. data/CONTRIBUTING.md +81 -0
  6. data/Gemfile +3 -2
  7. data/Gemfile.lock +44 -13
  8. data/LICENSE +3 -3
  9. data/README.md +272 -264
  10. data/Rakefile +23 -1
  11. data/lib/ap.rb +1 -1
  12. data/lib/awesome_print/colorize.rb +24 -0
  13. data/lib/awesome_print/core_ext/array.rb +12 -2
  14. data/lib/awesome_print/core_ext/class.rb +1 -1
  15. data/lib/awesome_print/core_ext/kernel.rb +8 -3
  16. data/lib/awesome_print/core_ext/logger.rb +1 -1
  17. data/lib/awesome_print/core_ext/method.rb +1 -1
  18. data/lib/awesome_print/core_ext/object.rb +1 -1
  19. data/lib/awesome_print/core_ext/string.rb +1 -1
  20. data/lib/awesome_print/ext/action_view.rb +1 -1
  21. data/lib/awesome_print/ext/active_record.rb +8 -2
  22. data/lib/awesome_print/ext/active_support.rb +2 -2
  23. data/lib/awesome_print/ext/mongo_mapper.rb +86 -3
  24. data/lib/awesome_print/ext/mongoid.rb +3 -3
  25. data/lib/awesome_print/ext/nobrainer.rb +49 -0
  26. data/lib/awesome_print/ext/nokogiri.rb +1 -1
  27. data/lib/awesome_print/ext/ostruct.rb +27 -0
  28. data/lib/awesome_print/ext/ripple.rb +72 -0
  29. data/lib/awesome_print/ext/sequel.rb +57 -0
  30. data/lib/awesome_print/formatter.rb +59 -309
  31. data/lib/awesome_print/formatters/array_formatter.rb +73 -0
  32. data/lib/awesome_print/formatters/base_formatter.rb +138 -0
  33. data/lib/awesome_print/formatters/class_formatter.rb +24 -0
  34. data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
  35. data/lib/awesome_print/formatters/file_formatter.rb +22 -0
  36. data/lib/awesome_print/formatters/hash_formatter.rb +54 -0
  37. data/lib/awesome_print/formatters/method_formatter.rb +22 -0
  38. data/lib/awesome_print/formatters/object_formatter.rb +80 -0
  39. data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
  40. data/lib/awesome_print/indentator.rb +18 -0
  41. data/lib/awesome_print/inspector.rb +48 -6
  42. data/lib/awesome_print/version.rb +2 -2
  43. data/lib/awesome_print.rb +20 -10
  44. data/spec/active_record_helper.rb +24 -0
  45. data/spec/colors_spec.rb +10 -10
  46. data/spec/formats_spec.rb +191 -170
  47. data/spec/methods_spec.rb +69 -68
  48. data/spec/misc_spec.rb +250 -0
  49. data/spec/objects_spec.rb +62 -12
  50. data/spec/spec_helper.rb +66 -34
  51. metadata +125 -49
  52. data/CHANGELOG +0 -77
@@ -0,0 +1,22 @@
1
+ require_relative 'base_formatter'
2
+ require "shellwords"
3
+
4
+ module AwesomePrint
5
+ module Formatters
6
+ class FileFormatter < BaseFormatter
7
+
8
+ attr_reader :file, :inspector, :options
9
+
10
+ def initialize(file, inspector)
11
+ @file = file
12
+ @inspector = inspector
13
+ @options = inspector.options
14
+ end
15
+
16
+ def format
17
+ ls = File.directory?(file) ? `ls -adlF #{file.path.shellescape}` : `ls -alF #{file.path.shellescape}`
18
+ colorize(ls.empty? ? file.inspect : "#{file.inspect}\n#{ls.chop}", :file)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ require_relative 'base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class HashFormatter < BaseFormatter
6
+
7
+ attr_reader :hash, :inspector, :options
8
+
9
+ def initialize(hash, inspector)
10
+ @hash = hash
11
+ @inspector = inspector
12
+ @options = inspector.options
13
+ end
14
+
15
+ def format
16
+ return "{}" if hash == {}
17
+
18
+ keys = hash.keys
19
+ keys = keys.sort { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
20
+ data = keys.map do |key|
21
+ plain_single_line do
22
+ [ inspector.awesome(key), hash[key] ]
23
+ end
24
+ end
25
+
26
+ width = data.map { |key, | key.size }.max || 0
27
+ width += indentation if options[:indent] > 0
28
+
29
+ data = data.map do |key, value|
30
+ indented do
31
+ align(key, width) << colorize(" => ", :hash) << inspector.awesome(value)
32
+ end
33
+ end
34
+
35
+ data = limited(data, width, :hash => true) if should_be_limited?
36
+ if options[:multiline]
37
+ "{\n" << data.join(",\n") << "\n#{outdent}}"
38
+ else
39
+ "{ #{data.join(', ')} }"
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def plain_single_line
46
+ plain, multiline = options[:plain], options[:multiline]
47
+ options[:plain], options[:multiline] = true, false
48
+ yield
49
+ ensure
50
+ options[:plain], options[:multiline] = plain, multiline
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class MethodFormatter < BaseFormatter
6
+
7
+ attr_reader :method, :inspector, :options
8
+
9
+ def initialize(method, inspector)
10
+ @method = method
11
+ @inspector = inspector
12
+ @options = inspector.options
13
+ end
14
+
15
+ def format
16
+ name, args, owner = method_tuple(method)
17
+
18
+ "#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,80 @@
1
+ require_relative 'base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class ObjectFormatter < BaseFormatter
6
+
7
+ attr_reader :object, :variables, :inspector, :options
8
+
9
+ def initialize(object, variables, inspector)
10
+ @object = object
11
+ @variables = variables
12
+ @inspector = inspector
13
+ @options = inspector.options
14
+ end
15
+
16
+ def format
17
+ vars = variables.map do |var|
18
+ property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
19
+ accessor = if object.respond_to?(:"#{property}=")
20
+ object.respond_to?(property) ? :accessor : :writer
21
+ else
22
+ object.respond_to?(property) ? :reader : nil
23
+ end
24
+ if accessor
25
+ [ "attr_#{accessor} :#{property}", var ]
26
+ else
27
+ [ var.to_s, var ]
28
+ end
29
+ end
30
+
31
+ data = vars.sort.map do |declaration, var|
32
+ key = left_aligned do
33
+ align(declaration, declaration.size)
34
+ end
35
+
36
+ unless options[:plain]
37
+ if key =~ /(@\w+)/
38
+ key.sub!($1, colorize($1, :variable))
39
+ else
40
+ key.sub!(/(attr_\w+)\s(\:\w+)/, "#{colorize('\\1', :keyword)} #{colorize('\\2', :method)}")
41
+ end
42
+ end
43
+
44
+ indented do
45
+ var_contents = if valid_instance_var?(var)
46
+ object.instance_variable_get(var)
47
+ else
48
+ object.send(var) # Enables handling of Struct attributes
49
+ end
50
+
51
+ key << colorize(" = ", :hash) + inspector.awesome(var_contents)
52
+ end
53
+ end
54
+
55
+ if options[:multiline]
56
+ "#<#{awesome_instance}\n#{data.join(%Q/,\n/)}\n#{outdent}>"
57
+ else
58
+ "#<#{awesome_instance} #{data.join(', ')}>"
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def valid_instance_var?(variable_name)
65
+ variable_name.to_s.start_with?('@')
66
+ end
67
+
68
+ def awesome_instance
69
+ "#{object.class}:0x%08x" % (object.__id__ * 2)
70
+ end
71
+
72
+ def left_aligned
73
+ current, options[:indent] = options[:indent], 0
74
+ yield
75
+ ensure
76
+ options[:indent] = current
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class SimpleFormatter < BaseFormatter
6
+
7
+ attr_reader :string, :type, :inspector, :options
8
+
9
+ def initialize(string, type, inspector)
10
+ @string = string
11
+ @type = type
12
+ @inspector = inspector
13
+ @options = inspector.options
14
+ end
15
+
16
+ def format
17
+ colorize(string, type)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module AwesomePrint
2
+ class Indentator
3
+
4
+ attr_reader :shift_width, :indentation
5
+
6
+ def initialize(indentation)
7
+ @indentation = indentation
8
+ @shift_width = indentation.freeze
9
+ end
10
+
11
+ def indent
12
+ @indentation += shift_width
13
+ yield
14
+ ensure
15
+ @indentation -= shift_width
16
+ end
17
+ end
18
+ end
@@ -1,8 +1,10 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
1
+ # Copyright (c) 2010-2013 Michael Dvorkin
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
+ require_relative "indentator"
7
+
6
8
  module AwesomePrint
7
9
 
8
10
  class << self # Class accessors for custom defaults.
@@ -14,10 +16,41 @@ module AwesomePrint
14
16
  def force_colors!(value = true)
15
17
  @force_colors = value
16
18
  end
19
+
20
+ def console?
21
+ !!(defined?(IRB) || defined?(Pry))
22
+ end
23
+
24
+ def rails_console?
25
+ console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"])
26
+ end
27
+
28
+ def irb!
29
+ return unless defined?(IRB)
30
+ unless IRB.version.include?("DietRB")
31
+ IRB::Irb.class_eval do
32
+ def output_value
33
+ ap @context.last_value
34
+ end
35
+ end
36
+ else # MacRuby
37
+ IRB.formatter = Class.new(IRB::Formatter) do
38
+ def inspect_object(object)
39
+ object.ai
40
+ end
41
+ end.new
42
+ end
43
+ end
44
+
45
+ def pry!
46
+ if defined?(Pry)
47
+ Pry.print = proc { |output, value| output.puts value.ai }
48
+ end
49
+ end
17
50
  end
18
51
 
19
52
  class Inspector
20
- attr_accessor :options
53
+ attr_accessor :options, :indentator
21
54
 
22
55
  AP = :__awesome_print__
23
56
 
@@ -28,6 +61,7 @@ module AwesomePrint
28
61
  :html => false, # Use ANSI color codes rather than HTML.
29
62
  :multiline => true, # Display in multiple lines.
30
63
  :plain => false, # Use colors.
64
+ :raw => false, # Do not recursively format object instance variables.
31
65
  :sort_keys => false, # Do not sort hash keys.
32
66
  :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
33
67
  :color => {
@@ -43,6 +77,7 @@ module AwesomePrint
43
77
  :keyword => :cyan,
44
78
  :method => :purpleish,
45
79
  :nilclass => :red,
80
+ :rational => :blue,
46
81
  :string => :yellowish,
47
82
  :struct => :pale,
48
83
  :symbol => :cyanish,
@@ -57,8 +92,17 @@ module AwesomePrint
57
92
  merge_options!(options)
58
93
 
59
94
  @formatter = AwesomePrint::Formatter.new(self)
95
+ @indentator = AwesomePrint::Indentator.new(@options[:indent].abs)
60
96
  Thread.current[AP] ||= []
61
97
  end
98
+
99
+ def current_indentation
100
+ indentator.indentation
101
+ end
102
+
103
+ def increase_indentation
104
+ indentator.indent(&Proc.new)
105
+ end
62
106
 
63
107
  # Dispatcher that detects data nesting and invokes object-aware formatter.
64
108
  #------------------------------------------------------------------------------
@@ -129,10 +173,8 @@ module AwesomePrint
129
173
  #------------------------------------------------------------------------------
130
174
  def merge_custom_defaults!
131
175
  dotfile = File.join(ENV["HOME"], ".aprc")
132
- if File.readable?(dotfile)
133
- load dotfile
134
- merge_options!(AwesomePrint.defaults)
135
- end
176
+ load dotfile if File.readable?(dotfile)
177
+ merge_options!(AwesomePrint.defaults) if AwesomePrint.defaults.is_a?(Hash)
136
178
  rescue => e
137
179
  $stderr.puts "Could not load #{dotfile}: #{e}"
138
180
  end
@@ -1,10 +1,10 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
1
+ # Copyright (c) 2010-2013 Michael Dvorkin
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  module AwesomePrint
7
7
  def self.version
8
- '1.0.2'
8
+ '1.7.0'
9
9
  end
10
10
  end
data/lib/awesome_print.rb CHANGED
@@ -1,13 +1,13 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
1
+ # Copyright (c) 2010-2013 Michael Dvorkin
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  #
7
- # AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing
8
- # for subsequent requires.
7
+ # AwesomePrint might be loaded implicitly through ~/.irbrc or ~/.pryrc
8
+ # so do nothing for subsequent requires.
9
9
  #
10
- unless defined?(AwesomePrint)
10
+ unless defined?(AwesomePrint::Inspector)
11
11
  %w(array string method object class kernel).each do |file|
12
12
  require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}"
13
13
  end
@@ -16,15 +16,25 @@ unless defined?(AwesomePrint)
16
16
  require File.dirname(__FILE__) + "/awesome_print/formatter"
17
17
  require File.dirname(__FILE__) + "/awesome_print/version"
18
18
  require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger)
19
-
19
+ #
20
20
  # Load the following under normal circumstances as well as in Rails
21
- # console when required from ~/.irbrc.
22
- require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
23
- require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
24
-
21
+ # console when required from ~/.irbrc or ~/.pryrc.
22
+ #
23
+ require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || AwesomePrint.rails_console?
24
+ require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console?
25
+ #
25
26
  # Load remaining extensions.
26
- require File.dirname(__FILE__) + "/awesome_print/ext/action_view" if defined?(ActionView::Base)
27
+ #
28
+ if defined?(ActiveSupport.on_load)
29
+ ActiveSupport.on_load(:action_view) do
30
+ require File.dirname(__FILE__) + "/awesome_print/ext/action_view"
31
+ end
32
+ end
27
33
  require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
28
34
  require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
29
35
  require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri)
36
+ require File.dirname(__FILE__) + "/awesome_print/ext/nobrainer" if defined?(NoBrainer)
37
+ require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple)
38
+ require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
39
+ require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct)
30
40
  end
@@ -0,0 +1,24 @@
1
+ if ExtVerifier.has_rails?
2
+ # Required to use the column support
3
+ module Rails
4
+ def self.env
5
+ {}
6
+ end
7
+ end
8
+
9
+ # Establish connection to in-memory SQLite DB
10
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
11
+
12
+ # Create the users table
13
+ ActiveRecord::Migration.verbose = false
14
+ ActiveRecord::Migration.create_table :users do |t|
15
+ t.string :name
16
+ t.integer :rank
17
+ t.boolean :admin
18
+ t.datetime :created_at
19
+ end
20
+
21
+ # Create models
22
+ class User < ActiveRecord::Base; end
23
+ class SubUser < User; end
24
+ end
data/spec/colors_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- describe "AwesomePrint" do
3
+ RSpec.describe "AwesomePrint" do
4
4
  def stub_tty!(output = true, stream = STDOUT)
5
5
  if output
6
6
  stream.instance_eval { def tty?; true; end }
@@ -30,14 +30,14 @@ describe "AwesomePrint" do
30
30
 
31
31
  it "colorizes tty processes by default" do
32
32
  stub_tty!
33
- @arr.ai(:multiline => false).should == COLORIZED
33
+ expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
34
34
  end
35
35
 
36
36
  it "colorizes processes with ENV['ANSICON'] by default" do
37
37
  begin
38
38
  stub_tty!
39
39
  term, ENV['ANSICON'] = ENV['ANSICON'], "1"
40
- @arr.ai(:multiline => false).should == COLORIZED
40
+ expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
41
41
  ensure
42
42
  ENV['ANSICON'] = term
43
43
  end
@@ -47,7 +47,7 @@ describe "AwesomePrint" do
47
47
  begin
48
48
  stub_tty!
49
49
  term, ENV['TERM'] = ENV['TERM'], "dumb"
50
- @arr.ai(:multiline => false).should == PLAIN
50
+ expect(@arr.ai(:multiline => false)).to eq(PLAIN)
51
51
  ensure
52
52
  ENV['TERM'] = term
53
53
  end
@@ -56,7 +56,7 @@ describe "AwesomePrint" do
56
56
  it "does not colorize subprocesses by default" do
57
57
  begin
58
58
  stub_tty! false
59
- @arr.ai(:multiline => false).should == PLAIN
59
+ expect(@arr.ai(:multiline => false)).to eq(PLAIN)
60
60
  ensure
61
61
  stub_tty!
62
62
  end
@@ -70,14 +70,14 @@ describe "AwesomePrint" do
70
70
 
71
71
  it "still colorizes tty processes" do
72
72
  stub_tty!
73
- @arr.ai(:multiline => false).should == COLORIZED
73
+ expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
74
74
  end
75
75
 
76
76
  it "colorizes processes with ENV['ANSICON'] set to 0" do
77
77
  begin
78
78
  stub_tty!
79
79
  term, ENV['ANSICON'] = ENV['ANSICON'], "1"
80
- @arr.ai(:multiline => false).should == COLORIZED
80
+ expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
81
81
  ensure
82
82
  ENV['ANSICON'] = term
83
83
  end
@@ -87,7 +87,7 @@ describe "AwesomePrint" do
87
87
  begin
88
88
  stub_tty!
89
89
  term, ENV['TERM'] = ENV['TERM'], "dumb"
90
- @arr.ai(:multiline => false).should == COLORIZED
90
+ expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
91
91
  ensure
92
92
  ENV['TERM'] = term
93
93
  end
@@ -96,7 +96,7 @@ describe "AwesomePrint" do
96
96
  it "colorizes subprocess" do
97
97
  begin
98
98
  stub_tty! false
99
- @arr.ai(:multiline => false).should == COLORIZED
99
+ expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
100
100
  ensure
101
101
  stub_tty!
102
102
  end