mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
ebccd57013
John "banister" Mair describes the following key features of commands as classes: 1. It enables people to extend them by either subclassing or monkeypatching. 2. It enables them to provide their own API, so that for example, the Pry::Command::Edit class could have class methods for people to configure it. Please, note that I didn't touch easter eggs commands. I also prettified some strings (your source code reading experience should vastly improve!). Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
54 lines
1.4 KiB
Ruby
54 lines
1.4 KiB
Ruby
class Pry
|
|
class Command::Ri < Pry::ClassCommand
|
|
match 'ri'
|
|
group 'Introspection'
|
|
description 'View ri documentation. e.g `ri Array#each`'
|
|
|
|
banner <<-BANNER
|
|
Usage: ri [spec]
|
|
e.g. ri Array#each
|
|
|
|
Relies on the rdoc gem being installed. See also: show-doc.
|
|
BANNER
|
|
|
|
def process(spec)
|
|
# 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 page
|
|
paging_text = StringIO.new
|
|
yield paging_text
|
|
Pry::Pager.page(paging_text.string)
|
|
end
|
|
|
|
def formatter(io)
|
|
if @formatter_klass then
|
|
@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 :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
|