Navigation Menu

Skip to content

Commit

Permalink
From the TTY and Readline drivers, rewrite the last line if it was ch…
Browse files Browse the repository at this point in the history
…anged.

git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4857 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
alloy committed Oct 31, 2010
1 parent b5bc8e3 commit 67a556b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/irb/driver/tty.rb
Expand Up @@ -3,6 +3,11 @@
module IRB
module Driver
class TTY
move_one_line_up = "\e[1A"
move_to_begin_of_line = "\r"
clear_to_end_of_line = "\e[0K"
CLEAR_LAST_LINE = move_one_line_up + move_to_begin_of_line + clear_to_end_of_line

attr_reader :input, :output, :context_stack

def initialize(input = $stdin, output = $stdout)
Expand All @@ -28,17 +33,13 @@ def consume
""
end

def last_line_decreased_indentation_level(reformatted_line)
move_one_line_up = "\e[1A"
move_to_begin_of_line = "\r"
clear_to_end_of_line = "\e[0K"
clear_last_line = move_one_line_up + move_to_begin_of_line + clear_to_end_of_line
@output.print clear_last_line
@output.puts(context.prompt + reformatted_line)
def update_last_line(reformatted_line)
@output.print CLEAR_LAST_LINE
@output.puts(context.prompt(true) + reformatted_line)
end

def process_input(line)
context.process_line(line) { |new_line| last_line_decreased_indentation_level(new_line) }
context.process_line(line) { |reformatted_line| update_last_line(reformatted_line) }
end

# Feeds input into a given context.
Expand Down
20 changes: 19 additions & 1 deletion spec/driver/tty_spec.rb
@@ -1,10 +1,12 @@
require File.expand_path('../../spec_helper', __FILE__)
require 'irb/driver/tty'

main = self

describe "IRB::Driver::TTY" do
before do
@driver = IRB::Driver::TTY.new(InputStub.new, OutputStub.new)
@context = IRB::Context.new(Object.new)
@context = IRB::Context.new(main)
@driver.context_stack << @context
end

Expand Down Expand Up @@ -38,6 +40,22 @@ def @driver.readline; raise Interrupt; end
@driver.consume.should == ""
@context.source.to_s.should == ""
end

it "feeds the input into the context" do
@driver.process_input("def foo")
@context.source.to_s.should == "def foo"
end

it "updates the previously printed line on the console, if a change to the input occurs (such as re-indenting)" do
@context.formatter.auto_indent = true
@driver.process_input("def foo")
@driver.process_input("p :ok")
@driver.process_input(" end")
@driver.output.printed.strip.should == [
IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):002:1> p :ok",
IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):003:1> end"
].join("\n")
end
end

describe "IRB::Driver::TTY, when starting the runloop" do
Expand Down

0 comments on commit 67a556b

Please sign in to comment.