amazing_print 1.4.0 → 1.6.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 (37) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +16 -0
  3. data/CHANGELOG.md +56 -33
  4. data/Gemfile +12 -0
  5. data/Gemfile.lock +61 -44
  6. data/README.md +3 -4
  7. data/lib/amazing_print/colorize.rb +5 -12
  8. data/lib/amazing_print/colors.rb +37 -0
  9. data/lib/amazing_print/core_ext/awesome_method_array.rb +1 -1
  10. data/lib/amazing_print/custom_defaults.rb +7 -1
  11. data/lib/amazing_print/ext/mongoid.rb +14 -2
  12. data/lib/amazing_print/ext_loader.rb +27 -0
  13. data/lib/amazing_print/formatters/array_formatter.rb +1 -1
  14. data/lib/amazing_print/formatters/base_formatter.rb +8 -3
  15. data/lib/amazing_print/formatters/dir_formatter.rb +10 -1
  16. data/lib/amazing_print/formatters/file_formatter.rb +10 -1
  17. data/lib/amazing_print/formatters/hash_formatter.rb +3 -5
  18. data/lib/amazing_print/formatters/mswin_helper.rb +63 -0
  19. data/lib/amazing_print/inspector.rb +6 -3
  20. data/lib/amazing_print/version.rb +1 -1
  21. data/lib/amazing_print.rb +3 -9
  22. data/spec/colors_spec.rb +9 -4
  23. data/spec/ext/action_controller_spec.rb +3 -3
  24. data/spec/ext/active_record_spec.rb +30 -60
  25. data/spec/ext/active_support_spec.rb +2 -2
  26. data/spec/ext/mongoid_spec.rb +110 -47
  27. data/spec/ext/nokogiri_spec.rb +6 -6
  28. data/spec/ext/sequel_spec.rb +1 -1
  29. data/spec/formats_spec.rb +97 -89
  30. data/spec/misc_spec.rb +15 -61
  31. data/spec/spec_helper.rb +7 -1
  32. data/spec/support/active_record_data/7_0_diana.txt +110 -0
  33. data/spec/support/active_record_data/7_0_multi.txt +222 -0
  34. data/spec/support/rails_versions.rb +5 -0
  35. metadata +13 -156
  36. data/lib/amazing_print/core_ext/string.rb +0 -45
  37. data/spec/core_ext/string_spec.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95f05f3413c3497f346ec0bb7de2ed94a065aa6224a0a6bc8b36c699728ea4b2
4
- data.tar.gz: 112760cadb1e3b76240163a64a462323975e5885ea0e002845790d75fb0c6858
3
+ metadata.gz: 114d8297770972541aa0e40c8dd8fbc8dccf658089fe2b69d272fac2994b9621
4
+ data.tar.gz: 4e26b60b130dfe0b8eb4ebfe1d91f61cb3ca3de60d07ce0166650257bf83bce9
5
5
  SHA512:
6
- metadata.gz: 2f2e2fddf5cbf25953721ab0bc8f0ff0648fbe93710508137af6022ef5b8c5153be9292cbea2910fe4ef8b434c2a3510585f2ef5e98097296f78e0c74f292140
7
- data.tar.gz: 0b2e88771d4c2835e586a5ccf5c3c6e8822267475a65ddd80faf5908e526b099ea888953395fb77047df776ba7f6e7e63cf8bf50cb7676b253b2c67987b1437c
6
+ metadata.gz: f1890155df4d922d6975a3ddca5c229a2d6f3c1326cc82836bea4816a068dade724f1dfcaadc8697b213474908fc17671aa75dc79d1758cbe378509267440534
7
+ data.tar.gz: 270b1f75b7aa990b103325f69468ea985c29a09f5b23450cc0e7b9a600a5f3a3ea6805c813151e1b584801195784fd604e30881dae9fd1e2ab1107c769eadd52
data/Appraisals CHANGED
@@ -37,6 +37,22 @@ if RUBY_VERSION < '3'
37
37
  end
38
38
  end
39
39
 
