1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/commands/ri.rb

65 lines
1.6 KiB
Ruby
Raw Normal View History

class Pry
class Command::Ri < Pry::ClassCommand
match 'ri'
group 'Introspection'
description 'View ri documentation.'
banner <<-'BANNER'
Usage: ri [spec]
View ri documentation. Relies on the "rdoc" gem being installed.
See also "show-doc" command.
ri Array#each
BANNER
def process(spec)
unless spec
return output.puts "Please provide a class, module, or method name (e.g: ri Array#push)"
end
# Lazily load RI
require 'rdoc/ri/driver'
unless defined? RDoc::RI::PryDriver
# Subclass RI so that it formats its output nicely, and uses `lesspipe`.
subclass = Class.new(RDoc::RI::Driver) # the hard way.
subclass.class_eval do
def initialize(pager, opts)
@pager = pager
super opts
end
def page
2012-09-16 17:19:10 -04:00
paging_text = StringIO.new
yield paging_text
@pager.page(paging_text.string)
end
def formatter(io)
2014-02-23 04:37:56 -05:00
if @formatter_klass
@formatter_klass.new
else
RDoc::Markup::ToAnsi.new
end
end
end
RDoc::RI.const_set :PryDriver, subclass # hook it up!
end
# Spin-up an RI insance.
ri = RDoc::RI::PryDriver.new _pry_.pager, :use_stdout => true, :interactive => false
begin
ri.display_names [spec] # Get the documentation (finally!)
rescue RDoc::RI::Driver::NotFoundError => e
output.puts "error: '#{e.name}' not found"
end
end
end
Pry::Commands.add_command(Pry::Command::Ri)
end