1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

pager.rb: fix Helpers constant invocation

Since the top class is Pry::Pager and not Pry, the cref constant static lookup
only look for constants under Pry::Pager (or at the top level).

Since Helpers refers to Pry::Helpers, it won't be found. Fix this by
changing the top class to Pry and put the class Pager in the Pry
namespace.

This introduce a two space shift of all the code, so reformat the
comments.

This also fixes the following bug: in SystemPager.available? the Helpers
constant lookup fails, so this raises an exception which is rescued and
then returns false; so the SimplePager is always chosen instead.
This commit is contained in:
Damien Robert 2018-11-07 11:49:14 +01:00
parent 7209409091
commit bd07d11c1b

View file

@ -3,7 +3,8 @@ require 'pry/terminal'
# A pager is an `IO`-like object that accepts text and either prints it # 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 # immediately, prints it one page at a time, or streams it to an external
# program to print one page at a time. # program to print one page at a time.
class Pry::Pager class Pry
class Pager
class StopPaging < StandardError class StopPaging < StandardError
end end
@ -13,9 +14,10 @@ class Pry::Pager
@_pry_ = _pry_ @_pry_ = _pry_
end end
# Send the given text through the best available pager (if `Pry.config.pager` is # Send the given text through the best available pager (if
# enabled). If you want to send text through in chunks as you generate it, use `open` # `Pry.config.pager` is enabled). If you want to send text through in
# to get a writable object instead. # chunks as you generate it, use `open` to get a writable object
# instead.
# #
# @param [String] text # @param [String] text
# Text to run through a pager. # Text to run through a pager.
@ -26,8 +28,8 @@ class Pry::Pager
end end
end end
# Yields a pager object (`NullPager`, `SimplePager`, or `SystemPager`). All # Yields a pager object (`NullPager`, `SimplePager`, or `SystemPager`).
# pagers accept output with `#puts`, `#print`, `#write`, and `#<<`. # All pagers accept output with `#puts`, `#print`, `#write`, and `#<<`.
def open def open
pager = best_available pager = best_available
yield pager yield pager
@ -42,12 +44,13 @@ class Pry::Pager
def output; @output; end def output; @output; end
# Return an instance of the "best" available pager class -- `SystemPager` if # Return an instance of the "best" available pager class --
# possible, `SimplePager` if `SystemPager` isn't available, and `NullPager` # `SystemPager` if possible, `SimplePager` if `SystemPager` isn't
# if the user has disabled paging. All pagers accept output with `#puts`, # available, and `NullPager` if the user has disabled paging. All
# `#print`, `#write`, and `#<<`. You must call `#close` when you're done # pagers accept output with `#puts`, `#print`, `#write`, and `#<<`. You
# writing output to a pager, and you must rescue `Pry::Pager::StopPaging`. # must call `#close` when you're done writing output to a pager, and
# These requirements can be avoided by using `.open` instead. # you must rescue `Pry::Pager::StopPaging`. These requirements can be
# avoided by using `.open` instead.
def best_available def best_available
if !_pry_.config.pager if !_pry_.config.pager
NullPager.new(_pry_.output) NullPager.new(_pry_.output)
@ -58,8 +61,8 @@ class Pry::Pager
end end
end end
# `NullPager` is a "pager" that actually just prints all output as it comes # `NullPager` is a "pager" that actually just prints all output as it
# in. Used when `Pry.config.pager` is false. # comes in. Used when `Pry.config.pager` is false.
class NullPager class NullPager
def initialize(out) def initialize(out)
@out = out @out = out
@ -92,8 +95,8 @@ class Pry::Pager
end end
end end
# `SimplePager` is a straightforward pure-Ruby pager. We use it on JRuby and # `SimplePager` is a straightforward pure-Ruby pager. We use it on
# when we can't find a usable external pager. # JRuby and when we can't find a usable external pager.
class SimplePager < NullPager class SimplePager < NullPager
def initialize(*) def initialize(*)
super super
@ -118,14 +121,16 @@ class Pry::Pager
end end
end end
# `SystemPager` buffers output until we're pretty sure it's at least a page # `SystemPager` buffers output until we're pretty sure it's at least a
# long, then invokes an external pager and starts streaming output to it. If # page long, then invokes an external pager and starts streaming output
# `#close` is called before then, it just prints out the buffered content. # to it. If `#close` is called before then, it just prints out the
# buffered content.
class SystemPager < NullPager class SystemPager < NullPager
def self.default_pager def self.default_pager
pager = ENV["PAGER"] || "" pager = ENV["PAGER"] || ""
# Default to less, and make sure less is being passed the correct options # Default to less, and make sure less is being passed the correct
# options
if pager.strip.empty? or pager =~ /^less\b/ if pager.strip.empty? or pager =~ /^less\b/
pager = "less -R -F -X" pager = "less -R -F -X"
end end
@ -198,14 +203,15 @@ class Pry::Pager
end end
end end
# `PageTracker` tracks output to determine whether it's likely to take up a # `PageTracker` tracks output to determine whether it's likely to take
# whole page. This doesn't need to be super precise, but we can use it for # up a whole page. This doesn't need to be super precise, but we can
# `SimplePager` and to avoid invoking the system pager unnecessarily. # use it for `SimplePager` and to avoid invoking the system pager
# unnecessarily.
# #
# One simplifying assumption is that we don't need `#page?` to return `true` # One simplifying assumption is that we don't need `#page?` to return
# on the basis of an incomplete line. Long lines should be counted as # `true` on the basis of an incomplete line. Long lines should be
# multiple lines, but we don't have to transition from `false` to `true` # counted as multiple lines, but we don't have to transition from
# until we see a newline. # `false` to `true` until we see a newline.
class PageTracker class PageTracker
def initialize(rows, cols) def initialize(rows, cols)
@rows, @cols = rows, cols @rows, @cols = rows, cols
@ -240,4 +246,5 @@ class Pry::Pager
line.chomp.gsub(/\e\[[\d;]*m/, '').length line.chomp.gsub(/\e\[[\d;]*m/, '').length
end end
end end
end
end end