Class: IRB::Driver::TTY

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/driver/tty.rb

Direct Known Subclasses

Readline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, output = $stdout) ⇒ TTY

Returns a new instance of TTY.



8
9
10
11
12
# File 'lib/irb/driver/tty.rb', line 8

def initialize(input = $stdin, output = $stdout)
  @input  = input
  @output = output
  @context_stack = []
end

Instance Attribute Details

#context_stackObject (readonly)

Returns the value of attribute context_stack.



6
7
8
# File 'lib/irb/driver/tty.rb', line 6

def context_stack
  @context_stack
end

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/irb/driver/tty.rb', line 6

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/irb/driver/tty.rb', line 6

def output
  @output
end

Instance Method Details

#consumeObject

TODO make it take the current context instead of storing it



24
25
26
27
28
29
# File 'lib/irb/driver/tty.rb', line 24

def consume
  readline
rescue Interrupt
  context.clear_buffer
  ""
end

#contextObject



14
15
16
# File 'lib/irb/driver/tty.rb', line 14

def context
  @context_stack.last
end

#readlineObject



18
19
20
21
# File 'lib/irb/driver/tty.rb', line 18

def readline
  @output.print(context.prompt)
  @input.gets
end

#run(context) ⇒ Object

Feeds input into a given context.

Ensures that the standard output object is a OutputRedirector, or a subclass thereof.



35
36
37
38
39
40
41
42
43
44
# File 'lib/irb/driver/tty.rb', line 35

def run(context)
  @context_stack << context
  before, $stdout = $stdout, OutputRedirector.new unless $stdout.is_a?(OutputRedirector)
  while line = consume
    break unless context.process_line(line)
  end
ensure
  @context_stack.pop
  $stdout = before if before
end