2013-02-12 07:42:29 -05:00
|
|
|
require 'pry/terminal'
|
2013-10-29 00:25:13 -04:00
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# A pager is an `IO`-like object that accepts text and either prints it
|
2013-10-29 00:25:13 -04:00
|
|
|
# immediately, prints it one page at a time, or streams it to an external
|
|
|
|
# program to print one page at a time.
|
2013-11-02 21:46:01 -04:00
|
|
|
module Pry::Pager
|
2013-10-29 00:25:13 -04:00
|
|
|
class StopPaging < StandardError
|
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# Send the given text through the best available pager (if `Pry.pager` is
|
2013-11-02 21:40:56 -04:00
|
|
|
# enabled).
|
|
|
|
# @param [String] text A piece of text to run through a pager.
|
2013-11-03 19:35:33 -05:00
|
|
|
# @param [IO] output (`$stdout`) An object to send output to.
|
|
|
|
def self.page(text, output = $stdout)
|
|
|
|
with_pager(output) do |pager|
|
|
|
|
pager << text
|
2012-08-09 11:43:46 -04:00
|
|
|
end
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# 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)
|
|
|
|
yield pager
|
2013-11-02 21:50:04 -04:00
|
|
|
rescue StopPaging
|
2013-10-29 00:25:13 -04:00
|
|
|
ensure
|
|
|
|
pager.close if pager
|
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# 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.
|
|
|
|
# @param [#<<] output ($stdout) An object to send output to.
|
2013-11-02 21:40:56 -04:00
|
|
|
def self.best_available(output)
|
|
|
|
if !Pry.pager
|
|
|
|
NullPager.new(output)
|
|
|
|
elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby?
|
2013-10-29 00:25:13 -04:00
|
|
|
SimplePager.new(output)
|
2012-08-09 11:43:46 -04:00
|
|
|
else
|
2013-11-02 21:40:56 -04:00
|
|
|
SystemPager.new(output)
|
2012-08-09 11:43:46 -04:00
|
|
|
end
|
2012-08-09 17:51:50 -04:00
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# `NullPager` is a "pager" that actually just prints all output as it comes
|
|
|
|
# in. Used when `Pry.pager` is false.
|
2013-11-02 21:40:56 -04:00
|
|
|
class NullPager
|
|
|
|
def initialize(out)
|
|
|
|
@out = out
|
|
|
|
end
|
2013-10-29 00:25:13 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
def puts(str)
|
|
|
|
print "#{str.chomp}\n"
|
|
|
|
end
|
2013-10-29 00:25:13 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
def print(str)
|
|
|
|
write str
|
|
|
|
end
|
|
|
|
alias << print
|
2013-10-29 00:25:13 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
def write(str)
|
|
|
|
@out.write str
|
|
|
|
end
|
2013-11-02 17:14:51 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
def close
|
|
|
|
end
|
2012-08-08 18:29:51 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
private
|
2013-11-02 17:14:51 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
def height
|
|
|
|
@height ||= Pry::Terminal.height!
|
|
|
|
end
|
2013-11-02 17:14:51 -04:00
|
|
|
|
2013-11-02 21:40:56 -04:00
|
|
|
def width
|
|
|
|
@width ||= Pry::Terminal.width!
|
|
|
|
end
|
2012-08-08 18:29:51 -04:00
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# `SimplePager` is a straightforward pure-Ruby pager. We use it on JRuby and
|
|
|
|
# when we can't find a usable external pager.
|
2013-11-02 21:40:56 -04:00
|
|
|
class SimplePager < NullPager
|
2013-10-29 00:25:13 -04:00
|
|
|
def initialize(*)
|
|
|
|
super
|
2013-11-02 17:14:51 -04:00
|
|
|
@tracker = PageTracker.new(height - 3, width)
|
2013-10-29 00:25:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write(str)
|
|
|
|
str.lines.each do |line|
|
2013-11-03 19:49:07 -05:00
|
|
|
@out.print line
|
2013-11-02 17:14:51 -04:00
|
|
|
@tracker.record line
|
2013-10-29 00:25:13 -04:00
|
|
|
|
2013-11-02 17:14:51 -04:00
|
|
|
if @tracker.page?
|
2013-11-09 19:50:16 -05:00
|
|
|
@out.print "\n"
|
|
|
|
@out.print "\e[0m" if Pry.color
|
|
|
|
@out.print "<page break> --- Press enter to continue " \
|
|
|
|
"( q<enter> to break ) --- <page break>\n"
|
2013-11-09 18:25:19 -05:00
|
|
|
raise StopPaging if Readline.readline.chomp == "q"
|
2013-11-02 17:14:51 -04:00
|
|
|
@tracker.reset
|
2012-08-08 18:29:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# `SystemPager` buffers output until we're pretty sure it's at least a page
|
|
|
|
# long, then invokes an external pager and starts streaming output to it. If
|
|
|
|
# `#close` is called before then, it just prints out the buffered content.
|
2013-11-02 21:40:56 -04:00
|
|
|
class SystemPager < NullPager
|
2012-10-26 09:09:07 -04:00
|
|
|
def self.default_pager
|
2013-01-18 20:58:11 -05:00
|
|
|
pager = ENV["PAGER"] || ""
|
|
|
|
|
|
|
|
# Default to less, and make sure less is being passed the correct options
|
2013-11-02 17:14:51 -04:00
|
|
|
if pager.strip.empty? or pager =~ /^less\b/
|
2013-11-09 15:49:27 -05:00
|
|
|
pager = "less -R -F -X"
|
2013-01-18 20:58:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
pager
|
2012-10-26 09:09:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.available?
|
2013-01-18 20:58:11 -05:00
|
|
|
if @system_pager.nil?
|
|
|
|
@system_pager = begin
|
|
|
|
pager_executable = default_pager.split(' ').first
|
|
|
|
`which #{ pager_executable }`
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@system_pager
|
|
|
|
end
|
2012-10-26 09:09:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
2013-11-02 17:14:51 -04:00
|
|
|
@tracker = PageTracker.new(height, width)
|
|
|
|
@buffer = ""
|
2012-10-26 09:09:07 -04:00
|
|
|
end
|
|
|
|
|
2013-10-29 00:25:13 -04:00
|
|
|
def write(str)
|
2013-11-02 17:14:51 -04:00
|
|
|
if invoked_pager?
|
|
|
|
pager.write str
|
|
|
|
else
|
|
|
|
@tracker.record str
|
|
|
|
@buffer << str
|
|
|
|
|
|
|
|
if @tracker.page?
|
|
|
|
pager.write @buffer
|
|
|
|
end
|
|
|
|
end
|
2013-10-29 00:25:13 -04:00
|
|
|
rescue Errno::EPIPE
|
2013-11-02 18:54:21 -04:00
|
|
|
raise StopPaging
|
2012-10-26 09:09:07 -04:00
|
|
|
end
|
|
|
|
|
2013-10-29 00:25:13 -04:00
|
|
|
def close
|
2013-11-02 17:14:51 -04:00
|
|
|
if invoked_pager?
|
|
|
|
pager.close
|
|
|
|
else
|
|
|
|
@out.puts @buffer
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
end
|
2013-11-02 17:14:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def invoked_pager?
|
|
|
|
@pager
|
|
|
|
end
|
|
|
|
|
|
|
|
def pager
|
|
|
|
@pager ||= IO.popen(self.class.default_pager, 'w')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-03 19:35:33 -05:00
|
|
|
# `PageTracker` tracks output to determine whether it's likely to take up a
|
2013-11-02 17:14:51 -04:00
|
|
|
# whole page. This doesn't need to be super precise, but we can use it for
|
2013-11-03 19:35:33 -05:00
|
|
|
# `SimplePager` and to avoid invoking the system pager unnecessarily.
|
2013-11-02 17:14:51 -04:00
|
|
|
#
|
2013-11-03 19:35:33 -05:00
|
|
|
# One simplifying assumption is that we don't need `#page?` to return `true`
|
|
|
|
# on the basis of an incomplete line. Long lines should be counted as
|
|
|
|
# multiple lines, but we don't have to transition from `false` to `true`
|
|
|
|
# until we see a newline.
|
2013-11-02 17:14:51 -04:00
|
|
|
class PageTracker
|
|
|
|
def initialize(rows, cols)
|
|
|
|
@rows, @cols = rows, cols
|
|
|
|
reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def record(str)
|
|
|
|
str.lines.each do |line|
|
|
|
|
if line.end_with? "\n"
|
|
|
|
@row += ((@col + line_length(line) - 1) / @cols) + 1
|
|
|
|
@col = 0
|
|
|
|
else
|
2013-11-02 18:46:38 -04:00
|
|
|
@col += line_length(line)
|
2013-11-02 17:14:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def page?
|
|
|
|
@row >= @rows
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@row = 0
|
|
|
|
@col = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Approximation of the printable length of a given line, without the
|
|
|
|
# newline and without ANSI color codes.
|
|
|
|
def line_length(line)
|
|
|
|
line.chomp.gsub(/\e\[[\d;]*m/, '').length
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|