Use _pry_.pager instead of Pry::Pager
This is not a concern for backward compatibility as it was introduced since the last release
This commit is contained in:
parent
01785a8020
commit
dd5e2511f3
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -85,7 +85,7 @@ class Pry
|
|||
:_pry_ => _pry_
|
||||
})
|
||||
|
||||
stagger_output(ls_entity.entities_table)
|
||||
_pry_.pager.page ls_entity.entities_table
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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!)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue