summaryrefslogtreecommitdiff
path: root/lib/pry/config.rb
blob: fd6d92b9efb533e8f0be92f612114c2bb5b13a3d (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class Pry
  # The Pry config.
  # @api public
  class Config < Pry::BasicObject
    include Behavior

    # @return [Pry::Config]
    #   An object who implements the default configuration for all
    #   Pry sessions.
    # rubocop:disable Metrics/AbcSize
    def self.defaults
      defaults = from_hash(
        input: Pry.lazy { lazy_readline(defaults) },
        output: $stdout.tap { |out| out.sync = true },
        commands: Pry::Commands,
        prompt_name: Pry::Prompt::DEFAULT_NAME,
        prompt: Pry::Prompt[:default],
        prompt_safe_contexts: Pry::Prompt::SAFE_CONTEXTS,

        print: proc do |_output, value, pry_instance|
          pry_instance.pager.open do |pager|
            pager.print pry_instance.config.output_prefix
            Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1)
          end
        end,

        quiet: false,

        # Will only show the first line of the backtrace
        exception_handler: proc do |output, exception, _|
          if exception.is_a?(UserError) && exception.is_a?(SyntaxError)
            output.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
          else
            output.puts "#{exception.class}: #{exception.message}"
            output.puts "from #{exception.backtrace.first}"

            if exception.respond_to? :cause
              cause = exception.cause
              while cause
                output.puts "Caused by #{cause.class}: #{cause}\n"
                output.puts "from #{cause.backtrace.first}"
                cause = cause.cause
              end
            end
          end
        end,

        unrescued_exceptions: [
          ::SystemExit, ::SignalException, Pry::TooSafeException
        ],

        exception_whitelist: Pry.lazy do
          defaults.output.puts(
            '[warning] Pry.config.exception_whitelist is deprecated, ' \
            'please use Pry.config.unrescued_exceptions instead.'
          )
          [::SystemExit, ::SignalException, Pry::TooSafeException]
        end,

        # The default hooks - display messages when beginning and ending Pry
        # sessions.
        hooks: Pry::Hooks.new.add_hook(
          :before_session, :default
        ) do |_out, _target, pry_instance|
          next if pry_instance.quiet?

          pry_instance.run_command('whereami --quiet')
        end,

        pager: true,

        system: proc do |output, cmd, _|
          next if system(cmd)

          output.puts 'Error: there was a problem executing system ' \
                      "command: #{cmd}"
        end,

        color: Pry::Helpers::BaseHelpers.use_ansi_codes?,
        default_window_size: 5,
        editor: Pry.default_editor_for_platform,
        should_load_rc: true,
        should_load_local_rc: true,
        should_trap_interrupts: Pry::Helpers::Platform.jruby?,
        disable_auto_reload: false,
        command_prefix: "",
        auto_indent: Pry::Helpers::BaseHelpers.use_ansi_codes?,
        correct_indent: true,
        collision_warning: false,
        output_prefix: "=> ",
        requires: [],
        should_load_requires: true,
        should_load_plugins: true,
        windows_console_warning: true,

        # Deal with the ^D key being pressed. Different behaviour in different
        # cases:
        #   1. In an expression behave like `!` command.
        #   2. At top-level session behave like `exit` command.
        #   3. In a nested session behave like `cd ..`.
        control_d_handler: proc do |eval_string, pry_instance|
          if !eval_string.empty?
            eval_string.replace('') # Clear input buffer.
          elsif pry_instance.binding_stack.one?
            pry_instance.binding_stack.clear
            throw(:breakout)
          else
            # Otherwise, saves current binding stack as old stack and pops last
            # binding out of binding stack (the old stack still has that binding).
            pry_instance.command_state["cd"] ||= Pry::Config.from_hash({})
            pry_instance.command_state['cd'].old_stack = pry_instance.binding_stack.dup
            pry_instance.binding_stack.pop
          end
        end,

        memory_size: 100,
        extra_sticky_locals: {},
        command_completions: proc { defaults.commands.keys },
        file_completions: proc { Dir["."] },
        ls: Pry::Config.from_hash(Pry::Command::Ls::DEFAULT_OPTIONS),
        completer: Pry::InputCompleter,
        history: Pry::Config.from_hash(
          should_save: true, should_load: true
        ).tap do |history|
          history_file =
            if File.exist?(File.expand_path('~/.pry_history'))
              '~/.pry_history'
            elsif ENV.key?('XDG_DATA_HOME') && ENV['XDG_DATA_HOME'] != ''
              # See XDG Base Directory Specification at
              # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
              ENV['XDG_DATA_HOME'] + '/pry/pry_history'
            else
              '~/.local/share/pry/pry_history'
            end
          history.file = File.expand_path(history_file)
        end,
        exec_string: ""
      )
    end
    # rubocop:enable Metrics/AbcSize

    def self.shortcuts
      Convenience::SHORTCUTS
    end

    # @api private
    def self.lazy_readline(defaults)
      require 'readline'
      ::Readline
    rescue LoadError
      defaults.output.puts(
        "Sorry, you can't use Pry without Readline or a compatible library. \n" \
        "Possible solutions: \n" \
        " * Rebuild Ruby with Readline support using `--with-readline` \n" \
        " * Use the rb-readline gem, which is a pure-Ruby port of Readline \n" \
        " * Use the pry-coolline gem, a pure-ruby alternative to Readline"
      )
      raise
    end
    private_class_method :lazy_readline
  end
end