From f84e65bf04d6e457f72d17d2ff6d94bf14404b06 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 8 Aug 2012 22:57:19 +0100 Subject: [PATCH] Move the pager to Pry::Pager. --- lib/pry.rb | 1 + lib/pry/helpers/base_helpers.rb | 36 +++++++++------------------------ lib/pry/pager.rb | 22 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 lib/pry/pager.rb diff --git a/lib/pry.rb b/lib/pry.rb index 90522e9c..64df36f4 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -216,3 +216,4 @@ require "pry/core_extensions" require "pry/pry_class" require "pry/pry_instance" require "pry/cli" +require "pry/pager" diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index a91283cf..83c6c776 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -91,10 +91,6 @@ class Pry Pry.color ? "\e[1m#{text}\e[0m": text end - def page_size - 27 - end - # have fun on the Windows platform. def windows? RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ @@ -123,22 +119,8 @@ class Pry RUBY_VERSION =~ /1.9/ && RbConfig::CONFIG['ruby_install_name'] == 'ruby' 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 --- Press enter to continue ( q to break ) --- " - break if $stdin.gets.chomp == "q" - end - end - end - # Try to use `less` for paging, if it fails then use # simple_pager. Also do not page if Pry.pager is falsey - # FIXME! Another JRuby hack def stagger_output(text, out = nil) out ||= case when respond_to?(:output) @@ -152,17 +134,17 @@ class Pry $stdout end - if text.lines.count < page_size || !Pry.pager - out.puts text - return - end + #if text.lines.count < Pry::Pager.page_size || !Pry.pager + # out.puts text + # return + #end # FIXME! Another JRuby hack - if jruby? - simple_pager(text, out) - else - lesspipe { |less| less.puts text } - end + + Pry::Pager.new(text, out).page + #else + # lesspipe { |less| less.puts text } + #end rescue Errno::ENOENT simple_pager(text, out) rescue Errno::EPIPE diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb new file mode 100644 index 00000000..8f412e00 --- /dev/null +++ b/lib/pry/pager.rb @@ -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 --- Press enter to continue ( q to break ) --- " + break if $stdin.gets.chomp == "q" + end + end + end +end