diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2018-10-20 00:46:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-20 00:46:14 +0800 |
commit | e3b579adeffeccc3e5b2445a64bb249c4c750dd2 (patch) | |
tree | 8a7279c8cf10bd79713887632789198738388729 | |
parent | d37d0c0489f52659238a04ec3e9e7906999d1269 (diff) | |
parent | 9622b00689a6b3ccf969153f36b23f46341ba416 (diff) | |
download | pry-e3b579adeffeccc3e5b2445a64bb249c4c750dd2.tar.gz |
Merge pull request #1814 from pry/history-array-refactoring
Rename HistoryArray to Ring
-rw-r--r-- | lib/pry.rb | 10 | ||||
-rw-r--r-- | lib/pry/commands/cat.rb | 2 | ||||
-rw-r--r-- | lib/pry/commands/code_collector.rb | 8 | ||||
-rw-r--r-- | lib/pry/commands/edit.rb | 6 | ||||
-rw-r--r-- | lib/pry/commands/gist.rb | 4 | ||||
-rw-r--r-- | lib/pry/pry_instance.rb | 34 | ||||
-rw-r--r-- | lib/pry/ring.rb (renamed from lib/pry/history_array.rb) | 25 | ||||
-rw-r--r-- | spec/pry_spec.rb | 4 | ||||
-rw-r--r-- | spec/ring_spec.rb (renamed from spec/history_array_spec.rb) | 20 |
9 files changed, 57 insertions, 56 deletions
@@ -64,11 +64,11 @@ class Pry # The default prompt; includes the target and nesting level DEFAULT_PROMPT = [ proc { |target_self, nest_level, pry| - "[#{pry.input_array.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> " + "[#{pry.input_ring.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> " }, proc { |target_self, nest_level, pry| - "[#{pry.input_array.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* " + "[#{pry.input_ring.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* " } ] @@ -89,11 +89,11 @@ class Pry NAV_PROMPT = [ proc do |_, _, _pry_| tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / " - "[#{_pry_.input_array.count}] (#{_pry_.config.prompt_name}) #{tree}: #{_pry_.binding_stack.size - 1}> " + "[#{_pry_.input_ring.count}] (#{_pry_.config.prompt_name}) #{tree}: #{_pry_.binding_stack.size - 1}> " end, proc do |_, _, _pry_| tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / " - "[#{_pry_.input_array.count}] (#{ _pry_.config.prompt_name}) #{tree}: #{_pry_.binding_stack.size - 1}* " + "[#{_pry_.input_ring.count}] (#{ _pry_.config.prompt_name}) #{tree}: #{_pry_.binding_stack.size - 1}* " end, ] @@ -144,7 +144,7 @@ require 'pathname' require 'pry/version' require 'pry/repl' require 'pry/code' -require 'pry/history_array' +require 'pry/ring' require 'pry/helpers' require 'pry/code_object' require 'pry/method' diff --git a/lib/pry/commands/cat.rb b/lib/pry/commands/cat.rb index 8ea71861..a039c566 100644 --- a/lib/pry/commands/cat.rb +++ b/lib/pry/commands/cat.rb @@ -36,7 +36,7 @@ class Pry when opts.present?(:ex) ExceptionFormatter.new(_pry_.last_exception, _pry_, opts).format when opts.present?(:in) - InputExpressionFormatter.new(_pry_.input_array, opts).format + InputExpressionFormatter.new(_pry_.input_ring, opts).format else FileFormatter.new(args.first, _pry_, opts).format end diff --git a/lib/pry/commands/code_collector.rb b/lib/pry/commands/code_collector.rb index 2604f193..599d287c 100644 --- a/lib/pry/commands/code_collector.rb +++ b/lib/pry/commands/code_collector.rb @@ -87,22 +87,22 @@ class Pry Array(content.lines.to_a[range]).join end - # The selected `_pry_.output_array` as a string, as specified by + # The selected `_pry_.output_ring` as a string, as specified by # the `-o` switch. # # @return [String] def pry_output_content - pry_array_content_as_string(_pry_.output_array, self.class.output_result_ranges) do |v| + pry_array_content_as_string(_pry_.output_ring, self.class.output_result_ranges) do |v| _pry_.config.gist.inspecter.call(v) end end - # The selected `_pry_.input_array` as a string, as specified by + # The selected `_pry_.input_ring` as a string, as specified by # the `-i` switch. # # @return [String] def pry_input_content - pry_array_content_as_string(_pry_.input_array, self.class.input_expression_ranges) { |v| v } + pry_array_content_as_string(_pry_.input_ring, self.class.input_expression_ranges) { |v| v } end # The line range passed to `--lines`, converted to a 0-indexed range. diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index 65b61ee3..b536cab8 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -157,9 +157,9 @@ class Pry def input_expression case opts[:i] when Range - (_pry_.input_array[opts[:i]] || []).join + (_pry_.input_ring[opts[:i]] || []).join when Integer - _pry_.input_array[opts[:i]] || "" + _pry_.input_ring[opts[:i]] || "" else raise Pry::CommandError, "Not a valid range: #{opts[:i]}" end @@ -186,7 +186,7 @@ class Pry when eval_string.strip != "" eval_string else - _pry_.input_array.reverse_each.find { |x| x && x.strip != "" } || "" + _pry_.input_ring.reverse_each.find { |x| x && x.strip != "" } || "" end end diff --git a/lib/pry/commands/gist.rb b/lib/pry/commands/gist.rb index 248cb9bd..ef2b5e0a 100644 --- a/lib/pry/commands/gist.rb +++ b/lib/pry/commands/gist.rb @@ -53,13 +53,13 @@ class Pry def input_content content = "" CodeCollector.input_expression_ranges.each do |range| - input_expressions = _pry_.input_array[range] || [] + input_expressions = _pry_.input_ring[range] || [] Array(input_expressions).each_with_index do |code, index| corrected_index = index + range.first if code && code != "" content << code if code !~ /;\Z/ - content << "#{comment_expression_result_for_gist(_pry_.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}" + content << "#{comment_expression_result_for_gist(_pry_.config.gist.inspecter.call(_pry_.output_ring[corrected_index]))}" end end end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 16b4ec99..c102acc9 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -34,8 +34,8 @@ class Pry attr_reader :last_exception attr_reader :command_state attr_reader :exit_value - attr_reader :input_array - attr_reader :output_array + attr_reader :input_ring + attr_reader :output_ring attr_reader :config extend Pry::Config::Convenience @@ -72,11 +72,11 @@ class Pry @config = Pry::Config.new config.merge!(options) push_prompt(config.prompt) - @input_array = Pry::HistoryArray.new config.memory_size - @output_array = Pry::HistoryArray.new config.memory_size + @input_ring = Pry::Ring.new(config.memory_size) + @output_ring = Pry::Ring.new(config.memory_size) @custom_completions = config.command_completions set_last_result nil - @input_array << nil + @input_ring << nil push_initial_binding(target) exec_hook(:when_started, target, options, self) end @@ -172,13 +172,13 @@ class Pry # @return [Integer] The maximum amount of objects remembered by the inp and # out arrays. Defaults to 100. def memory_size - @output_array.max_size + @output_ring.max_size end undef :memory_size= if method_defined? :memory_size= def memory_size=(size) - @input_array = Pry::HistoryArray.new(size) - @output_array = Pry::HistoryArray.new(size) + @input_ring = Pry::Ring.new(size) + @output_ring = Pry::Ring.new(size) end # Inject all the sticky locals into the current binding. @@ -198,14 +198,14 @@ class Pry end def sticky_locals - { _in_: input_array, - _out_: output_array, + { _in_: input_ring, + _out_: output_ring, _pry_: self, _ex_: last_exception && last_exception.wrapped_exception, _file_: last_file, _dir_: last_dir, _: proc { last_result }, - __: proc { output_array[-2] } + __: proc { output_ring[-2] } }.merge(config.extra_sticky_locals) end @@ -483,7 +483,7 @@ class Pry # @param [String] code The code that was run. def set_last_result(result, code="") @last_result_is_exception = false - @output_array << result + @output_ring << result self.last_result = result unless code =~ /\A\s*\z/ end @@ -497,7 +497,7 @@ class Pry def last_exception=(e) last_exception = Pry::LastException.new(e) @last_result_is_exception = true - @output_array << last_exception + @output_ring << last_exception @last_exception = last_exception end @@ -505,8 +505,8 @@ class Pry # This method should not need to be invoked directly. # @param [String] code The code we just eval'd def update_input_history(code) - # Always push to the @input_array as the @output_array is always pushed to. - @input_array << code + # Always push to the @input_ring as the @output_ring is always pushed to. + @input_ring << code if code Pry.line_buffer.push(*code.each_line) Pry.current_line += code.lines.count @@ -539,10 +539,10 @@ class Pry open_token: open_token, session_line: Pry.history.session_line_count + 1, history_line: Pry.history.history_line_count + 1, - expr_number: input_array.count, + expr_number: input_ring.count, _pry_: self, binding_stack: binding_stack, - input_array: input_array, + input_ring: input_ring, eval_string: @eval_string, cont: !@eval_string.empty? }) diff --git a/lib/pry/history_array.rb b/lib/pry/ring.rb index ad476011..de198c08 100644 --- a/lib/pry/history_array.rb +++ b/lib/pry/ring.rb @@ -1,19 +1,19 @@ class Pry - # A history array is an array to which you can only add elements. Older - # entries are removed progressively, so that the array never contains more than - # N elements. + # A ring is an array to which you can only add elements. Older entries are + # removed progressively, so that the array never contains more than N + # elements. # - # History arrays are used by Pry to store the output of the last commands. + # Rings are used by Pry to store the output of the last commands. # # @example - # ary = Pry::HistoryArray.new 10 - # ary << 1 << 2 << 3 - # ary[0] # => 1 - # ary[1] # => 2 - # 10.times { |n| ary << n } - # ary[0] # => nil - # ary[-1] # => 9 - class HistoryArray + # ring = Pry::Ring.new(10) + # ring << 1 << 2 << 3 + # ring[0] # => 1 + # ring[1] # => 2 + # 10.times { |n| ring << n } + # ring[0] # => nil + # ring[-1] # => 9 + class Ring include Enumerable # @param [Integer] size Maximum amount of objects in the array @@ -103,6 +103,7 @@ class Pry attr_reader :max_size private + def convert_index(n) n >= 0 ? n : @count + n end diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index a727ae52..3be96b06 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -273,7 +273,7 @@ describe Pry do t.eval "42" res = t.eval "_out_" - expect(res).to be_a_kind_of Pry::HistoryArray + expect(res).to be_a_kind_of(Pry::Ring) expect(res[1..2]).to eq [:foo, 42] end @@ -283,7 +283,7 @@ describe Pry do t.eval "42" res = t.eval "_in_" - expect(res).to be_a_kind_of Pry::HistoryArray + expect(res).to be_a_kind_of(Pry::Ring) expect(res[1..2]).to eq [":foo\n", "42\n"] end diff --git a/spec/history_array_spec.rb b/spec/ring_spec.rb index 2d7d7b8b..65630d12 100644 --- a/spec/history_array_spec.rb +++ b/spec/ring_spec.rb @@ -1,13 +1,13 @@ require_relative 'helper' -describe Pry::HistoryArray do +describe Pry::Ring do before do - @array = Pry::HistoryArray.new 10 - @populated = @array.dup << 1 << 2 << 3 << 4 + @ring = Pry::Ring.new(10) + @populated = @ring.dup << 1 << 2 << 3 << 4 end it 'should have a maximum size specifed at creation time' do - expect(@array.max_size).to eq 10 + expect(@ring.max_size).to eq 10 end it 'should be able to be added objects to' do @@ -48,16 +48,16 @@ describe Pry::HistoryArray do end it 'should remove older entries' do - 11.times { |n| @array << n } + 11.times { |n| @ring << n } - expect(@array[0]).to eq nil - expect(@array[1]).to eq 1 - expect(@array[10]).to eq 10 + expect(@ring[0]).to eq nil + expect(@ring[1]).to eq 1 + expect(@ring[10]).to eq 10 end it 'should not be larger than specified maximum size' do - 12.times { |n| @array << n } - expect(@array.entries.compact.size).to eq 10 + 12.times { |n| @ring << n } + expect(@ring.entries.compact.size).to eq 10 end it 'should pop!' do |