40
+ if RUBY_VERSION >= '2.7'
41
+ appraise 'rails-7.0' do
42
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 70.0', platform: :jruby
43
+ gem 'rails', '~> 7.0.0'
44
+ gem 'sqlite3', platform: :mri
45
+ end
46
+
47
+ appraise 'mongoid-7.0' do
48
+ gem 'mongoid', '~> 7.0.0'
49
+ end
50
+
51
+ appraise 'mongoid-8.0' do
52
+ gem 'mongoid', '~> 8.0.0'
53
+ end
54
+ end
55
+
40
56
  appraise 'rails-6.1' do
41
57
  gem 'activerecord-jdbcsqlite3-adapter', '~> 61.0', platform: :jruby
42
58
  gem 'rails', '~> 6.1.0'
data/CHANGELOG.md CHANGED
@@ -1,52 +1,75 @@
1
+ # CHANGELOG
2
+
1
3
  ## Unreleased
2
4
 
5
+ ## V1.6.0
6
+
7
+ - Use pager with Pry #96
8
+ - Add Rails 7 appraisal #98
9
+ - Allow Hash keys to be colorized #99
10
+ - Use CI merge queue #104
11
+ - Add support for Ruby 3.3 #105
12
+ - Add Mongoid field aliases #106
13
+ - Add bigdecimal gem #109
14
+ - Add ExtLoader to help with require order issues #110
15
+
16
+ ## v1.5.0
17
+
18
+ - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1
19
+ - Add File/Dir formatters for mswin platform #48
20
+ - Don't monkey patch String class #91
21
+ - Fix ruby19 hash syntax so it can be copy-pasted #94
22
+
3
23
  ## v1.4.0
4
- - Support loading config from `$XDG_CONFIG_HOME/aprc` - #63
5
- - Remove support for Rails 5.1 #75
6
- - Update AR specs for Ruby 2.6.7 #76
7
- - Load .aprc configs only once. #74
8
- - Add XDG config support #77
9
- - Rubocop updates #79
10
- - Update Irb integration for v1.2.6+ #81
24
+
25
+ - Support loading config from `$XDG_CONFIG_HOME/aprc` - #63
26
+ - Remove support for Rails 5.1 #75
27
+ - Update AR specs for Ruby 2.6.7 #76
28
+ - Load .aprc configs only once. #74
29
+ - Add XDG config support #77
30
+ - Rubocop updates #79
31
+ - Update Irb integration for v1.2.6+ #81
11
32
 
12
33
  ## v1.3.0
13
- - Fix HTML escaping problems #53
14
- - Update test suite for Ruby 2.7.2 and JRuby #61
15
- - Add ActionView spec for html_safe #65
16
- - Add support for Rails 6.1 #68
17
- - Update specs for Ruby 3.0 #69
34
+
35
+ - Fix HTML escaping problems #53
36
+ - Update test suite for Ruby 2.7.2 and JRuby #61
37
+ - Add ActionView spec for html_safe #65
38
+ - Add support for Rails 6.1 #68
39
+ - Update specs for Ruby 3.0 #69
18
40
 
19
41
  ## v1.2.2
20
- - Support Ruby 3.0 / IRB 1.2.6 - #57
21
- - Fix FrozenError - #51
22
- - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1 - #46
23
- - Add passing of `options` to `Logger#ap` - #55
42
+
43
+ - Support Ruby 3.0 / IRB 1.2.6 - #57
44
+ - Fix FrozenError - #51
45
+ - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1 - #46
46
+ - Add passing of `options` to `Logger#ap` - #55
24
47
 
25
48
  ## v1.2.1
26
- - Correctly print active_model_errors for models that don't have tables - #42 by sahglie
27
- - Update AmazingPrint::MongoMapper for frozen strings - #44
28
49
 
29
- ## v1.2.0
50
+ - Correctly print active_model_errors for models that don't have tables - #42 by sahglie
51
+ - Update AmazingPrint::MongoMapper for frozen strings - #44
30
52
 
31
- - Fix frozen string literal issue with ActiveRecord
32
- - Add uncolor String method to remove ANSI color codes - #30 by duffyjp
33
- - Restore original copyright - #33 by amarshall
34
- - Remove method core extension since it is not needed since ruby 1.9 - #37 by grosser
35
- - Remove pale and black string color aliases - #38
36
- - Fix formatting ActionController::Parameters - #29
53
+ ## v1.2.0
37
54
 
