betterp 0.1.0 → 0.1.5

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
- SHA1:
3
- metadata.gz: 55c8d01c5d2b646fe695c25f21fcbf3087256693
4
- data.tar.gz: c41bbb1ac04217b8deb4012d928ef54fb0d19e1c
2
+ SHA256:
3
+ metadata.gz: e5abc15316aa50a89ef3f7da15d2a6a48d27ec84ab57b82633a9cd1d67e644e2
4
+ data.tar.gz: ca846c0f3140aa40a562ad21b9679de04425e9d33cb6e6ee1258529555bfc60a
5
5
  SHA512:
6
- metadata.gz: fded6c3023ff132759b372eefc9627e148625fb1276c044ed425ebfc026869dd9d69ffc3a08402ae51200cbb1e76971e488a4e7ca9ee314351f0ffa2ad776929
7
- data.tar.gz: d6d04561089b7d778b8e31d8638b71372352f4636357a81d17cf0a3413718dc95a0ccfa5688f690b6a4b9bcdf2e8b50ec7a04f10966e64df0f231c722d1afb3a
6
+ metadata.gz: e1bc1be485f36644cd93c2f180a1ba494f676cb89886abadaafdbfc0174f3e55629274d9e4c20b43745e9a10ca060f8b5a6573754526b37da0f417f64ee464f9
7
+ data.tar.gz: 0af13aa85b55f786590fdb57ca8dad23941669c8ca4c20045f35f1f7d0b9c71adbb9ae8c393b0e1a0f0f34b8f19436fa580ee24331263274393d968e7774521c
data/.gitignore CHANGED
@@ -2,15 +2,15 @@
2
2
  /.yardoc
3
3
  /_yardoc/
4
4
  /coverage/
5
- /doc/
6
5
  /pkg/
7
6
  /spec/reports/
8
7
  /tmp/
9
8
 
10
9
  Gemfile.lock
11
10
 
12
- # rspec failure tracking
11
+ .ruby-version
13
12
  .rspec_status
14
13
  .byebug_history
15
14
  *.swp
16
15
  *.swo
16
+ betterp-*.gem
@@ -0,0 +1,18 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Style/StringLiterals:
5
+ Exclude:
6
+ - 'lib/betterp/output.rb'
7
+
8
+ Style/FormatString:
9
+ Exclude:
10
+ - 'lib/betterp/output.rb'
11
+
12
+ Style/FormatStringToken:
13
+ Exclude:
14
+ - 'lib/betterp/output.rb'
15
+
16
+ AllCops:
17
+ Exclude:
18
+ - 'bin/**/*'
@@ -0,0 +1,5 @@
1
+ .PHONY: test
2
+ test:
3
+ bin/rspec
4
+ bin/rubocop
5
+ bin/strong_versions
data/README.md CHANGED
@@ -16,7 +16,7 @@ The original semantics of `Kernel#p` are still applied, i.e. it returns the valu
16
16
  Add the gem to your `Gemfile`
17
17
 
18
18
  ```ruby
19
- gem 'strong_versions', '~> 0.3.0'
19
+ gem 'betterp', '~> 0.1.5'
20
20
  ```
21
21
 
22
22
  And rebuild your bundle:
@@ -25,9 +25,9 @@ And rebuild your bundle:
25
25
  $ bundle install
26
26
  ```
27
27
 
28
- Or install yourself:
28
+ Or install standalone:
29
29
  ```bash
30
- $ gem install strong_versions -v '0.3.0'
30
+ $ gem install betterp -v '0.1.5'
31
31
  ```
32
32
 
33
33
  ## Usage
@@ -35,6 +35,8 @@ $ gem install strong_versions -v '0.3.0'
35
35
  Call `p` from anywhere in your code just as you normally would:
36
36
 
37
37
  ```ruby
38
+ require 'betterp'
39
+
38
40
  p 'hello'
