Class: IRB::Driver::TTY

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

Direct Known Subclasses

Readline

Constant Summary collapse

CLEAR_LAST_LINE =
move_one_line_up + move_to_begin_of_line + clear_to_end_of_line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TTY.



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

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.



11
12
13
# File 'lib/irb/driver/tty.rb', line 11

def context_stack
  @context_stack
end

#inputObject (readonly)

Returns the value of attribute input.



11
12
13
# File 'lib/irb/driver/tty.rb', line 11

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



11
12
13
# File 'lib/irb/driver/tty.rb', line 11

def output
  @output
end

Instance Method Details

#consumeObject

TODO make it take the current context instead of storing it



29
30
31
32
33
34
# File 'lib/irb/driver/tty.rb', line 29

def consume
  readline
rescue Interrupt
  context.clear_buffer
  ""
end

#contextObject



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

def context
  @context_stack.last
end

#process_input(line) ⇒ Object



41
42
43
44
45
# File 'lib/irb/driver/tty.rb', line 41

def process_input(line)
  context.process_line(line) do |prompt, line|
    update_last_line(prompt, line)
  end
end

#readlineObject



23
24
25
26
# File 'lib/irb/driver/tty.rb', line 23

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.



51
52
53
54
55
56
57
58
59
# File 'lib/irb/driver/tty.rb', line 51

def run(context)
  @context_stack << context
  while line = consume
    continue = process_input(line)
    break unless continue
  end
ensure
  @context_stack.pop
end

#update_last_line(prompt, reformatted_line) ⇒ Object



36
37
38
39
# File 'lib/irb/driver/tty.rb', line 36

def update_last_line(prompt, reformatted_line)
  @output.print CLEAR_LAST_LINE
  @output.puts("#{prompt}#{reformatted_line}")
end