diff --git a/CHANGELOG.md b/CHANGELOG.md index 8adf4941..a0359f18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,7 +83,7 @@ through a 'pry-local' config who, at times, acts as a 'pry-local store'. (#1096) * `rake pry` now accepts switches prefixed with `_` (e.g., `rake pry _v`) * Pagers now act like `IO`s and accept streaming output - * See `Pager.page` and `Pager.with_pager` + * See `_pry_.pager.page` and `_pry_.pager.opent` * The `Pry` class has been broken up into two smaller classes * `Pry` represents non-UI-specific session state, including the eval string * `Pry::REPL` controls the user-facing interface diff --git a/lib/pry.rb b/lib/pry.rb index 09306c9a..391cdf46 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -18,7 +18,7 @@ class Pry # The default print DEFAULT_PRINT = proc do |output, value, _pry_| - Pry::Pager.with_pager(output) do |pager| + _pry_.pager.open do |pager| pager.print _pry_.config.output_prefix Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1) end diff --git a/lib/pry/commands/cat.rb b/lib/pry/commands/cat.rb index 357b8b12..8ca537fe 100644 --- a/lib/pry/commands/cat.rb +++ b/lib/pry/commands/cat.rb @@ -41,7 +41,7 @@ class Pry FileFormatter.new(args.first, _pry_, opts).format end - stagger_output(output) + _pry_.pager.page output end def complete(search) diff --git a/lib/pry/commands/help.rb b/lib/pry/commands/help.rb index eb09e615..170c8cd7 100644 --- a/lib/pry/commands/help.rb +++ b/lib/pry/commands/help.rb @@ -49,7 +49,7 @@ class Pry end end - stagger_output(help_text.join("\n\n")) + _pry_.pager.page help_text.join("\n\n") end # Given a group name and an array of commands, @@ -121,7 +121,7 @@ class Pry # # @param [Pry::Command] command def display_command(command) - stagger_output command.new.help + _pry_.pager.page command.new.help end # Find a subset of a hash that matches the user's search term. diff --git a/lib/pry/commands/hist.rb b/lib/pry/commands/hist.rb index 820680a3..75643843 100644 --- a/lib/pry/commands/hist.rb +++ b/lib/pry/commands/hist.rb @@ -78,7 +78,7 @@ class Pry @history = @history.with_line_numbers end - Pry::Pager.with_pager(output) do |pager| + _pry_.pager.open do |pager| @history.print_to_output(pager, _pry_.config.color) end end diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 148b384e..9f0e68f2 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -85,7 +85,7 @@ class Pry :_pry_ => _pry_ }) - stagger_output(ls_entity.entities_table) + _pry_.pager.page ls_entity.entities_table end private diff --git a/lib/pry/commands/pry_backtrace.rb b/lib/pry/commands/pry_backtrace.rb index bac23514..6795aa32 100644 --- a/lib/pry/commands/pry_backtrace.rb +++ b/lib/pry/commands/pry_backtrace.rb @@ -17,8 +17,7 @@ class Pry BANNER def process - stagger_output text.bold('Backtrace:') << - "\n--\n" << _pry_.backtrace.join("\n") + _pry_.pager.page text.bold('Backtrace:') << "\n--\n" << _pry_.backtrace.join("\n") end end diff --git a/lib/pry/commands/raise_up.rb b/lib/pry/commands/raise_up.rb index 418d7c34..70ad0872 100644 --- a/lib/pry/commands/raise_up.rb +++ b/lib/pry/commands/raise_up.rb @@ -22,7 +22,7 @@ class Pry BANNER def process - return stagger_output help if captures[0] =~ /(-h|--help)\b/ + return _pry.pager.page help if captures[0] =~ /(-h|--help)\b/ # Handle 'raise-up', 'raise-up "foo"', 'raise-up RuntimeError, 'farble' in a rubyesque manner target.eval("_pry_.raise_up#{captures[0]}") end diff --git a/lib/pry/commands/ri.rb b/lib/pry/commands/ri.rb index 49434994..d29c426d 100644 --- a/lib/pry/commands/ri.rb +++ b/lib/pry/commands/ri.rb @@ -23,10 +23,14 @@ class Pry subclass = Class.new(RDoc::RI::Driver) # the hard way. subclass.class_eval do + def initialize(pager, opts) + @pager = pager + super opts + end def page paging_text = StringIO.new yield paging_text - Pry::Pager.page(paging_text.string) + @pager.page(paging_text.string) end def formatter(io) @@ -42,7 +46,7 @@ class Pry end # Spin-up an RI insance. - ri = RDoc::RI::PryDriver.new :use_stdout => true, :interactive => false + ri = RDoc::RI::PryDriver.new _pry_.pager, :use_stdout => true, :interactive => false begin ri.display_names [spec] # Get the documentation (finally!) diff --git a/lib/pry/commands/show_info.rb b/lib/pry/commands/show_info.rb index 8ee71d4e..92958cb4 100644 --- a/lib/pry/commands/show_info.rb +++ b/lib/pry/commands/show_info.rb @@ -27,7 +27,7 @@ class Pry end set_file_and_dir_locals(code_object.source_file) - stagger_output result + _pry_.pager.page result end # This method checks whether the `code_object` is a WrappedModule, diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 447a161a..de514ad9 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -66,7 +66,7 @@ class Pry if expressions.empty? output.puts "No watched expressions" else - Pry::Pager.with_pager(output) do |pager| + _pry_.pager.open do |pager| pager.puts "Listing all watched expressions:" pager.puts "" expressions.each_with_index do |expr, index| diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index 68b41678..e7df0e0d 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -91,7 +91,7 @@ class Pry out = "\n#{text.bold('From:')} #{location}:\n\n" << code.with_line_numbers(use_line_numbers?).with_marker(marker).highlighted(_pry_.config.color) << "\n" - stagger_output(out) + _pry_.pager.page out end private diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index b9f15fde..8b262dc4 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -108,20 +108,9 @@ class Pry # Send the given text through the best available pager (if Pry.config.pager is # enabled). Infers where to send the output if used as a mixin. + # DEPRECATED. def stagger_output(text, out = nil) - out ||= case - when respond_to?(:output) - # Mixin. - output - when Pry.respond_to?(:output) - # Parent. - Pry.config.output - else - # Sys. - $stdout - end - - Pry::Pager.page(text, out) + Pry::Pager.new(Pry.config.pager, out || Pry.config.output).page text end end end diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb index dae67f71..d245941e 100644 --- a/lib/pry/pager.rb +++ b/lib/pry/pager.rb @@ -3,16 +3,24 @@ require 'pry/terminal' # A pager is an `IO`-like object that accepts text and either prints it # immediately, prints it one page at a time, or streams it to an external # program to print one page at a time. -module Pry::Pager +class Pry::Pager class StopPaging < StandardError end + attr_reader :_pry_ + + def initialize(_pry_) + @_pry_ = _pry_ + end + # Send the given text through the best available pager (if `Pry.config.pager` is # enabled). + # If you want to send text through in chunks as you generate it, use `open` to + # get a writable object instead. # @param [String] text A piece of text to run through a pager. # @param [IO] output (`$stdout`) An object to send output to. - def self.page(text, output = $stdout) - with_pager(output) do |pager| + def page(text) + open do |pager| pager << text end end @@ -20,28 +28,33 @@ module Pry::Pager # Yields a pager object (`NullPager`, `SimplePager`, or `SystemPager`). All # pagers accept output with `#puts`, `#print`, `#write`, and `#<<`. # @param [IO] output (`$stdout`) An object to send output to. - def self.with_pager(output = $stdout) - pager = best_available(output) + def open + pager = best_available yield pager rescue StopPaging ensure pager.close if pager end + private + + attr_reader :output + def enabled?; !!@enabled; end + # Return an instance of the "best" available pager class -- `SystemPager` if # possible, `SimplePager` if `SystemPager` isn't available, and `NullPager` # if the user has disabled paging. All pagers accept output with `#puts`, # `#print`, `#write`, and `#<<`. You must call `#close` when you're done # writing output to a pager, and you must rescue `Pry::Pager::StopPaging`. - # These requirements can be avoided by using `.with_pager` instead. + # These requirements can be avoided by using `.open` instead. # @param [#<<] output ($stdout) An object to send output to. - def self.best_available(output) - if !Pry.config.pager - NullPager.new(output) + def best_available + if !_pry_.config.pager + NullPager.new(_pry_.config.output) elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby? - SimplePager.new(output) + SimplePager.new(_pry_.config.output) else - SystemPager.new(output) + SystemPager.new(_pry_.config.output) end end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index cc9582b2..fbbdccdd 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -596,6 +596,13 @@ class Pry prompt_stack.size > 1 ? prompt_stack.pop : prompt end + # Returns the currently configured pager + # @example + # _pry_.pager.page text + def pager + Pry::Pager.new(self) + end + # Raise an exception out of Pry. # # See Kernel#raise for documentation of parameters. diff --git a/lib/pry/test/helper.rb b/lib/pry/test/helper.rb index d0976f59..8ddf1446 100644 --- a/lib/pry/test/helper.rb +++ b/lib/pry/test/helper.rb @@ -63,7 +63,8 @@ module PryTestHelpers def mock_command(cmd, args=[], opts={}) output = StringIO.new - ret = cmd.new(opts.merge(:output => output)).call_safely(*args) + pry = Pry.new(output: output) + ret = cmd.new(opts.merge(pry_instance: pry, :output => output)).call_safely(*args) Struct.new(:output, :return).new(output.string, ret) end diff --git a/spec/command_set_spec.rb b/spec/command_set_spec.rb index ebd4963b..8e7eee38 100644 --- a/spec/command_set_spec.rb +++ b/spec/command_set_spec.rb @@ -9,7 +9,7 @@ describe Pry::CommandSet do @ctx = { :target => binding, :command_set => @set, - :pry_instance => Pry.new + :pry_instance => Pry.new(output: StringIO.new) } end diff --git a/spec/command_spec.rb b/spec/command_spec.rb index 7ce34737..f493f14f 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -144,7 +144,7 @@ describe "Pry::Command" do :output => StringIO.new, :eval_string => "eval-string", :command_set => @set, - :pry_instance => Object.new + :pry_instance => Pry.new } it 'should capture lots of stuff from the hash passed to new before setup' do diff --git a/spec/pry_output_spec.rb b/spec/pry_output_spec.rb index 7c59e5f3..dc594003 100644 --- a/spec/pry_output_spec.rb +++ b/spec/pry_output_spec.rb @@ -37,6 +37,7 @@ describe Pry do it "should include the =>" do pry = Pry.new accumulator = StringIO.new + pry.config.output = accumulator pry.config.print.call(accumulator, [1], pry) accumulator.string.should == "=> \[1\]\n" end @@ -54,6 +55,7 @@ describe Pry do it "should be able to change output_prefix" do pry = Pry.new accumulator = StringIO.new + pry.config.output = accumulator pry.config.output_prefix = "-> " pry.config.print.call(accumulator, [1], pry) accumulator.string.should == "-> \[1\]\n" @@ -73,6 +75,7 @@ describe Pry do pry = Pry.new accumulator = StringIO.new colorized = CodeRay.scan("[1]", :ruby).term + pry.config.output = accumulator pry.config.print.call(accumulator, [1], pry) accumulator.string.should == "=> #{colorized}\n" end @@ -84,6 +87,7 @@ describe Pry do "\e[1;31mFoo\e[0m" end accumulator = StringIO.new + pry.config.output = accumulator pry.config.print.call(accumulator, f, pry) # We add an extra \e[0m to prevent color leak accumulator.string.should == "=> \e[1;31mFoo\e[0m\e[0m\n"