39
41
  ```
40
42
 
@@ -6,6 +6,7 @@ require 'betterp/version'
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'betterp'
9
+ spec.licenses = ['MIT']
9
10
  spec.version = Betterp::VERSION
10
11
  spec.authors = ['Bob Farrell']
11
12
  spec.email = ['bob@homeflow.co.uk']
@@ -28,5 +29,7 @@ Gem::Specification.new do |spec|
28
29
  spec.add_development_dependency 'bundler', '~> 1.16'
29
30
  spec.add_development_dependency 'rake', '~> 10.0'
30
31
  spec.add_development_dependency 'rspec', '~> 3.0'
31
- spec.add_development_dependency 'rubocop', '~> 0.60.0'
32
+ spec.add_development_dependency 'rspec-its', '~> 1.2'
33
+ spec.add_development_dependency 'rubocop', '~> 0.77.0'
34
+ spec.add_development_dependency 'strong_versions', '~> 0.3.2'
32
35
  end
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'strong_versions' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("strong_versions", "strong_versions")
Binary file
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'betterp'
4
+
5
+ class MyCustomClass
6
+ end
7
+
8
+ p 'hello'
9
+ p 'hi'
10
+ p 'debug output'
11
+ p 'hello', 'bob', 'how', 'are', 'you'
12
+ p(i: 'am', a: 'hash')
13
+ p MyCustomClass.new
14
+ p 'hello again'
15
+ p 'etc.'
16
+
17
+ def foo
18
+ p 'hi from a method'
19
+ end
20
+
21
+ foo
@@ -1,12 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'digest'
4
+ require 'pathname'
5
+ require 'pp'
6
+ require 'stringio'
4
7
 
5
8
  require 'paint'
6
9
 
7
10
  require 'kernel'
8
11
  require 'betterp/output'
12
+ require 'betterp/source'
9
13
  require 'betterp/version'
10
14
 
11
15
  module Betterp
16
+ def self.root
17
+ Pathname.new(File.dirname(__dir__))
18
+ end
12
19
  end
@@ -1,38 +1,100 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Betterp
2
4
  class Output
3
- COLORS = %i[
4
- red
5
- green
6
- yellow
7
- blue
8
- magenta
9
- cyan
10
- ].freeze
11
-
5
+ COLORS = %i[red green yellow blue magenta cyan].freeze
12
6
  EFFECTS = [:bright, nil].freeze
13
7
 
14
- def initialize(source)
8
+ def initialize(raw, source, options = {})
9
+ @raw = raw
15
10
  @source = source
11
+ @color = color
12
+ @effect = effect
13
+ @pretty = options.fetch(:pretty, false)
16
14
  end
17
15
 
18
16
  def format(args)
19
- args.map do |arg|
20
- colorize("#{@source}: ") + arg.inspect
17
+ args.map(&:inspect).map do |arg|
18
+ style = %i[yellow]
19
+ header + colorize(prefix) + caller_code + Paint[pretty(arg), *style]
21
20
  end
22
21
  end
23
22
 
24
23
  private
25
24
 
25
+ def pretty(arg)
26
+ return arg unless @pretty
27
+
28
+ io = StringIO.new
29
+ PP.pp(arg, io)
30
+ return io.string unless io.string.include?("\n")
31
+
32
+ "\n" + io.string.split("\n").map { |line| " #{line}" }.join("\n")
33
+ end
34
+
35
+ def caller_code
36
+ return '' unless @raw.include?(':')
37
+
38
+ path, line, *_rest = @raw.split(':')
39
+ return '' unless Pathname.new(path).readable? && line.to_i.positive?
40
+
41
+ Paint % [
42
+ +'%{open}%{code}%{close}',
43
+ :default,
44
+ open: ['{ ', :white, :default],
45
+ code: [find_caller(line.to_i, path).to_s.strip, :cyan],
46
+ close: [' } ', :white, :default]
47
+ ]
48
+ end
49
+
50
+ def find_caller(line_number, path)
51
+ lines = File.readlines(path)
52
+ token = @pretty ? 'pp' : 'p'
53
+ lines[0...line_number].reverse.find { |line| line.match(/\b#{token}\b/) }
54
+ end
55
+
56
+ def header
57
+ Paint % [
58
+ +"%{standard}%{relevant}",
59
+ :default,
60
+ standard: [' ', :default],
61
+ relevant: ['•••• ', @color, @effect]
62
+ ]
63
+ end
64
+
65
+ def prefix
66
+ [
67
+ '%{path}',
68
+ '%{separator}',
69
+ '%{line_no}',
70
+ '%{method_pointer}',
71
+ '%{method_name}',
72
+ '%{terminator}'
73
+ ].join
74
+ end
75
+
26
76
  def colorize(string)
27
- Paint[string, color, effect]
77
+ Paint % [string, :default, mapping]
78
+ end
79
+
80
+ def mapping
81
+ {
82
+ path: [@source.path],
83
+ line_no: [@source.line_no],
84
+ method_name: [@source.method_name],
85
+
86
+ method_pointer: [' => ', :reset, :bright],
87
+ separator: [':', :reset],
88
+ terminator: [' :: ', :reset]
89
+ }
28
90
  end
29
91
 
30
92
  def color
31
- COLORS[hash(@source).hex % COLORS.size]
93
+ COLORS[hash(@raw).hex % COLORS.size]
32
94
  end
33
95
 
34
96
  def effect
35
- EFFECTS[hash(hash(@source)).hex % EFFECTS.size]
97
+ EFFECTS[hash(hash(@raw)).hex % EFFECTS.size]
36
98
  end
37
99
 
38
100
  def hash(input)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Betterp
4
+ class Source
5
+ def initialize(source, base_path)
6
+ @source = source
7
+ @base_path = base_path
8
+ @path, @line_no, @method_info = source.split(':')
9
+ end
10
+
11
+ def path
12
+ Pathname.new(@path).relative_path_from(Pathname.new(@base_path)).to_s
13
+ rescue ArgumentError
14
+ @path
15
+ end
16
+
17
+ def line_no
18
+ @line_no.to_i
19
+ end
20
+
21
+ def method_name
22
+ @method_info.partition('`').last.chomp("'")
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Betterp
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.5'
5
5
  end