55
+ - Fix frozen string literal issue with ActiveRecord
56
+ - Add uncolor String method to remove ANSI color codes - #30 by duffyjp
57
+ - Restore original copyright - #33 by amarshall
58
+ - Remove method core extension since it is not needed since ruby 1.9 - #37 by grosser
59
+ - Remove pale and black string color aliases - #38
60
+ - Fix formatting ActionController::Parameters - #29
38
61
 
39
62
  ## v1.1.0
40
- - Print out method keyword arguments
41
- - Fix NoMethodError with Sequel
42
- - Code cleanups
43
63
 
44
- Thanks for the great contributions from:
45
-
46
- - andydna
47
- - beanieboi
64
+ - Print out method keyword arguments
65
+ - Fix NoMethodError with Sequel
66
+ - Code cleanups
48
67
 
68
+ Thanks for the great contributions from:
49
69
 
70
+ - andydna
71
+ - beanieboi
50
72
 
51
73
  ## v1.0.0
52
- - Initial Release.
74
+
75
+ - Initial Release.
data/Gemfile CHANGED
@@ -4,3 +4,15 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in amazing_print.gemspec
6
6
  gemspec
7
+
8
+ # Development dependencies
9
+ # Update this once https://github.com/thoughtbot/appraisal/pull/202 is released
10
+ gem 'appraisal', git: 'https://github.com/thoughtbot/appraisal.git', branch: :main
11
+
12
+ gem 'bigdecimal'
13
+ gem 'fakefs', '~> 1.2'
14
+ gem 'nokogiri', '~> 1.10'
15
+ gem 'pry'
16
+ gem 'rspec', '~> 3.9'
17
+ gem 'rubocop', '~> 1.20'
18
+ gem 'rubocop-rspec', '~> 2.4'
data/Gemfile.lock CHANGED
@@ -1,71 +1,88 @@
1
+ GIT
2
+ remote: https://github.com/thoughtbot/appraisal.git
3
+ revision: feb78bcc6177038399bff098cb6c2bd4bca4972a
4
+ branch: main
5
+ specs:
6
+ appraisal (2.5.0)
7
+ bundler
8
+ rake
9
+ thor (>= 0.14.0)
10
+
1
11
  PATH
2
12
  remote: .
3
13
  specs:
4
- amazing_print (1.3.1.pre)
14
+ amazing_print (1.5.0)
5
15
 
6
16
  GEM
7
17
  remote: https://rubygems.org/
8
18
  specs:
9
- appraisal (2.4.1)
10
- bundler
11
- rake
12
- thor (>= 0.14.0)
13
19
  ast (2.4.2)
20
+ bigdecimal (3.1.6)
14
21
  coderay (1.1.3)
15
- diff-lcs (1.4.4)
16
- fakefs (1.3.2)
22
+ diff-lcs (1.5.0)
23
+ fakefs (1.9.0)
24
+ json (2.7.1)
25
+ language_server-protocol (3.17.0.3)
17
26
  method_source (1.0.0)
18
- mini_portile2 (2.6.1)
19
- nokogiri (1.12.5)
20
- mini_portile2 (~> 2.6.1)
27
+ nokogiri (1.16.0-x86_64-linux)
21
28
  racc (~> 1.4)
22
- parallel (1.21.0)
23
- parser (3.0.2.0)
29
+ parallel (1.24.0)
30
+ parser (3.3.0.2)
24
31
  ast (~> 2.4.1)
25
- pry (0.14.1)
32
+ racc
33
+ pry (0.14.2)
26
34
  coderay (~> 1.1)
27
35
  method_source (~> 1.0)
28
- racc (1.5.2)
29
- rainbow (3.0.0)
30
- rake (13.0.6)
31
- regexp_parser (2.1.1)
32
- rexml (3.2.5)
33
- rspec (3.10.0)
34
- rspec-core (~> 3.10.0)
35
- rspec-expectations (~> 3.10.0)
36
- rspec-mocks (~> 3.10.0)
37
- rspec-core (3.10.1)
38
- rspec-support (~> 3.10.0)
39
- rspec-expectations (3.10.1)
36
+ racc (1.7.3)
37
+ rainbow (3.1.1)
38
+ rake (13.1.0)
39
+ regexp_parser (2.9.0)
40
+ rexml (3.2.6)
41
+ rspec (3.12.0)
42
+ rspec-core (~> 3.12.0)
43
+ rspec-expectations (~> 3.12.0)
44
+ rspec-mocks (~> 3.12.0)
45
+ rspec-core (3.12.2)
46
+ rspec-support (~> 3.12.0)
47
+ rspec-expectations (3.12.3)
40
48
  diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.10.0)
