pry-auto_benching.rb 1.0.0 → 1.1.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: 0aefc25f86eed7cb38d19ae6128aa04d84e5318892566a69e6a5650e02f02eb3
4
- data.tar.gz: 47aeb7d378f28fa24eb1d6cce8fef5e5ff56c74aaa35b80d654df45631d56538
3
+ metadata.gz: a0612c1d96859e2c9d813bba4572dd26019406bea956909cf56fa3a9c7952b00
4
+ data.tar.gz: 622b628f5055fc504b97fa12a638a5c8344834d91c25bcbc5e417a85c659f7d8
5
5
  SHA512:
6
- metadata.gz: 963a145aacc484144627e1f119e571f267505451dd7261c0d7d8fb06c0805cdfda228f63a261ec63cdad98994193d522b7d45a1239ae386a01341cb12cc8bb04
7
- data.tar.gz: 3cf40ff58c699a02f7c2cee7c98ae2180dbadc3c7c7b876016d74d0eebdc9d94deb30661945a22d40c6958809d39b970c7c6dcdc3c79e7ee8b4cec4e3e3b37dc
6
+ metadata.gz: 42a4169779af83587da49fef60cc01e4b3f49425b61e783c1c12b4ad01db4875808a17f2cc93a2280f379648176a7b02e82ae8bef3c38e590a351d17e91ac1de
7
+ data.tar.gz: 57755f3a2d39f39ca0b585a8ec0283fc63f8d21bedf5904e17853873d7f4e4229229e0642886503e821a805f2ad6333ea621d7398b65ef2ac656899787daf038
data/README.md CHANGED
@@ -19,14 +19,22 @@ Pry.configure do |config|
19
19
  # See https://www.rubydoc.info/stdlib/core/Process:clock_gettime
20
20
  config.auto_benching.clock_type = Process::CLOCK_REALTIME
21
21
 
22
+ # Configure where benchmark results should be displayed, either
23
+ # in the prompt or written to stdout. Default is :prompt.
24
+ config.auto_benching.target_display = ':prompt | :stdout'
25
+
22
26
  # Benchmark results are printed depending on the return value
23
- # of this lambda, by default results are printed if duration > 0.0.
27
+ # of this lambda, by default results are printed if duration >= 0.1s
24
28
  config.auto_benching.speak_if = ->(pry, duration) {
25
29
  duration > 0.5
26
30
  }
27
31
 
28
- # A lambda for presenting the benchmark results.
29
- # Default is: "Benchmark: %.2fs".
32
+ # The color to use for benchmark results when `target_display` is :prompt.
33
+ # The default is :green.
34
+ config.auto_benching.prompt_color = :red
35
+
36
+ # A lambda for presenting the benchmark results when `target_display`
37
+ # is :stdout. Default is: "Benchmark: %.2fs".
30
38
  config.auto_benching.speaker = ->(pry, duration) {
31
39
  pry.pager.page sprintf("Elapsed time: %.2fs", duration)
32
40
  }
@@ -37,10 +45,10 @@ end
37
45
 
38
46
  __1.__
39
47
 
40
- Show help by running `pry-auto_benching -h`:
48
+ Show help by running `auto-benching -h`:
41
49
 
42
- [1] pry(main)> pry-auto_benching -h
43
- Usage: pry-auto_benching [enable|disable]
50
+ [1] pry(main)> auto-benching -h
51
+ Usage: auto-benching [enable|disable]
44
52
 
45
53
  Enable or disable benchmarking of input.
46
54
 
@@ -48,34 +56,34 @@ Show help by running `pry-auto_benching -h`:
48
56
 
49
57
  __2.__
50
58
 
51
- Disable auto benchmarking by running `pry-auto_benching disable`:
59
+ Disable auto benchmarking by running `auto-benching disable`:
52
60
 
53
- [1] pry(main)> sleep 2
54
- pry-auto_benching.rb: 2.00s
55
- => 2
56
- [2] pry(main)> pry-auto_benching disable
61
+ [1] pry(main)> auto-benching disable
57
62
  pry-auto_benching.rb: stopped benchmarking.
58
- [3] pry(main)> sleep 2
63
+ [2] pry(main)> sleep 2
59
64
  => 2
60
65
 
61
66
  __3.__
62
67
 
