pry-auto_benching.rb 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c018cd4eeb0e2b222b7c8ecee196e667d1189a4e22700ff60c2f73e831550fcd
4
- data.tar.gz: bac2c715f28098373093d0c358dc5ed1cea4113a73bbb6ce2468d0742310bf8f
3
+ metadata.gz: 045cf80b47b7e90cd9b0ce925106b37e9280ca87ef74f603a264cfa442718b19
4
+ data.tar.gz: ebaf1241f1974405a91b6e2255561c43ff0b9401083eb6eccbff6c92769cf486
5
5
  SHA512:
6
- metadata.gz: aeedf31e5c738006c01766b53dffabeec79cfb5e1723f1db2217337081b4006f7fbf622272db1b8df69382bcc908ead3a260c580bbb46fff9667193ab5eb2293
7
- data.tar.gz: 58629e77f790a806a60b4febb8bf1f34627f618a5a9770a360a3507a63ee79669b2dc5e6650a02d28623c9012b20d3a31ff76b080814a09d8b37de20bcb3957e
6
+ metadata.gz: 126752554d260b8e977e86478aad4d8b251598762e9a4ab74fbbe7ee6d0f5f9bb22e76817c71b91e2605aec5067c5f26de2ed5c52fd880b87a2838210f6e683c
7
+ data.tar.gz: e5c63c874fb755a6f1f63f2e041ac6c80bb1cb94dc991133e2e61bb0aae584aaa24530f3819a980420c995ff79d6518961ea7864de66dc854251874459a992c2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v3.1.0
4
+
5
+ * Align the index, duration, and line of input in the output of
6
+ `auto-benching --past`.
7
+
8
+ * Improve `auto-benching --past` by modifying its output.
9
+
10
+ * Fix a typo shown when choosing an invalid target display.
11
+
3
12
  ## v3.0.0
4
13
 
5
14
  * Remove `:output` as a target display, and the related code that supported
data/README.md CHANGED
@@ -97,11 +97,12 @@ __3.__
97
97
 
98
98
  `auto-benching --past` shows past benchmark results:
99
99
 
100
- [1] 0.5s (main)> auto-benching --past
101
- 1. Net::HTTP.get_response URI.parse('https://github.com') (0.88s)
102
- 2. Net::HTTP.get_response URI.parse('https://www.youtube.com') (2.18s)
103
- 3. Net::HTTP.get_response URI.parse('https://www.github.com/ruby/ruby') (0.59s)
104
- 4. sleep 0.5 (0.5s)
100
+ [1] 1.30s (main)> auto-benching -past
101
+ 1 0.01 sleep 0.01
102
+ 2 0.02 sleep 0.02
103
+ 3 0.03 sleep 0.03
104
+ 4 1.30 sleep 1.3
105
+ 5 1.40 Net::HTTP.get_response(URI.parse('https://github.com'))
105
106
  [2] pry(main)>
106
107
 
107
108
  __4.__
@@ -1,50 +1,9 @@
1
1
  module Pry::AutoBenching
2
2
  require 'pry'
3
3
  require 'pry-auto_benching/version'
4
+ require 'pry-auto_benching/moment_list'
4
5
  require 'pry-auto_benching/pry_command'
5
6
 
6
- #
7
- # MomentList is an Array-like object who cycles its contents
8
- # when a max size is reached.
9
- #
10
- # @api private
11
- #
12
- MomentList = Class.new do
13
- include Enumerable
14
-
15
- def initialize(pry)
16
- @pry = pry
17
- @ary = []
18
- end
19
-
20
- def each
21
- @ary.each do |moment|
22
- yield moment
23
- end
24
- end
25
-
26
- def [](index)
27
- @ary[index]
28
- end
29
-
30
- def pop
31
- @ary.pop
32
- end
33
-
34
- def empty?
35
- @ary.empty?
36
- end
37
-
38
- def <<(other)
39
- @ary.shift if @ary.size + 1 > max_size
40
- @ary << other
41
- end
42
-
43
- def max_size
44
- @pry.config.auto_benching.max_history_size
45
- end
46
- end
47
-
48
7
  #
