amazing_print 1.8.1 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c54bf5089bc3eb9ecb72f953d2b7972ec91f512d17d85f64e0e68bfffe963eb
4
- data.tar.gz: e25109a0515688c4a3212e2dfa1a9a65a615959f67372a88f77011d8e0a531e0
3
+ metadata.gz: 8072cc250156956633f6956bdb3bc23c93b8c1e2c578471bfac37de210b8dc2d
4
+ data.tar.gz: 1abd0d9bc13804532c4fd5c4c2c2c180a32980809d3381866b367981d5396c62
5
5
  SHA512:
6
- metadata.gz: 53eb2f3a3bba9510f098ea1d550a2c4ce2080f21c4b19daea1e40e7b4fdc8df4336ef494bdb2626892e73f17ab6dc4bc2001dea1f64168d8d331baeb998ee273
7
- data.tar.gz: 5cf3937b23f27e89a75c2a0adbf0353f8a2fb5f936f836b44f7e152559bdb0c7f8b1e30c95f9bca506342bf08b2da3e29b377bf1757ccf031de7e24cf7ccdc99
6
+ metadata.gz: 9797830f7ff0055f00ce07a86ecb28ad912e1081c7718d5e0f41bd04167d11c93553182f96b029df10759116e29348434e153419a14c3fd3b11c358b842280c9
7
+ data.tar.gz: f9e236e54d4a1d10c2f54f70f71ebbd51fb799485cb9ccb5dd15aa33b5a33d60281f44117b81d414a8b57a69f79d8fb79b54e823684045c543fdab5c9943abe0
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v2.0.0
6
+
7
+ - Rework hash formats and add JSON format (#83)
8
+ - Format Data objects using StructFormatter (#138)
9
+
5
10
  ## v1.8.1
6
11
 
7
12
  - Fix authors list in gemspec #128
data/README.md CHANGED
@@ -43,7 +43,7 @@ raw: false, # Do not recursively format instance variables.
43
43
  sort_keys: false, # Do not sort hash keys.
44
44
  sort_vars: true, # Sort instance variables.
45
45
  limit: false, # Limit arrays & hashes. Accepts bool or int.
46
- ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
46
+ hash_format: :symbol,# The format for printing hashes. Can be one of :json, :rocket, or :symbol
47
47
  class_name: :class, # Method called to report the instance class name. (e.g. :to_s)
48
48
  object_id: true, # Show object id.
49
49
  color: {
@@ -53,6 +53,35 @@ module AmazingPrint
53
53
  end
54
54
  end
55
55
 
56
+ def rdbg!
57
+ return unless defined?(::DEBUGGER__::SESSION)
58
+
59
+ ::DEBUGGER__::SESSION.extend_feature(
60
+ thread_client: Module.new do
61
+ # rdbg calls this for both p and pp printing paths
62
+ def color_pp(obj, width)
63
+ opts = {
64
+ multiline: true,
65
+ index: false,
66
+ indent: 2
67
+ }
68
+
69
+ opts[:plain] = true if ::DEBUGGER__::CONFIG[:no_color]
70
+
71
+ if obj.respond_to?(:ai)
72
+ obj.ai(opts)
73
+ else
74
+ # Fallback if AP isn't mixed into the object for some reason
75
+ super
76
+ end
77
+ rescue StandardError
78
+ # Never break the debugger; fall back to rdbg default
79
+ super
80
+ end
81
+ end
82
+ )
83
+ end
84
+
56
85
  ##
57
86
  # Reload the cached custom configurations.
58
87
  #
@@ -65,7 +94,7 @@ module AmazingPrint
65
94
  # Takes a value and returns true unless it is false or nil
66
95
  # This is an alternative to the less readable !!(value)
67
96
  # https://github.com/bbatsov/ruby-style-guide#no-bang-bang
68
- def boolean(value)
97
+ def boolean(value) # rubocop:todo Naming/PredicateMethod
69
98
  value ? true : false
70
99
  end
71
100
  end
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'base_formatter'
4
+ require_relative '../json_helper'
4
5
 
5
6
  module AmazingPrint
6
7
  module Formatters
7
8
  class ArrayFormatter < BaseFormatter
9
+ include AmazingPrint::JSONHelper
10
+
8
11
  attr_reader :array, :inspector, :options
9
12
 
10
13
  def initialize(array, inspector)
@@ -51,7 +54,9 @@ module AmazingPrint
51
54
  def generate_printable_array
52
55
  array.map.with_index do |item, index|
53
56
  array_prefix(index, width(array)).tap do |temp|
54
- indented { temp << inspector.awesome(item) }
57
+ indented do
58
+ temp << json_awesome(item)
59
+ end
55
60
  end
56
61
  end
57
62
  end
@@ -127,7 +132,7 @@ module AmazingPrint
127
132
  end
128
133
 
129
134
  def generic_prefix(iteration, width, padding = '')
130
- if options[:index]
135
+ if options[:index] && options[:hash_format] != :json
131
136
  indent + colorize("[#{iteration.to_s.rjust(width)}] ", :array)
132
137
  else
133
138
  indent + padding
@@ -1,10 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'base_formatter'
4
+ require_relative '../json_helper'
4
5
 
5
6
  module AmazingPrint
6
7
  module Formatters
7
8
  class HashFormatter < BaseFormatter
9
+ include AmazingPrint::JSONHelper
10
+
11
+ VALID_HASH_FORMATS = %i[json rocket symbol].freeze
12
+
13
+ class InvalidHashFormatError < StandardError; end
14
+
8
15
  attr_reader :hash, :inspector, :options
9
16
 
10
17
  def initialize(hash, inspector)
@@ -12,6 +19,11 @@ module AmazingPrint
12
19
  @hash = hash
13
20
  @inspector = inspector
14
21
  @options = inspector.options
22
+
23
+ return if VALID_HASH_FORMATS.include?(options[:hash_format])
24
+
25
+ raise(InvalidHashFormatError, "Invalid hash_format: #{options[:hash_format].inspect}. " \
26
+ "Must be one of #{VALID_HASH_FORMATS}")
15
27
  end
16
28
 
17
29
  def format
@@ -48,10 +60,13 @@ module AmazingPrint
48
60
 
49
61
  data.map! do |key, value|
50
62
  indented do
51
- if options[:ruby19_syntax] && symbol?(key)
52
- ruby19_syntax(key, value, width)
53
- else
63
+ case options[:hash_format]
64
+ when :json
65
+ json_syntax(key, value, width)
66
+ when :rocket
54
67
  pre_ruby19_syntax(key, value, width)
68
+ when :symbol
69
+ ruby19_syntax(key, value, width)
55
70
  end
56
71
  end
57
72
  end
@@ -91,13 +106,36 @@ module AmazingPrint
91
106
  end
92
107
  end
93
108
 
109
+ def string?(key)
110
+ key[0] == '"' && key[-1] == '"'
111
+ end
112
+
94
113
  def symbol?(key)
95
114
  key.is_a?(Symbol)
96
115
  end
97
116
 
117
+ def json_format?
118
+ options[:hash_format] == :json
119
+ end
120
+
121
+ def json_syntax(key, value, width)
122
+ unless defined?(JSON)
123
+ warn 'JSON is not defined. Defaulting hash format to symbol'
124
+ return ruby19_syntax(key, value, width)
125
+ end
126
+
127
+ formatted_key = json_awesome(key, is_key: true)
128
+ formatted_value = json_awesome(value)
129
+
130
+ "#{align(formatted_key, width)}#{colorize(': ', :hash)}#{formatted_value}"
131
+ end
132
+
98
133
  def ruby19_syntax(key, value, width)
134
+ return pre_ruby19_syntax(key, value, width) unless symbol?(key)
135
+
99
136
  # Move the colon to the right side of the symbol
100
- awesome_key = inspector.awesome(key).sub(/#{key.inspect}/, "#{key}:")
137
+ key_string = key.inspect.include?('"') ? key.inspect.sub(':', '') : key.to_s
138
+ awesome_key = inspector.awesome(key).sub(/#{Regexp.escape(key.inspect)}/, "#{key_string}:")
101
139
 
102
140
  "#{align(awesome_key, width)} #{inspector.awesome(value)}"
103
141
  end
@@ -7,6 +7,7 @@ module AmazingPrint
7
7
  module Formatters
8
8
  module Kernel32
9
9
  extend Fiddle::Importer
10
+
10
11
  dlload 'kernel32'
11
12
  extern 'unsigned long GetFileAttributesA(const char*)'
12
13
  end
@@ -61,7 +61,11 @@ module AmazingPrint
61
61
  # We need to ensure that the original Kernel#format is used here instead of the one defined
62
62
  # above.
63
63
  # rubocop:disable Style/ColonMethodCall
64
- Kernel::format("#{struct.class.superclass}:#{struct.class}:0x%08x", struct.__id__ * 2)
64
+ if defined?(Data) && struct.is_a?(Data)
65
+ Kernel::format("data #{struct.class}:0x%08x", struct.__id__ * 2)
66
+ else
67
+ Kernel::format("#{struct.class.superclass}:#{struct.class}:0x%08x", struct.__id__ * 2)
68
+ end
65
69
  # rubocop:enable Style/ColonMethodCall
66
70
  end
67
71
 
@@ -20,7 +20,7 @@ module AmazingPrint
20
20
  ##
21
21
  # Unload the cached dotfile and load it again.
22
22
  #
23
- def self.reload_dotfile
23
+ def self.reload_dotfile # rubocop:todo Naming/PredicateMethod
24
24
  @@dotfile = nil
25
25
  new.send :load_dotfile
26
26
  true
@@ -37,7 +37,7 @@ module AmazingPrint
37
37
  sort_keys: false, # Do not sort hash keys.
38
38
  sort_vars: true, # Sort instance variables.
39
39
  limit: false, # Limit arrays & hashes. Accepts bool or int.
40
- ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
40
+ hash_format: :symbol, # The format for printing hashes. Can be one of :json, :rocket, or :symbol
41
41
  class_name: :class, # Method used to get Instance class name.
42
42
  object_id: true, # Show object_id.
43
43
  color: {
@@ -150,7 +150,12 @@ module AmazingPrint
150
150
  when File then :file
151
151
  when Dir then :dir
152
152
  when Struct then :struct
153
- else object.class.to_s.gsub(/:+/, '_').downcase.to_sym
153
+ else
154
+ if defined?(Data) && object.is_a?(Data)
155
+ :struct
156
+ else
157
+ object.class.to_s.gsub(/:+/, '_').downcase.to_sym
158
+ end
154
159
  end
155
160
  end
156
161
 
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Helper module for hash_format: :json
5
+ #
6
+ module AmazingPrint
7
+ module JSONHelper
8
+ VALUE_CLASSES_NOT_TO_CONVERT = %w[Array BigDecimal Float Hash Integer String].freeze
9
+
10
+ def json_awesome(object, is_key: false)
11
+ return inspector.awesome(object) unless options[:hash_format] == :json && object.respond_to?(:to_json)
12
+
13
+ if object.nil?
14
+ # Color null like we do nil
15
+ colorize(object.to_json, :nilclass)
16
+ elsif is_key && object.is_a?(Numeric)
17
+ # JSON keys should be a string
18
+ inspector.awesome(object.to_s)
19
+ elsif VALUE_CLASSES_NOT_TO_CONVERT.include?(object.class.name) || !object.respond_to?(:to_json)
20
+ # These objects should not be converted to strings with #to_json so we can treat them normally
21
+ inspector.awesome(object)
22
+ else
23
+ # Remove surrounding quotes added by #to_json from the standard library since
24
+ # inspector.awesome is going to add those for us for strings.
25
+ inspector.awesome(object.to_json.gsub(/\A"/, '').gsub(/"\z/, ''))
26
+ end
27
+ end
28
+ end
29
+ end
@@ -7,6 +7,6 @@
7
7
  #------------------------------------------------------------------------------
8
8
  module AmazingPrint
9
9
  def self.version
10
- '1.8.1'
10
+ '2.0.0'
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazing_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Dvorkin
@@ -56,6 +56,7 @@ files:
56
56
  - lib/amazing_print/formatters/struct_formatter.rb
57
57
  - lib/amazing_print/indentator.rb
58
58
  - lib/amazing_print/inspector.rb
59
+ - lib/amazing_print/json_helper.rb
59
60
  - lib/amazing_print/version.rb
60
61
  - lib/ap.rb
61
62
  homepage: https://github.com/amazing-print/amazing_print
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  - !ruby/object:Gem::Version
78
79
  version: '0'
79
80
  requirements: []
80
- rubygems_version: 3.6.7
81
+ rubygems_version: 3.6.9
81
82
  specification_version: 4
82
83
  summary: Pretty print Ruby objects with proper indentation and colors
83
84
  test_files: []