Class: IRB::Driver::TTY
- Inherits:
-
Object
- Object
- IRB::Driver::TTY
- Defined in:
- lib/irb/driver/tty.rb
Direct Known Subclasses
Constant Summary collapse
- CLEAR_LAST_LINE =
move_one_line_up + move_to_begin_of_line + clear_to_end_of_line
Instance Attribute Summary collapse
-
#context_stack ⇒ Object
readonly
Returns the value of attribute context_stack.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#consume ⇒ Object
TODO make it take the current context instead of storing it.
- #context ⇒ Object
-
#initialize(input = $stdin, output = $stdout) ⇒ TTY
constructor
A new instance of TTY.
- #process_input(line) ⇒ Object
- #readline ⇒ Object
-
#run(context) ⇒ Object
Feeds input into a given context.
- #update_last_line(prompt, reformatted_line) ⇒ Object
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_stack ⇒ Object (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 |
#input ⇒ Object (readonly)
Returns the value of attribute input.
11 12 13 |
# File 'lib/irb/driver/tty.rb', line 11 def input @input end |
#output ⇒ Object (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
#consume ⇒ Object
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 |
#context ⇒ Object
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 |
#readline ⇒ Object
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 |