49
8
  # @api private
50
9
  #
@@ -53,21 +12,6 @@ module Pry::AutoBenching
53
12
  attr_accessor :duration
54
13
  end
55
14
 
56
- write_duration = -> (pry, duration) {
57
- auto_benching = pry.config.auto_benching
58
- target_display = auto_benching.target_display.to_s
59
- pry.config.forget(:prompt_name)
60
- if target_display == 'prompt'
61
- pry.config.prompt_name = "#{Pry::Helpers::Text.send(auto_benching.prompt_color, duration.to_s + 's')} "
62
- elsif target_display == 'none'
63
- # no op
64
- else
65
- pry.output.warn "_pry_.config.auto_benching.target_display has an invalid value " \
66
- "(#{auto_benching.target_display.inspect}). \n" \
67
- "It should be equal to ':output', ':prompt' or ':none'"
68
- end
69
- }
70
-
71
15
  @moments = Hash.new do |h, pry|
72
16
  h[pry.hash] = MomentList.new(pry)
73
17
  end
@@ -81,10 +25,10 @@ module Pry::AutoBenching
81
25
  @after_eval = ->(_, pry) do
82
26
  auto_benching = pry.config.auto_benching
83
27
  moment = @moments[pry.hash][-1]
84
- moment.duration = (sprintf "%.2f", Process.clock_gettime(auto_benching.clock_type) - moment.start_timestamp).to_f
28
+ moment.duration = Process.clock_gettime(auto_benching.clock_type) - moment.start_timestamp.to_f
85
29
  if auto_benching.display_duration_if.call(pry, moment.duration)
86
30
  moment.input = pry.input_ring.to_a[-1]
87
- write_duration.call(pry, moment.duration)
31
+ write_duration(pry, moment.duration)
88
32
  else
89
33
  @moments[pry.hash].pop
90
34
  pry.config.forget(:prompt_name)
@@ -140,6 +84,34 @@ module Pry::AutoBenching
140
84
  @moments
141
85
  end
142
86
 
87
+ #
88
+ # @api private
89
+ #
90
+ def self.write_duration(pry, duration)
91
+ auto_benching = pry.config.auto_benching
92
+ target = auto_benching.target_display.to_s
93
+ pry.config.forget(:prompt_name)
94
+ if target == 'prompt'
95
+ pry.config.prompt_name = Pry::Helpers::Text.send auto_benching.prompt_color,
96
+ format_duration(duration)
97
+ elsif target == 'none'
98
+ # no op
99
+ else
100
+ pry.output.warn "_pry_.config.auto_benching.target_display has an invalid value: \n" \
101
+ "#{auto_benching.target_display.inspect}\n" \
102
+ "Valid values are: " \
103
+ "#{Pry::AutoBenching::PryCommand::VALID_TARGET_DISPLAYS.join(', ')}"
104
+ end
105
+ end
106
+
107
+ #
108
+ # @api private
109
+ #
110
+ def self.format_duration(float)
111
+ duration = Kernel.sprintf("%.2f", float).ljust(4)
112
+ "#{duration}s "
113
+ end
114
+
143
115
  Pry.configure do |config|
144
116
  config.hooks.add_hook :before_session, @before_session.hash, @before_session
145
117
  config.auto_benching = Pry::Config.from_hash({
@@ -148,15 +120,7 @@ module Pry::AutoBenching
148
120
  target_display: :prompt,
149
121
  prompt_color: :green,
150
122
  clock_type: Process::CLOCK_MONOTONIC,
151
- display_duration_if: ->(pry, duration) { duration >= 0.01 },
152
- output_string: ->(pry, duration) {
153
- format("%{TextDesc} %{Duration}s", {
154
- :TextDesc => pry.color ?
155
- Pry::Helpers::Text.green("pry-auto_benching.rb:") :
156
- "pry-auto_benching.rb:",
157
- :Duration => sprintf("%.2f", duration)
158
- })
159
- }
123
+ display_duration_if: ->(pry, duration) { duration >= 0.01 }
160
124
  })
