Move 'lesspipe' to Pry::Pager::SystemPager.

This commit is contained in:
Robert Gleeson 2012-08-08 23:29:51 +01:00
parent 557a917772
commit f65c915a76
2 changed files with 30 additions and 71 deletions

View File

@ -136,74 +136,14 @@ class Pry
if text.lines.count < Pry::Pager.page_size || !Pry.pager
out.puts text
return
end
# FIXME! Another JRuby hack
if jruby?
Pry::Pager.new(text, out).page
else
lesspipe { |less| less.puts text }
Pry::Pager.page(text, out)
end
rescue Errno::ENOENT
simple_pager(text, out)
rescue Errno::EPIPE
end
#
# Create scrollable output via less!
#
# This command runs `less` in a subprocess, and gives you the IO to its STDIN pipe
# so that you can communicate with it.
#
# Example:
#
# lesspipe do |less|
# 50.times { less.puts "Hi mom!" }
# end
#
# The default less parameters are:
# * Allow colour
# * Don't wrap lines longer than the screen
# * Quit immediately (without paging) if there's less than one screen of text.
#
# You can change these options by passing a hash to `lesspipe`, like so:
#
# lesspipe(:wrap=>false) { |less| less.puts essay.to_s }
#
# It accepts the following boolean options:
# :color => Allow ANSI colour codes?
# :wrap => Wrap long lines?
# :always => Always page, even if there's less than one page of text?
#
def lesspipe(*args)
if args.any? and args.last.is_a?(Hash)
options = args.pop
else
options = {}
end
output = args.first if args.any?
params = []
params << "-R" unless options[:color] == false
params << "-S" unless options[:wrap] == true
params << "-F" unless options[:always] == true
if options[:tail] == true
params << "+\\>"
$stderr.puts "Seeking to end of stream..."
end
params << "-X"
IO.popen("less #{params * ' '}", "w") do |less|
if output
less.puts output
else
yield less
end
end
end
# @param [String] arg_string The object path expressed as a string.
# @param [Pry] _pry_ The relevant Pry instance.
# @param [Array<Binding>] old_stack The state of the old binding stack

View File

@ -3,19 +3,38 @@ class Pry::Pager
27
end
def initialize(text, io)
@text = text
@out = io
def self.page(text, out = $stdout)
is_jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
is_jruby ? SimplePager.new(text, out).page : SystemPager.new(text, out).page
end
def page
text_array = @text.lines.to_a
text_array.each_slice(Pry::Pager.page_size) do |chunk|
@out.puts chunk.join
break if chunk.size < Pry::Pager.page_size
if text_array.size > Pry::Pager.page_size
@out.puts "\n<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>"
break if $stdin.gets.chomp == "q"
raise NotImplementedError, "#{self.class} does not implement #page."
end
def initialize(text, out = $stdout)
@text = text
@out = out
end
class SimplePager < Pry::Pager
def page
text_array = @text.lines.to_a
text_array.each_slice(Pry::Pager.page_size) do |chunk|
@out.puts chunk.join
break if chunk.size < Pry::Pager.page_size
if text_array.size > Pry::Pager.page_size
@out.puts "\n<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>"
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
end
end
end