Move the pager to Pry::Pager.

This commit is contained in:
Robert Gleeson 2012-08-08 22:57:19 +01:00
parent 6386706645
commit f84e65bf04
3 changed files with 32 additions and 27 deletions

View File

@ -216,3 +216,4 @@ require "pry/core_extensions"
require "pry/pry_class" require "pry/pry_class"
require "pry/pry_instance" require "pry/pry_instance"
require "pry/cli" require "pry/cli"
require "pry/pager"

View File

@ -91,10 +91,6 @@ class Pry
Pry.color ? "\e[1m#{text}\e[0m": text Pry.color ? "\e[1m#{text}\e[0m": text
end end
def page_size
27
end
# have fun on the Windows platform. # have fun on the Windows platform.
def windows? def windows?
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
@ -123,22 +119,8 @@ class Pry
RUBY_VERSION =~ /1.9/ && RbConfig::CONFIG['ruby_install_name'] == 'ruby' RUBY_VERSION =~ /1.9/ && RbConfig::CONFIG['ruby_install_name'] == 'ruby'
end end
# a simple pager for systems without `less`. A la windows.
def simple_pager(text, output=output())
text_array = text.lines.to_a
text_array.each_slice(page_size) do |chunk|
output.puts chunk.join
break if chunk.size < page_size
if text_array.size > page_size
output.puts "\n<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>"
break if $stdin.gets.chomp == "q"
end
end
end
# Try to use `less` for paging, if it fails then use # Try to use `less` for paging, if it fails then use
# simple_pager. Also do not page if Pry.pager is falsey # simple_pager. Also do not page if Pry.pager is falsey
# FIXME! Another JRuby hack
def stagger_output(text, out = nil) def stagger_output(text, out = nil)
out ||= case out ||= case
when respond_to?(:output) when respond_to?(:output)
@ -152,17 +134,17 @@ class Pry
$stdout $stdout
end end
if text.lines.count < page_size || !Pry.pager #if text.lines.count < Pry::Pager.page_size || !Pry.pager
out.puts text # out.puts text
return # return
end #end
# FIXME! Another JRuby hack # FIXME! Another JRuby hack
if jruby?
simple_pager(text, out) Pry::Pager.new(text, out).page
else #else
lesspipe { |less| less.puts text } # lesspipe { |less| less.puts text }
end #end
rescue Errno::ENOENT rescue Errno::ENOENT
simple_pager(text, out) simple_pager(text, out)
rescue Errno::EPIPE rescue Errno::EPIPE

22
lib/pry/pager.rb Normal file
View File

@ -0,0 +1,22 @@
class Pry::Pager
def self.page_size
27
end
def initialize(text, io)
@text = text
@out = io
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"
end
end
end
end