63
- Last example just shows the benchmark of a method that sleeps for a random
64
- number of seconds:
68
+ A screenshot, using `:prompt` as the target_display.
69
+ The prompt name (by default 'pry') is replaced with the benchmark results
70
+ for the previous expression. This is the default since it is subtle, yet
71
+ easy to spot in a sea of output.
72
+
73
+ ![s1](./s1.png)
65
74
 
66
- [3] pry(main)> def sleep_rand
67
- [3] pry(main)* sleep rand(10)
68
- [3] pry(main)* end
69
- => :sleep_rand
70
- [4] pry(main)> sleep_rand
71
- pry-auto_benching.rb: 7.00s
72
- => 7
75
+ __4.__
76
+
77
+ A screenshot, using `:stdout` as the target_display.
78
+ To switch to using this display, run `_pry_.config.auto_benching.target_display = :stdout`
79
+ inside the repl or set `Pry.config.auto_benching.target_display` from your .pryrc for a
80
+ permanent change.
81
+
82
+ ![s2](./s2.png)
73
83
 
74
84
  ## Install
75
85
 
76
- ```ruby
77
- gem "pry-auto_benching.rb", github: "r-obert/pry-auto_benching.rb"
78
- ```
86
+ gem install pry-auto_benching.rb
79
87
 
80
88
  ## License
81
89
 
@@ -1,5 +1,5 @@
1
1
  class Pry
2
2
  module AutoBenching
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -6,17 +6,36 @@ module Pry::AutoBenching
6
6
  MEMORY[pry.hash].push Process.clock_gettime(clock_type)
7
7
  end
8
8
 
9
+ write_result = ->(pry, duration) {
10
+ target_display = pry.config.auto_benching.target_display
11
+ if target_display == :prompt
12
+ prompt_color = pry.config.auto_benching.prompt_color
13
+ pry.config.prompt_name = Pry.lazy {
14
+ "#{Pry::Helpers::Text.send(prompt_color, duration.to_s + 's')} "
15
+ }
16
+ elsif target_display == :stdout
17
+ pry.config.auto_benching.speaker.call(pry, duration)
18
+ else
19
+ raise Pry::CommandError, "_pry_.config.auto_benching.target_display has an invalid value (#{target_display})"
20
+ end
21
+ }
22
+
9
23
  AFTER_EVAL = ->(input, pry) do
10
24
  clock_type = pry.config.auto_benching.clock_type
11
25
  duration = (sprintf "%.2f", Process.clock_gettime(clock_type) - MEMORY[pry.hash][-1]).to_f
12
26
  if input.nil? # Pry command
13
- pry.config.auto_benching.benchmark_commands and
14
- pry.config.auto_benching.speak_if.call(pry, duration) and
15
- pry.config.auto_benching.speaker.call(pry, duration)
27
+ if pry.config.auto_benching.benchmark_commands and
28
+ pry.config.auto_benching.speak_if.call(pry, duration)
29
+ write_result.(pry, duration)
30
+ end
16
31
  else
17
- pry.config.auto_benching.speak_if.call(pry, duration) and
18
- pry.config.auto_benching.speaker.call(pry, duration)
32
+ if pry.config.auto_benching.speak_if.call(pry, duration)
33
+ write_result.(pry, duration)
34
+ else
35
+ pry.config.prompt_name = 'pry'
36
+ end
19
37
  end
38
+ pry.config.prompt_name = 'pry' if pry.config.auto_benching.target_display != :prompt
20
39
  MEMORY[pry.hash].clear
21
40
  end
22
41
 
@@ -42,45 +61,49 @@ module Pry::AutoBenching
42
61
  pry.config.hooks.delete_hook :after_session , AFTER_SESSION.hash
43
62
  MEMORY[pry.hash].clear
44
63
  end
64
+ end
45
65
 
