2012-08-08 21:57:19 +00:00
|
|
|
class Pry::Pager
|
2012-08-09 21:51:50 +00:00
|
|
|
# @param [String] text
|
|
|
|
# A piece of text to run through a pager.
|
2012-11-04 03:59:21 +00: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 15:43:46 +00:00
|
|
|
def self.page(text, pager = nil)
|
|
|
|
case pager
|
|
|
|
when nil
|
2012-10-26 13:09:07 +00:00
|
|
|
no_pager = !SystemPager.available?
|
2012-08-09 15:43:46 +00:00
|
|
|
is_jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
|
2012-09-15 20:24:12 +00:00
|
|
|
(is_jruby || no_pager) ? SimplePager.new(text).page : SystemPager.new(text).page
|
2012-08-09 15:43:46 +00:00
|
|
|
when :simple
|
|
|
|
SimplePager.new(text).page
|
|
|
|
when :system
|
|
|
|
SystemPager.new(text).page
|
|
|
|
else
|
|
|
|
raise "'#{pager}' is not a recongized pager."
|
|
|
|
end
|
2012-08-08 21:57:19 +00:00
|
|
|
end
|
|
|
|
|
2012-08-09 21:51:50 +00:00
|
|
|
def self.page_size
|
|
|
|
27
|
|
|
|
end
|
|
|
|
|
2012-08-09 14:58:49 +00:00
|
|
|
def initialize(text)
|
2012-08-08 22:29:51 +00:00
|
|
|
@text = text
|
|
|
|
end
|
|
|
|
|
|
|
|
class SimplePager < Pry::Pager
|
|
|
|
def page
|
|
|
|
text_array = @text.lines.to_a
|
|
|
|
text_array.each_slice(Pry::Pager.page_size) do |chunk|
|
2012-08-09 14:58:49 +00:00
|
|
|
puts chunk.join
|
2012-08-08 22:29:51 +00:00
|
|
|
break if chunk.size < Pry::Pager.page_size
|
|
|
|
if text_array.size > Pry::Pager.page_size
|
2012-08-09 14:58:49 +00:00
|
|
|
puts "\n<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>"
|
2012-08-08 22:29:51 +00:00
|
|
|
break if $stdin.gets.chomp == "q"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SystemPager < Pry::Pager
|
2012-10-26 13:09:07 +00:00
|
|
|
def self.default_pager
|
|
|
|
ENV["PAGER"] || "less -R -S -F -X"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.available?
|
2012-11-19 21:37:03 +00:00
|
|
|
pager_executable = default_pager.split(' ').first
|
|
|
|
`which #{ pager_executable }`
|
|
|
|
rescue
|
2012-10-26 13:09:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
2012-10-26 13:13:23 +00:00
|
|
|
@pager = SystemPager.default_pager
|
2012-10-26 13:09:07 +00:00
|
|
|
end
|
|
|
|
|
2012-08-08 22:29:51 +00:00
|
|
|
def page
|
2012-10-26 13:09:07 +00:00
|
|
|
IO.popen(@pager, 'w') do |io|
|
|
|
|
io.write @text
|
2012-08-08 21:57:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|