Skip to content
Browse files

If process a new input line changes the indentation, update the previ…

…ously printed version and indent the prompt.

git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4854 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent db9a497 commit 7deea955e7ffd152129c1b86d4b4c1223d33ef01 @alloy alloy committed Oct 31, 2010
Showing with 39 additions and 4 deletions.
  1. +1 −1 lib/irb/driver/readline.rb
  2. +12 −2 lib/irb/driver/tty.rb
  3. +16 −0 spec/driver/readline_spec.rb
  4. +10 −1 spec/driver/tty_spec.rb
View
2 lib/irb/driver/readline.rb
@@ -15,7 +15,7 @@ def initialize(input = $stdin, output = $stdout)
end
def readline
- source = ::Readline.readline(context.prompt, true)
+ source = ::Readline.readline(prompt, true)
IRB::History.input(source)
source
end
View
14 lib/irb/driver/tty.rb
@@ -4,6 +4,7 @@ module IRB
module Driver
class TTY
attr_reader :input, :output, :context_stack
+ attr_accessor :auto_indent # TODO: this should probably go to a more global config, which means we shouldn't completely deprecate the CONF
def initialize(input = $stdin, output = $stdout)
@input = input
@@ -14,9 +15,13 @@ def initialize(input = $stdin, output = $stdout)
def context
@context_stack.last
end
+
+ def prompt
+ context.prompt(@auto_indent)
+ end
def readline
- @output.print(context.prompt)
+ @output.print(prompt)
@input.gets
end
@@ -36,6 +41,10 @@ def last_line_decreased_indentation_level(reformatted_line)
@output.print clear_last_line
@output.puts(context.prompt + reformatted_line)
end
+
+ def process_input(line)
+ context.process_line(line) { |new_line| last_line_decreased_indentation_level(new_line) }
+ end
# Feeds input into a given context.
#
@@ -44,7 +53,8 @@ def last_line_decreased_indentation_level(reformatted_line)
def run(context)
@context_stack << context
while line = consume
- break unless context.process_line(line)
+ continue = process_input(line)
+ break unless continue
end
ensure
@context_stack.pop
View
16 spec/driver/readline_spec.rb
@@ -41,6 +41,7 @@ def self.readline(prompt, use_history)
@driver = IRB::Driver::Readline.new(InputStub.new, OutputStub.new)
@context = IRB::Context.new(Object.new)
@driver.context_stack << @context
+ Readline.clear_printed!
end
it "is a subclass of IRB::Driver::TTY" do
@@ -61,6 +62,21 @@ def self.readline(prompt, use_history)
@driver.readline.should == "nom nom nom"
end
+ it "prints a prompt" do
+ @context.source << "def foo"
+ Readline.stub_input "nom nom nom"
+ @driver.readline
+ Readline.printed.should == @context.prompt
+ end
+
+ it "prints a prompt with indentation if it's configured" do
+ @driver.auto_indent = true
+ @context.source << "def foo"
+ Readline.stub_input "nom nom nom"
+ @driver.readline
+ Readline.printed.should == @context.prompt(true)
+ end
+
it "tells the Readline module to use the history" do
Readline.use_history = false
Readline.stub_input "nom nom nom"
View
11 spec/driver/tty_spec.rb
@@ -9,10 +9,19 @@
end
it "prints the prompt and reads a line of input" do
+ @context.source << "def foo"
@driver.input.stub_input "calzone"
@driver.readline.should == "calzone"
@driver.output.printed.should == @context.prompt
end
+
+ it "prints a prompt with indentation if it's configured" do
+ @driver.auto_indent = true
+ @context.source << "def foo"
+ @driver.input.stub_input "calzone"
+ @driver.readline
+ @driver.output.printed.should == @context.prompt(true)
+ end
it "consumes input" do
@driver.input.stub_input "calzone"
@@ -67,4 +76,4 @@ def @driver.readline; raise Interrupt; end
# irb(o)
# IRBRan.should == o
# end
-# end
+# end

0 comments on commit 7deea95

Please sign in to comment.
Something went wrong with that request. Please try again.