42
- rspec-mocks (3.10.2)
49
+ rspec-support (~> 3.12.0)
50
+ rspec-mocks (3.12.6)
43
51
  diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.10.0)
45
- rspec-support (3.10.2)
46
- rubocop (1.22.1)
52
+ rspec-support (~> 3.12.0)
53
+ rspec-support (3.12.1)
54
+ rubocop (1.59.0)
55
+ json (~> 2.3)
56
+ language_server-protocol (>= 3.17.0)
47
57
  parallel (~> 1.10)
48
- parser (>= 3.0.0.0)
58
+ parser (>= 3.2.2.4)
49
59
  rainbow (>= 2.2.2, < 4.0)
50
60
  regexp_parser (>= 1.8, < 3.0)
51
- rexml
52
- rubocop-ast (>= 1.12.0, < 2.0)
61
+ rexml (>= 3.2.5, < 4.0)
62
+ rubocop-ast (>= 1.30.0, < 2.0)
53
63
  ruby-progressbar (~> 1.7)
54
- unicode-display_width (>= 1.4.0, < 3.0)
55
- rubocop-ast (1.12.0)
56
- parser (>= 3.0.1.1)
57
- rubocop-rspec (2.5.0)
58
- rubocop (~> 1.19)
59
- ruby-progressbar (1.11.0)
60
- thor (1.1.0)
61
- unicode-display_width (2.1.0)
64
+ unicode-display_width (>= 2.4.0, < 3.0)
65
+ rubocop-ast (1.30.0)
66
+ parser (>= 3.2.1.0)
67
+ rubocop-capybara (2.20.0)
68
+ rubocop (~> 1.41)
69
+ rubocop-factory_bot (2.25.1)
70
+ rubocop (~> 1.41)
71
+ rubocop-rspec (2.26.1)
72
+ rubocop (~> 1.40)
73
+ rubocop-capybara (~> 2.17)
74
+ rubocop-factory_bot (~> 2.22)
75
+ ruby-progressbar (1.13.0)
76
+ thor (1.3.0)
77
+ unicode-display_width (2.5.0)
62
78
 
63
79
  PLATFORMS
64
- ruby
80
+ x86_64-linux
65
81
 
66
82
  DEPENDENCIES
67
83
  amazing_print!
68
- appraisal (~> 2.3)
84
+ appraisal!
85
+ bigdecimal
69
86
  fakefs (~> 1.2)
70
87
  nokogiri (~> 1.10)
71
88
  pry
@@ -74,4 +91,4 @@ DEPENDENCIES
74
91
  rubocop-rspec (~> 2.4)
75
92
 
76
93
  BUNDLED WITH
77
- 2.2.27
94
+ 2.5.4
data/README.md CHANGED
@@ -3,9 +3,8 @@
3
3
  [![RubyGems][gem_version_badge]][ruby_gems]
4
4
  [![RubyGems][gem_downloads_badge]][ruby_gems]
5
5
  |
