2012-08-11 20:22:29 -04:00
|
|
|
class Pry
|
2012-12-25 16:35:17 -05:00
|
|
|
class Command::Ri < Pry::ClassCommand
|
|
|
|
match 'ri'
|
2012-08-11 21:26:59 -04:00
|
|
|
group 'Introspection'
|
2013-01-09 15:23:19 -05:00
|
|
|
description 'View ri documentation.'
|
2012-08-11 21:26:59 -04:00
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
banner <<-'BANNER'
|
2012-08-11 20:22:29 -04:00
|
|
|
Usage: ri [spec]
|
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
View ri documentation. Relies on the "rdoc" gem being installed.
|
|
|
|
See also "show-doc" command.
|
|
|
|
|
|
|
|
ri Array#each
|
2012-08-11 20:22:29 -04:00
|
|
|
BANNER
|
|
|
|
|
|
|
|
def process(spec)
|
2015-08-18 15:27:28 -04:00
|
|
|
unless spec
|
2015-08-19 05:35:54 -04:00
|
|
|
return output.puts "Please provide a class, module, or method name (e.g: ri Array#push)"
|
2015-08-18 15:27:28 -04:00
|
|
|
end
|
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
# 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
|
2014-04-30 05:08:29 -04:00
|
|
|
def initialize(pager, opts)
|
|
|
|
@pager = pager
|
|
|
|
super opts
|
|
|
|
end
|
2018-10-14 02:23:34 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def page
|
2012-09-16 17:19:10 -04:00
|
|
|
paging_text = StringIO.new
|
|
|
|
yield paging_text
|
2014-04-30 05:08:29 -04:00
|
|
|
@pager.page(paging_text.string)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
2018-10-14 02:56:53 -04:00
|
|
|
def formatter(_io)
|
2014-02-23 04:37:56 -05:00
|
|
|
if @formatter_klass
|
2012-08-11 20:22:29 -04:00
|
|
|
@formatter_klass.new
|
|
|
|
else
|
|
|
|
RDoc::Markup::ToAnsi.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 12:54:00 -04:00
|
|
|
RDoc::RI.const_set :PryDriver, subclass # hook it up!
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Spin-up an RI insance.
|
2018-10-12 15:09:29 -04:00
|
|
|
ri = RDoc::RI::PryDriver.new _pry_.pager, use_stdout: true, interactive: false
|
2012-08-11 20:22:29 -04:00
|
|
|
|
|
|
|
begin
|
2018-10-12 12:54:00 -04:00
|
|
|
ri.display_names [spec] # Get the documentation (finally!)
|
2012-08-11 20:22:29 -04:00
|
|
|
rescue RDoc::RI::Driver::NotFoundError => e
|
|
|
|
output.puts "error: '#{e.name}' not found"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-25 16:35:17 -05:00
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::Ri)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|