2012-08-08 17:57:19 -04:00
|
|
|
class Pry::Pager
|
2012-08-09 17:51:50 -04:00
|
|
|
# @param [String] text
|
|
|
|
# A piece of text to run through a pager.
|
2012-11-03 23:59:21 -04:00
|
|
|
# @param [Symbol?] pager
|
|
|
|
# `:simple` -- Use the pure ruby pager.
|
|
|
|
# `:system` -- Use the system pager (less) or the environment variable
|
|
|
|
# $PAGER if set.
|
|
|
|
# `nil` -- Infer what pager to use from the environment. What this
|
|
|
|
# really means is that JRuby and systems that do not have
|
|
|
|
# access to 'less' will run through the pure ruby pager.
|
2012-08-09 11:43:46 -04:00
|
|
|
def self.page(text, pager = nil)
|
|
|
|
case pager
|
|
|
|
when nil
|
2012-10-26 09:09:07 -04:00
|
|
|
no_pager = !SystemPager.available?
|
2013-01-30 02:46:16 -05:00
|
|
|
if no_pager || Pry::Helpers::BaseHelpers.jruby?
|
|
|
|
SimplePager.new(text).page
|
|
|
|
else
|
|
|
|
SystemPager.new(text).page
|
|
|
|
end
|
2012-08-09 11:43:46 -04:00
|
|
|
when :simple
|
|
|
|
SimplePager.new(text).page
|
|
|
|
when :system
|
|
|
|
SystemPager.new(text).page
|
|
|
|
else
|
2012-12-02 11:45:42 -05:00
|
|
|
raise "'#{pager}' is not a recognized pager."
|
2012-08-09 11:43:46 -04:00
|
|
|
end
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
|
2012-08-09 17:51:50 -04:00
|
|
|
def self.page_size
|
2013-01-30 22:14:06 -05:00
|
|
|
rows = Pry::Terminal.screen_size
|
2013-01-26 18:31:06 -05:00
|
|
|
@page_size ||= (rows && rows.first || 27)
|
2012-08-09 17:51:50 -04:00
|
|
|
end
|
|
|
|
|
2012-08-09 10:58:49 -04:00
|
|
|
def initialize(text)
|
2012-08-08 18:29:51 -04:00
|
|
|
@text = text
|
|
|
|
end
|
|
|
|
|
|
|
|
class SimplePager < Pry::Pager
|
|
|
|
def page
|
2013-01-23 21:27:08 -05:00
|
|
|
# The pager size minus the number of lines used by the simple pager info bar.
|
|
|
|
page_size = Pry::Pager.page_size - 3
|
2012-08-08 18:29:51 -04:00
|
|
|
text_array = @text.lines.to_a
|
2013-01-30 02:46:16 -05:00
|
|
|
|
2013-01-23 19:54:47 -05:00
|
|
|
text_array.each_slice(page_size) do |chunk|
|
2012-08-09 10:58:49 -04:00
|
|
|
puts chunk.join
|
2013-01-23 19:54:47 -05:00
|
|
|
break if chunk.size < page_size
|
|
|
|
if text_array.size > page_size
|
2012-08-09 10:58:49 -04:00
|
|
|
puts "\n<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>"
|
2012-08-08 18:29:51 -04:00
|
|
|
break if $stdin.gets.chomp == "q"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SystemPager < Pry::Pager
|
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
|
|
|
|
if pager.strip.empty? or pager =~ /^less\s*/
|
|
|
|
pager = "less -R -S -F -X"
|
|
|
|
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
|
2012-10-26 09:13:23 -04:00
|
|
|
@pager = SystemPager.default_pager
|
2012-10-26 09:09:07 -04:00
|
|
|
end
|
|
|
|
|
2012-08-08 18:29:51 -04:00
|
|
|
def page
|
2012-10-26 09:09:07 -04:00
|
|
|
IO.popen(@pager, 'w') do |io|
|
|
|
|
io.write @text
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|