46
- require 'pry' if not defined?(Pry::ClassCommand)
47
- class AutoBenchingCommand < Pry::ClassCommand
48
- match 'pry-auto_benching'
49
- command_options argument_required: true
50
- group 'pry-auto_benching.rb'
51
- description 'Start or disable benchmarking.'
52
- banner <<-CMDBANNER
53
- Usage: pry-auto_benching [enable|disable]
66
+ require 'pry' if not defined?(Pry::ClassCommand)
67
+ Pry::Commands.add_command Class.new(Pry::ClassCommand) {
68
+ match 'auto-benching'
69
+ command_options argument_required: true
70
+ group 'pry-auto_benching.rb'
71
+ description 'Enable or disable benchmarking.'
72
+ banner <<-CMDBANNER
73
+ Usage: pry-auto_benching [enable|disable]
54
74
 
55
- Start or disable benchmarking code (or Pry commands).
56
- CMDBANNER
75
+ Enable or disable benchmarking code (or Pry commands).
76
+ CMDBANNER
57
77
 
58
- def process(command)
59
- if command == 'enable'
60
- Pry::AutoBenching.enable(_pry_)
61
- _pry_.pager.page('pry-auto_benching.rb: benchmarking.')
62
- elsif command == 'disable'
63
- Pry::AutoBenching.disable(_pry_)
64
- _pry_.pager.page('pry-auto_benching.rb: stopped benchmarking.')
65
- else
66
- raise Pry::CommandError, "'#{command}' is not implemented by this command, try -h for help."
67
- end
78
+ def process(command)
79
+ if command == 'enable'
80
+ Pry::AutoBenching.enable(_pry_)
81
+ _pry_.pager.page('pry-auto_benching.rb: benchmarking.')
82
+ elsif command == 'disable'
83
+ Pry::AutoBenching.disable(_pry_)
84
+ _pry_.pager.page('pry-auto_benching.rb: stopped benchmarking.')
85
+ else
86
+ raise Pry::CommandError, "'#{command}' is not implemented by this command, try -h for help."
68
87
  end
69
88
  end
89
+ }
70
90
 
71
- Pry::Commands.add_command(AutoBenchingCommand)
72
- Pry.config.hooks.add_hook :before_session, BEFORE_SESSION.hash, BEFORE_SESSION
73
- Pry.config.auto_benching = Pry::Config.from_hash({
91
+ Pry.configure do |config|
92
+ config.hooks.add_hook :before_session, Pry::AutoBenching::BEFORE_SESSION.hash,
93
+ Pry::AutoBenching::BEFORE_SESSION
94
+ config.auto_benching = Pry::Config.from_hash({
74
95
  enabled: true,
96
+ target_display: :prompt,
97
+ prompt_color: :green,
75
98
  clock_type: Process::CLOCK_MONOTONIC,
76
99
  benchmark_commands: false,
77
- speak_if: ->(pry, duration) { duration > 0.0 },
100
+ speak_if: ->(pry, duration) { duration >= 0.1 },
78
101
  speaker: ->(pry, duration) {
79
- pry.pager.page format("%{AutoBenchingSays} %{WallTime}s", {
102
+ pry.pager.page format("%{AutoBenchingSays} %{Duration}s", {
80
103
  :AutoBenchingSays => pry.color ?
81
104
  Pry::Helpers::Text.green("pry-auto_benching.rb:") :
82
105
  "pry-auto_benching.rb:",
83
- :WallTime => sprintf("%.2f", duration)
106
+ :Duration => sprintf("%.2f", duration)
84
107
  })
85
108
  }
86
109
  }, nil)
@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
4
4
  spec.version = Pry::AutoBenching::VERSION
5
5
  spec.authors = ["Robert Gleeson"]
6
6
  spec.email = "trebor.g@protonmail.com"
7
- spec.summary = "Pry plugin for automatically benchmarking input using wall clock time."
7
+ spec.summary = "Pry plugin that automatically benchmarks input given to Pry."
8
8
  spec.description = spec.summary
9
9
  spec.homepage = "https://github.com/r-obert/pry-auto_benching.rb"
10
10
  spec.licenses = ["MIT"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-auto_benching.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-13 00:00:00.000000000 Z
11
+ date: 2018-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.11'
27
- description: Pry plugin for automatically benchmarking input using wall clock time.
27
+ description: Pry plugin that automatically benchmarks input given to Pry.
28
28
  email: trebor.g@protonmail.com
29
29
  executables: []
30
30
  extensions: []
@@ -58,5 +58,5 @@ rubyforge_project:
58
58
  rubygems_version: 2.7.7
59
59
  signing_key:
60
60
  specification_version: 4
61
- summary: Pry plugin for automatically benchmarking input using wall clock time.
61
+ summary: Pry plugin that automatically benchmarks input given to Pry.
62
62
  test_files: []