Class: IRBShell

Inherits:
Object show all
Defined in:
lib/redshift/util/irb-shell.rb

Overview

A shell that runs when the user interrupts the main process.

Constant Summary collapse

@@irb_setup_done =
false

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ IRBShell

args are binding, self (both optional)



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
# File 'lib/redshift/util/irb-shell.rb', line 9

def initialize(*args)
  ## maybe set some opts here, as in parse_opts in irb/init.rb?

  unless @@irb_setup_done
    @@irb_setup_done = true

    conf = IRB.conf
    
    if File.directory?("tmp")
      conf[:HISTORY_FILE] = "tmp/.irb_shell_history"
    else
      conf[:HISTORY_FILE] = ".irb_shell_history"
    end

    IRB.setup nil
    
    at_exit do
      IRB.irb_at_exit
    end
  end

  workspace = IRB::WorkSpace.new(*args)

  if conf[:SCRIPT] ## normally, set by parse_opts
    @irb = IRB::Irb.new(workspace, conf[:SCRIPT])
  else
    @irb = IRB::Irb.new(workspace)
  end

  conf[:IRB_RC].call(@irb.context) if conf[:IRB_RC]
  conf[:MAIN_CONTEXT] = @irb.context
end

Instance Method Details

#handle_interrupt(after = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/redshift/util/irb-shell.rb', line 72

def handle_interrupt after = nil
  if @interrupt_requests && @interrupt_requests > 0
    yield if block_given?
    run
    true
  else
    false
  end
end

#install_interrupt_handlerObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/redshift/util/irb-shell.rb', line 58

def install_interrupt_handler
  unless @interrupt_requests
    @interrupt_requests = 0
    trap("INT") do
      @interrupt_requests += 1
      if @interrupt_requests == 2
        puts "\nType one more ^C to abort, or wait for shell."
      elsif @interrupt_requests >= 3
        exit!
      end
    end
  end
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/redshift/util/irb-shell.rb', line 42

def run
  @interrupt_requests = nil

  trap("INT") do
    @irb.signal_handle
  end

  begin
    catch(:IRB_EXIT) do
      @irb.eval_input
    end
  ensure
    install_interrupt_handler
  end
end