summaryrefslogtreecommitdiff
path: root/lib/pry/control_d_handler.rb
blob: 1021c767deee3f2b55717f108eb30db90ec9516e (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
class Pry
  # @api private
  # @since ?.?.?
  module ControlDHandler
    # 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 ..`.
    def self.default(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).
        cd_state = Pry::CommandState.default.state_for('cd')
        cd_state.old_stack = pry_instance.binding_stack.dup
        pry_instance.binding_stack.pop
      end
    end
  end
end