summaryrefslogtreecommitdiff
path: root/lib/pry/commands/ri.rb
blob: b9cb59075ad812fbb878108f82474a23241e0ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

require 'stringio'

class Pry
  class Command
    class 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
              paging_text = StringIO.new
              yield paging_text
              @pager.page(paging_text.string)
            end

            def formatter(_io)
              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_instance.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
end