@@ -1,8 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kernel
2
4
  def p(*args)
3
- Betterp::Output.new(caller(1..1).first).format(args).each do |output|
4
- STDOUT.write(output + "\n")
5
+ raw = caller(1..1).first
6
+ _betterp(raw, args)
7
+ end
8
+
9
+ def pp(*args)
10
+ raw = caller(1..1).first
11
+ _betterp(raw, args, pretty: true)
12
+ end
13
+
14
+ def _betterp(raw, args, options = {})
15
+ source = Betterp::Source.new(raw, Dir.pwd)
16
+ pretty = options.fetch(:pretty, false)
17
+
18
+ Betterp::Output.new(raw, source, pretty: pretty).format(args).each do |str|
19
+ STDOUT.write(str + "\n")
5
20
  end
21
+
6
22
  args.size > 1 ? args : args.first
7
23
  end
8
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Farrell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-08 00:00:00.000000000 Z
11
+ date: 2020-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paint
@@ -66,20 +66,48 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.2'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rubocop
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: 0.60.0
89
+ version: 0.77.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.77.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: strong_versions
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.3.2
76
104
  type: :development
77
105
  prerelease: false
78
106
  version_requirements: !ruby/object:Gem::Requirement
79
107
  requirements:
80
108
  - - "~>"
81
109
  - !ruby/object:Gem::Version
82
- version: 0.60.0
110
+ version: 0.3.2
83
111
  description: Replaces Kernel#p with a fancier version
84
112
  email:
85
113
  - bob@homeflow.co.uk
@@ -89,19 +117,28 @@ extra_rdoc_files: []
89
117
  files:
90
118
  - ".gitignore"
91
119
  - ".rspec"
120
+ - ".rubocop.yml"
92
121
  - Gemfile
93
122
  - LICENSE
123
+ - Makefile
94
124
  - README.md
95
125
  - Rakefile
96
126
  - betterp.gemspec
97
127
  - bin/console
128
+ - bin/rspec
129
+ - bin/rubocop
98
130
  - bin/setup
131
+ - bin/strong_versions
132
+ - doc/images/screenshot.png
133
+ - doc/testp.rb
99
134
  - lib/betterp.rb
100
135
  - lib/betterp/output.rb
136
+ - lib/betterp/source.rb
101
137
  - lib/betterp/version.rb
102
138
  - lib/kernel.rb
103
139
  homepage: https://github.com/bobf/betterp
104
- licenses: []
140
+ licenses:
141
+ - MIT
105
142
  metadata: {}
106
143
  post_install_message:
107
144
  rdoc_options: []
@@ -118,8 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
155
  - !ruby/object:Gem::Version
119
156
  version: '0'
120
157
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 2.5.2.2
158
+ rubygems_version: 3.0.3
123
159
  signing_key:
124
160
  specification_version: 4
125
161
  summary: Enhanced debug output