pager -> irb-pager. Pager -> IRB::Pager

This commit is contained in:
Denis Knauf 2012-01-04 19:34:42 +01:00
parent ea3173d588
commit 9fa27bd927
4 changed files with 75 additions and 59 deletions

View file

@ -1,10 +1,24 @@
`IRB::Pager` vs. `Pager`
====================
Because naming conflicts, i renamed Pager to `IRB::Pager`.
There is no need for `IRB`; of course you can use `IRB::Pager` without `irb`.
I renamed these things:
* gem: `gem install pager` #=> `gem install irb-pager`
* lib: `require 'pager'` #=> `require 'irb-pager'`
* module: `Pager` #=> `IRB::Pager`
`Pager.pager` will be `IRB::Pager.pager` now.
Usage Usage
===== =====
Very simple: Very simple:
require 'pager' require 'irb-pager'
include Pager include IRB::Pager
pager { puts "Hello World!" } pager { puts "Hello World!" }
pager { 1.upto(200) {|i|sleep 0.2; puts i} } pager { 1.upto(200) {|i|sleep 0.2; puts i} }

View file

@ -4,9 +4,9 @@ require 'rake'
begin begin
require 'jeweler' require 'jeweler'
Jeweler::Tasks.new do |gem| Jeweler::Tasks.new do |gem|
gem.name = "pager" gem.name = "irb-pager"
gem.summary = %Q{Redirect output (and errors) to less} gem.summary = %Q{Redirect output (and errors) to less}
gem.description = %Q{You love something like "domany | less"? In irb you miss it? pager helps to redirect anything to less.} gem.description = %Q{You love something like "domany | less"? In irb you miss it? irb-pager helps to redirect anything to less.}
gem.email = %w[Denis.Knauf@gmail.com] gem.email = %w[Denis.Knauf@gmail.com]
gem.homepage = "http://github.com/DenisKnauf/irb-pager" gem.homepage = "http://github.com/DenisKnauf/irb-pager"
gem.authors = ["Denis Knauf"] gem.authors = ["Denis Knauf"]
@ -51,7 +51,7 @@ Rake::RDocTask.new do |rdoc|
end end
rdoc.rdoc_dir = 'rdoc' rdoc.rdoc_dir = 'rdoc'
rdoc.title = "pager #{version}" rdoc.title = "irb-pager #{version}"
rdoc.rdoc_files.include 'README.md' rdoc.rdoc_files.include 'README.md'
rdoc.rdoc_files.include 'AUTHORS' rdoc.rdoc_files.include 'AUTHORS'
rdoc.rdoc_files.include 'LICENSE' rdoc.rdoc_files.include 'LICENSE'

56
lib/irb-pager.rb Normal file
View file

@ -0,0 +1,56 @@
module IRB
module Pager
module PagerHelper
def self.options opts = nil
opts = opts.kind_of?( Hash) ? opts.dup : {}
stdout = opts[:stdout] || opts[:out] || $stdout
stderr = opts[:stderr] || opts[:err] || $stderr
stdin = opts[:stdin] || opts[:in] || $stdin
pager = opts[:pager] || opts[:less] || $PAGER || ENV['PAGER'] || 'less'
rescuing = opts[:rescuing].nil? ? opts[:exceptions].nil? ? $PAGER_RESCUE : opts[:exceptions] : opts[:rescuing]
[stdout, stderr, stdin, pager, rescuing]
end
def self.exception_formatter exception
["#{exception.class}: #{exception.message}", exception.backtrace.collect {|c| "\tfrom #{c}" }].join "\n"
end
end
def self.pager obj = nil, opts = nil, &exe
if block_given?
stdout, stderr, stdin, pager, rescuing = PagerHelper.options( opts || obj)
pid, dupout, duperr, dupin = nil, stdout.dup, stderr.dup, stdin.dup
IO.pipe do |inrd, inwr|
begin
IO.pipe do |rd, wr|
pid = Process.fork do
stdin.reopen rd
wr.close
exec *pager
end
stdout.reopen wr
stderr.reopen wr
stdin.reopen inrd
if rescuing
begin
yield
rescue Object
stdout.puts PagerHelper.exception_formatter
end
else
yield
end
end
ensure
stdout.reopen dupout
stderr.reopen duperr
Process.wait pid
stdin.reopen dupin
end
end
else
pager( opts) { Kernel.p obj }
end
end
end
end

View file

@ -1,54 +0,0 @@
module Pager
module PagerHelper
def self.options opts = nil
opts = opts.kind_of?( Hash) ? opts.dup : {}
stdout = opts[:stdout] || opts[:out] || $stdout
stderr = opts[:stderr] || opts[:err] || $stderr
stdin = opts[:stdin] || opts[:in] || $stdin
pager = opts[:pager] || opts[:less] || $PAGER || ENV['PAGER'] || 'less'
rescuing = opts[:rescuing] || opts[:exceptions] || $PAGER_RESCUE
[stdout, stderr, stdin, pager, rescuing]
end
def self.exception_formatter exception
["#{exception.class}: #{exception.message}", exception.backtrace.collect {|c| "\tfrom #{c}" }].join "\n"
end
end
def self.pager obj = nil, opts = nil, &exe
if block_given?
stdout, stderr, stdin, pager, rescuing = PagerHelper.options( opts || obj)
pid, dupout, duperr, dupin = nil, stdout.dup, stderr.dup, stdin.dup
IO.pipe do |inrd, inwr|
begin
IO.pipe do |rd, wr|
pid = Process.fork do
stdin.reopen rd
wr.close
exec *pager
end
stdout.reopen wr
stderr.reopen wr
stdin.reopen inrd
if rescuing
begin
yield
rescue Object
stdout.puts PagerHelper.exception_formatter
end
else
yield
end
end
ensure
stdout.reopen dupout
stderr.reopen duperr
Process.wait pid
stdin.reopen dupin
end
end
else
pager( opts) { Kernel.p obj }
end
end
end