6
- [![Travis CI][travis_ci_badge]][travis_ci]
7
6
  ![Specs](https://github.com/amazing-print/amazing_print/workflows/Specs/badge.svg)
8
- ![Lint](https://github.com/amazing-print/amazing_print/workflows/Lint/badge.svg)
7
+ ![Lint](https://github.com/amazing-print/amazing_print/workflows/Lints/badge.svg)
9
8
  |
10
9
  [![Gitter](https://badges.gitter.im/amazing-print/community.svg)](https://gitter.im/amazing-print/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
11
10
 
@@ -316,11 +315,11 @@ formatting:
316
315
 
317
316
  <%= ap @accounts.first, :html => true %>
318
317
 
319
- ### String Convenience Methods ###
318
+ ### Colorizing Strings ###
320
319
  Use methods such as `.red` to set string color:
321
320
 
322
321
  ```ruby
323
- irb> puts "red text".red
322
+ irb> puts AmazingPrint::Colors.red("red text")
324
323
  red text # (it's red)
325
324
  ```
326
325
 
@@ -2,24 +2,17 @@
2
2
 
3
3
  autoload :CGI, 'cgi'
4
4
 
5
+ require_relative 'colors'
6
+
5
7
  module AmazingPrint
6
8
  module Colorize
7
9
  # Pick the color and apply it to the given string as necessary.
8
10
  #------------------------------------------------------------------------------
9
11
  def colorize(str, type)
10
12
  str = CGI.escapeHTML(str) if options[:html]
11
- if options[:plain] || !options[:color][type] || !inspector.colorize?
12
- str
13
- #
14
- # Check if the string color method is defined by amazing_print and accepts
15
- # html parameter or it has been overriden by some gem such as colorize.
16
- #
17
- elsif str.method(options[:color][type]).arity == -1 # Accepts html parameter.
18
- str.send(options[:color][type], options[:html])
19
- else
20
- str = %(<kbd style="color:#{options[:color][type]}">#{str}</kbd>) if options[:html]
21
- str.send(options[:color][type])
22
- end
13
+ return str if options[:plain] || !options[:color][type] || !inspector.colorize?
14
+
15
+ AmazingPrint::Colors.public_send(options[:color][type], str, options[:html])
23
16
  end
24
17
  end
25
18
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingPrint
4
+ module Colors
5
+ module_function
6
+
7
+ #
8
+ # ANSI color codes:
9
+ # \e => escape
10
+ # 30 => color base
11
+ # 1 => bright
12
+ # 0 => normal
13
+ #
14
+ # For HTML coloring we use <kbd> tag instead of <span> to require monospace
15
+ # font. Note that beloved <tt> has been removed from HTML5.
16
+ #
17
+ %w[gray red green yellow blue purple cyan white].zip(
18
+ %w[black darkred darkgreen brown navy darkmagenta darkcyan slategray]
19
+ ).each_with_index do |(color, shade), i|
20
+ # NOTE: Format strings are created once only, for performance, and remembered by closures.
21
+
22
+ term_bright_seq = "\e[1;#{i + 30}m%s\e[0m"
23
+ html_bright_seq = %(<kbd style="color:#{color}">%s</kbd>)
24
+
25
+ define_method color do |str, html = false|
26
+ (html ? html_bright_seq : term_bright_seq) % str
27
+ end
28
+
29
+ term_normal_seq = "\e[0;#{i + 30}m%s\e[0m"
30
+ html_normal_seq = %(<kbd style="color:#{shade}">%s</kbd>)
31
+
32
+ define_method "#{color}ish" do |str, html = false|
33
+ (html ? html_normal_seq : term_normal_seq) % str
34
+ end
35
+ end
36
+ end
37
+ end
@@ -78,7 +78,7 @@ module AwesomeMethodArray # :nodoc:
78
78
  super(pattern)
79
79
  end
80
80
  arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
81
- arr.select! { |item| (item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
81
+ arr.select! { |item| item.is_a?(Symbol) || item.is_a?(String) } # grep block might return crap.
82
82
  arr
83
83
  end
84
84
  end
@@ -44,7 +44,13 @@ module AmazingPrint
44
44
  end
45
45
 
46
46
  def pry!
47
- Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
47
+ return unless defined?(Pry)
48
+
49
+ Pry.print = proc do |_output, value, pry_instance|
50
+ pry_instance.pager.open do |pager|
51
+ pager.print "#{value.ai}\n"
52
+ end
53
+ end
48
54
  end
49
55
 
50
56
  ##
@@ -33,8 +33,14 @@ module AmazingPrint
33
33
  def awesome_mongoid_class(object)
34
34
  return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
35
35
 
36
+ aliases = object.aliased_fields.invert
36
37
  data = object.fields.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
37
- hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
38
+ name = c[1].name
39
+ alias_name = aliases[name] unless name == '_id'
40
+ printed_name = alias_name ? "#{alias_name}(#{name})" : name
41
+
42
+ hash[printed_name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
43
+ hash
38
44
  end
39
45
 
40
46
  name = "class #{awesome_simple(object.to_s, :class)}"
@@ -48,8 +54,14 @@ module AmazingPrint
48
54
  def awesome_mongoid_document(object)
49
55
  return object.inspect unless defined?(::ActiveSupport::OrderedHash)
50
56
 
57
+ aliases = object.aliased_fields.invert
51
58
  data = (object.attributes || {}).sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
52
- hash[c[0].to_sym] = c[1]
59
+ name = c[0]
60
+ alias_name = aliases[name] unless name == '_id'
61
+ printed_name = alias_name ? "#{alias_name}(#{name})" : name
62
+
63
+ hash[printed_name.to_sym] = c[1]
64
+ hash
53
65
  end
54
66
  data = { errors: object.errors, attributes: data } unless object.errors.empty?
55
67
  "#{object} #{awesome_hash(data)}"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingPrint
4
+ ##
5
+ # Attempt to load extensions up to 3 times since this library may be required
6
+ # before dependencies that we have extensions for.
7
+ #
8
+ class ExtLoader
9
+ EXT_LOAD_ATTEMPT_LIMIT = 3
10
+
11
+ @load_attemps = 0
12
+
13
+ def self.call
14
+ return if @load_attemps >= EXT_LOAD_ATTEMPT_LIMIT
15
+
16
+ require_relative 'ext/mongo_mapper' if defined?(MongoMapper)
17
+ require_relative 'ext/mongoid' if defined?(Mongoid)
18
+ require_relative 'ext/nobrainer' if defined?(NoBrainer)
19
+ require_relative 'ext/nokogiri' if defined?(Nokogiri)
20
+ require_relative 'ext/ostruct' if defined?(OpenStruct) # rubocop:disable Style/OpenStructUse
21
+ require_relative 'ext/ripple' if defined?(Ripple)
22
+ require_relative 'ext/sequel' if defined?(Sequel)
23
+
24
+ @load_attemps += 1
25
+ end
26
+ end
27
+ end
@@ -15,7 +15,7 @@ module AmazingPrint
15
15
  end
16
16
 
17
17
  def format
18
- if array.length.zero?
18
+ if array.empty?
19
19
  '[]'
20
20
  elsif methods_array?
21
21
  methods_array
@@ -130,17 +130,22 @@ module AmazingPrint
130
130
  def align(value, width)
131
131
  if options[:multiline]
132
132
  indent_option = options[:indent]
133
+ effective_width = width + value.size - colorless_size(value)
133
134
  if indent_option.positive?
134
- value.rjust(width)
135
+ value.rjust(effective_width)
135
136
  elsif indent_option.zero?
136
- "#{indent}#{value.ljust(width)}"
137
+ "#{indent}#{value.ljust(effective_width)}"
137
138
  else
138
- "#{indent(indentation + indent_option)}#{value.ljust(width)}"
139
+ "#{indent(indentation + indent_option)}#{value.ljust(effective_width)}"
139
140
  end
140
141
  else
141
142
  value
142
143
  end
143
144
  end
145
+
146
+ def colorless_size(string)
147
+ string.gsub(/\e\[[\d;]+m/, '').size
148
+ end
144
149
  end
145
150
  end
146
151
  end
@@ -3,6 +3,7 @@
3
3
  require 'shellwords'
4
4
 
5
5
  require_relative 'base_formatter'
6
+ require_relative 'mswin_helper' if RUBY_PLATFORM.include?('mswin')
6
7
 
7
8
  module AmazingPrint
8
9
  module Formatters
@@ -17,9 +18,17 @@ module AmazingPrint
17
18
  end
18
19
 
19
20
  def format
20
- ls = `ls -alF #{dir.path.shellescape}`
21
+ ls = info
21
22
  colorize(ls.empty? ? dir.inspect : "#{dir.inspect}\n#{ls.chop}", :dir)
22
23
  end
24
+
25
+ def info
26
+ if RUBY_PLATFORM.include?('mswin')
27
+ "#{GetChildItem.new(@dir.path)}\n"
28
+ else
29
+ `ls -alF #{dir.path.shellescape}`
30
+ end
31
+ end
23
32
  end
24
33
  end
25
34
  end
@@ -3,6 +3,7 @@
3
3
  require 'shellwords'
4
4
 
5
5
  require_relative 'base_formatter'
6
+ require_relative 'mswin_helper' if RUBY_PLATFORM.include?('mswin')
6
7
 
7
8
  module AmazingPrint
8
9
  module Formatters
@@ -17,9 +18,17 @@ module AmazingPrint
17
18
  end
18
19
 
19
20
  def format
20
- ls = File.directory?(file) ? `ls -adlF #{file.path.shellescape}` : `ls -alF #{file.path.shellescape}`
21
+ ls = info
21
22
  colorize(ls.empty? ? file.inspect : "#{file.inspect}\n#{ls.chop}", :file)
22
23
  end
24
+
25
+ def info
26
+ if RUBY_PLATFORM.include?('mswin')
27
+ "#{GetChildItem.new(@file.path)}\n"
28
+ else
29
+ File.directory?(file) ? `ls -adlF #{file.path.shellescape}` : `ls -alF #{file.path.shellescape}`
30
+ end
31
+ end
23
32
  end
24
33
  end
25
34
  end
@@ -66,7 +66,7 @@ module AmazingPrint
66
66
  end
67
67
 
68
68
  def max_key_width(keys)
69
- keys.map { |key, _value| key.size }.max || 0
69
+ keys.map { |key, _value| colorless_size(key) }.max || 0
70
70
  end
71
71
 
72
72
  def printable_keys
@@ -87,7 +87,8 @@ module AmazingPrint
87
87
 
88
88
  def ruby19_syntax(key, value, width)
89
89
  key[0] = ''
90
- "#{align(key, width - 1)}#{colorize(': ', :hash)}#{inspector.awesome(value)}"
90
+ key << ':'
91
+ "#{align(key, width)} #{inspector.awesome(value)}"
91
92
  end
92
93
 
93
94
  def pre_ruby19_syntax(key, value, width)
@@ -95,13 +96,10 @@ module AmazingPrint
95
96
  end
96
97
 
97
98
  def plain_single_line
98
- plain = options[:plain]
99
99
  multiline = options[:multiline]
100
- options[:plain] = true
101
100
  options[:multiline] = false
102
101
  yield
103
102
  ensure
104
- options[:plain] = plain
105
103
  options[:multiline] = multiline
106
104
  end
107
105
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fiddle'
4
+ require 'fiddle/import'
5
+
6
+ module AmazingPrint
7
+ module Formatters
8
+ module Kernel32
9
+ extend Fiddle::Importer
10
+ dlload 'kernel32'
11
+ extern 'unsigned long GetFileAttributesA(const char*)'
12
+ end
13
+
14
+ class GetChildItem
15
+ def initialize(fname)
16
+ @fname = fname
17
+ @stat = File.send(File.symlink?(@fname) ? :lstat : :stat, @fname)
18
+ @attrs = Kernel32::GetFileAttributesA @fname
19
+ end
20
+
21
+ # docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
22
+ FILE_ATTRIBUTE_ARCHIVE = 0x20
23
+ FILE_ATTRIBUTE_READONLY = 0x1
24
+ FILE_ATTRIBUTE_HIDDEN = 0x2
25
+ FILE_ATTRIBUTE_SYSTEM = 0x4
26
+
27
+ def mode
28
+ r = ['-'] * 6
29
+ r[0] = 'd' if @stat.directory?
30
+ r[1] = 'a' unless (@attrs & FILE_ATTRIBUTE_ARCHIVE).zero?
31
+ r[2] = 'r' unless (@attrs & FILE_ATTRIBUTE_READONLY).zero?
32
+ r[3] = 'h' unless (@attrs & FILE_ATTRIBUTE_HIDDEN).zero?
33
+ r[4] = 's' unless (@attrs & FILE_ATTRIBUTE_SYSTEM).zero?
34
+ r[5] = 'l' if File.symlink? @fname
35
+ r.join
36
+ end
37
+
38
+ def last_write_time
39
+ @stat.mtime.strftime '%Y-%m-%d %H:%M'
40
+ end
41
+
42
+ def length
43
+ @stat.file? ? @stat.size.to_s : ''
44
+ end
45
+
46
+ def name
47
+ @fname
48
+ end
49
+
50
+ def to_s
51
+ format '%-12<Mode>s %<LastWriteTime>s %14<Length>s %<Name>s',
52
+ {
53
+ Mode: mode,
54
+ LastWriteTime: last_write_time,
55
+ Length: length,
56
+ Name: name
57
+ }
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ puts AmazingPrint::Formatters::GetChildItem.new ARGV[0] if __FILE__ == $PROGRAM_NAME