161
125
  end
162
126
  end
@@ -0,0 +1,41 @@
1
+ #
2
+ # MomentList is an Array-like object who cycles its contents
3
+ # when a max size is reached.
4
+ #
5
+ # @api private
6
+ #
7
+ class Pry::AutoBenching::MomentList
8
+ include Enumerable
9
+
10
+ def initialize(pry)
11
+ @pry = pry
12
+ @ary = []
13
+ end
14
+
15
+ def each
16
+ @ary.each do |moment|
17
+ yield moment
18
+ end
19
+ end
20
+
21
+ def [](index)
22
+ @ary[index]
23
+ end
24
+
25
+ def pop
26
+ @ary.pop
27
+ end
28
+
29
+ def empty?
30
+ @ary.empty?
31
+ end
32
+
33
+ def <<(other)
34
+ @ary.shift if @ary.size + 1 > max_size
35
+ @ary << other
36
+ end
37
+
38
+ def max_size
39
+ @pry.config.auto_benching.max_history_size
40
+ end
41
+ end
@@ -30,12 +30,9 @@ class Pry::AutoBenching::PryCommand < Pry::ClassCommand
30
30
 
31
31
  def process(command)
32
32
  case
33
- when opts.version?
34
- page(VERSION_STRING)
35
- when opts.present?('target-display')
36
- change_target_display(opts[:'target-display'])
37
- when opts.present?('past')
38
- process_past
33
+ when opts.version? then page(VERSION_STRING)
34
+ when opts.present?('target-display') then change_target_display(opts[:'target-display'])
35
+ when opts.present?('past') then read_past_benchmarks
39
36
  else
40
37
  if command == 'enable'
41
38
  Pry::AutoBenching.enable(_pry_)
@@ -52,16 +49,18 @@ class Pry::AutoBenching::PryCommand < Pry::ClassCommand
52
49
 
53
50
  private
54
51
 
55
- def process_past
52
+ def read_past_benchmarks
56
53
  moments = Pry::AutoBenching.moments[_pry_.hash]
57
54
  if moments.empty?
58
55
  page "No benchmark results are stored for this session."
59
56
  else
60
- page moments.map.with_index { |moment, i|
61
- format "%{index}. %{code} (%{duration})",
62
- index: blue(i + 1),
57
+ page moments.map.with_index {|moment,i|
58
+ format "%{i} %{duration}%{code}",
59
+ i: bold(i+1),
63
60
  code: moment.input.chomp,
64
- duration: bold("#{moment.duration}s")
61
+ duration: green(
62
+ Pry::AutoBenching.format_duration(moment.duration).sub('s', '')
63
+ )
65
64
  }.join("\n")
66
65
  end
67
66
  end
@@ -71,7 +70,7 @@ class Pry::AutoBenching::PryCommand < Pry::ClassCommand
71
70
  _pry_.config.auto_benching.target_display = target_display
72
71
  page "Display changed to '#{target_display}'"
73
72
  else
74
- raise Pry::CommandError, "'#{target_display}' is an invalid, " \
73
+ raise Pry::CommandError, "'#{target_display}' is invalid, " \
75
74
  "valid options are: #{VALID_TARGET_DISPLAYS.join(', ')}"
76
75
  end
77
76
  end
@@ -1,5 +1,5 @@
1
1
  class Pry
2
2
  module AutoBenching
3
- VERSION = "3.0.0"
3
+ VERSION = "3.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-auto_benching.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson
@@ -34,6 +34,7 @@ files:
34
34
  - LICENSE.txt
35
35
  - README.md
36
36
  - lib/pry-auto_benching.rb
37
+ - lib/pry-auto_benching/moment_list.rb
37
38
  - lib/pry-auto_benching/pry_command.rb
38
39
  - lib/pry-auto_benching/version.rb
39
40
  - pry-auto_benching.gemspec