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.
|
|
|
|
#
|
|
|
|
# @param [:simple] pager
|
|
|
|
# Use the pure ruby pager.
|
|
|
|
#
|
|
|
|
# @param [:system] pager
|
|
|
|
# Use the system pager (less)
|
|
|
|
#
|
|
|
|
# @param [nil] pager
|
|
|
|
# Infer what pager to use from the environment.
|
|
|
|
# What this really means is that JRuby uses the pure-ruby pager, and other
|
|
|
|
# platforms will use the system pager.
|
|
|
|
#
|
|
|
|
# @return [void]
|
|
|
|
#
|
2012-08-09 11:43:46 -04:00
|
|
|
def self.page(text, pager = nil)
|
|
|
|
case pager
|
|
|
|
when nil
|
|
|
|
is_jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
|
|
|
|
is_jruby ? SimplePager.new(text).page : SystemPager.new(text).page
|
|
|
|
when :simple
|
|
|
|
SimplePager.new(text).page
|
|
|
|
when :system
|
|
|
|
SystemPager.new(text).page
|
|
|
|
else
|
|
|
|
raise "'#{pager}' is not a recongized pager."
|
|
|
|
end
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
|
2012-08-09 17:51:50 -04:00
|
|
|
def self.page_size
|
|
|
|
27
|
|
|
|
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
|
|
|
|
text_array = @text.lines.to_a
|
|
|
|
text_array.each_slice(Pry::Pager.page_size) do |chunk|
|
2012-08-09 10:58:49 -04:00
|
|
|
puts chunk.join
|
2012-08-08 18:29:51 -04:00
|
|
|
break if chunk.size < Pry::Pager.page_size
|
|
|
|
if text_array.size > Pry::Pager.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
|
|
|
|
def page
|
|
|
|
IO.popen("less -R -S -F -X", "w") do |less|
|
|
|
|
less.puts @text
|
2012-08-08 17:57:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|