Permalink
Browse files

Continue OOification of the prompt system. Still a lot more to do

  • Loading branch information...
1 parent 6bd06b1 commit c8596358b048f097f9e9948445e883840ed2e70a Yehuda Katz committed Dec 5, 2009
Showing with 29 additions and 85 deletions.
  1. +17 −9 irb.rb
  2. +1 −24 irb/context.rb
  3. +2 −47 irb/init.rb
  4. +9 −5 irb/input-method.rb
View
26 irb.rb
@@ -110,6 +110,8 @@ def initialize(workspace = nil, input_method = nil, output_method = nil)
def handle_exception(exc)
return unless exc
+ reset_state
+
print exc.class, ": ", exc, "\n"
irb_bug = exc.backtrace[0] =~ /irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/
@@ -129,35 +131,41 @@ def each_top_level_statement(&block)
each_top_level_statement(&block)
end
- def eval_input
+ def reset_state
@lines = ""
- @line_number = 0
+ @relative_line = 0
+ @state = :finished
+ end
+
+ def eval_input
+ @line_number = 1
+ reset_state
# TODO: Use the new prompt object
- context.io.prompt = ">> "
+ context.io.prompt = IRB::DefaultPrompt.new
+ context.io.prompt_state = [@state, @line_number, 0]
each_top_level_statement do |line, line_no|
- context.io.prompt = ">> "
-
# TODO: Lex to see if this is *ever* valid
with_signal_status(:IN_EVAL) do
begin
@context.evaluate(line, line_no)
rescue SyntaxError
+ @state = :incomplete
+ @relative_line += 1
rescue Interrupt => e
- @lines = ""
handle_exception(e)
rescue SystemExit, SignalException
- @lines = ""
raise
rescue Exception => e
- @lines = ""
handle_exception(e)
else
- @lines = ""
+ reset_state
end
end
+
+ context.io.prompt_state = [@state, @line_number, @relative_line]
end
end
View
@@ -35,7 +35,7 @@ def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]
- self.prompt_mode = IRB.conf[:PROMPT_MODE]
+ self.display = ArrowDisplay.new
case input_method
when nil
@@ -95,12 +95,6 @@ def main
attr_accessor :irb_path
attr_accessor :use_readline
- attr_reader :prompt_mode
- attr_accessor :prompt_i
- attr_accessor :prompt_s
- attr_accessor :prompt_c
- attr_accessor :prompt_n
- attr_accessor :auto_indent_mode
attr_accessor :display
attr_accessor :echo
@@ -146,21 +140,6 @@ def irb_path
@irb_path ||= "(" + irb_name + ")"
end
- def prompt_mode=(mode)
- @prompt_mode = mode
- pconf = IRB.conf[:PROMPT][mode]
- @prompt_i = pconf[:PROMPT_I]
- @prompt_s = pconf[:PROMPT_S]
- @prompt_c = pconf[:PROMPT_C]
- @prompt_n = pconf[:PROMPT_N]
- @display = pconf[:RETURN]
- if ai = pconf.include?(:AUTO_INDENT)
- @auto_indent_mode = ai
- else
- @auto_indent_mode = IRB.conf[:AUTO_INDENT]
- end
- end
-
def file_input?
@io.class == FileInputMethod
end
@@ -188,8 +167,6 @@ def evaluate(line, line_no)
display.show(last_value)
end
- PROMPTS = {:ltype => :prompt_s, :continue => :prompt_c, :indent => :prompt_n, :normal => :prompt_i}
-
def print_verbose(str)
IRB.puts str if verbose?
end
View
@@ -20,10 +20,6 @@ def IRB.setup(ap_path)
IRB.init_error
IRB.parse_opts
IRB.run_config
-
- unless conf[:PROMPT][conf[:PROMPT_MODE]]
- IRB.fail(UndefinedPromptMode, conf[:PROMPT_MODE])
- end
end
class AbstractDisplay
@@ -85,8 +81,8 @@ def prompt(state, line, relative_line)
class DefaultPrompt
def prompt(state, line, relative_line)
- prompt = "#{irb_name}(#{main}):#{line_number.rjust(3, "0")}:#{relative_line}"
- prompt << state == :finished ? "> " : "* "
+ prompt = "#{IRB.main_context.irb_name}(#{IRB.main_context.main}):#{line.to_s.rjust(3, "0")}:#{relative_line}"
+ prompt << (state == :finished ? "> " : "* ")
prompt
end
end
@@ -125,47 +121,6 @@ def IRB.init_config(ap_path)
conf[:EVAL_HISTORY] = nil
conf[:SAVE_HISTORY] = nil
-
- conf[:PROMPT] = {
- :NULL => {
- :PROMPT_I => nil,
- :PROMPT_N => nil,
- :PROMPT_S => nil,
- :PROMPT_C => nil,
- :RETURN => SimpleDisplay.new
- },
- :DEFAULT => {
- :PROMPT_I => "%N(%m):%03n:%i> ",
- :PROMPT_N => "%N(%m):%03n:%i> ",
- :PROMPT_S => "%N(%m):%03n:%i%l ",
- :PROMPT_C => "%N(%m):%03n:%i* ",
- :RETURN => ArrowDisplay.new
- },
- :CLASSIC => {
- :PROMPT_I => "%N(%m):%03n:%i> ",
- :PROMPT_N => "%N(%m):%03n:%i> ",
- :PROMPT_S => "%N(%m):%03n:%i%l ",
- :PROMPT_C => "%N(%m):%03n:%i* ",
- :RETURN => SimpleDisplay.new
- },
- :SIMPLE => {
- :PROMPT_I => ">> ",
- :PROMPT_N => ">> ",
- :PROMPT_S => nil,
- :PROMPT_C => "?> ",
- :RETURN => ArrowDisplay.new
- },
- :INF_RUBY => {
- :PROMPT_I => "%N(%m):%03n:%i> ",
- :PROMPT_N => nil,
- :PROMPT_S => nil,
- :PROMPT_C => nil,
- :RETURN => SimpleDisplay.new,
- :AUTO_INDENT => true
- }
- }
-
- conf[:PROMPT_MODE] = (STDIN.tty? ? :DEFAULT : :NULL)
conf[:AUTO_INDENT] = false
conf[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING
View
@@ -25,8 +25,12 @@ def initialize(file = STDIN_FILE_NAME)
end
attr_reader :file_name
- attr_accessor :prompt
-
+ attr_accessor :prompt, :prompt_state
+
+ def prompt_for_input
+ print prompt.prompt(*prompt_state)
+ end
+
def gets
IRB.fail NotImplementedError, "gets"
end
@@ -45,7 +49,7 @@ def initialize
end
def gets
- print @prompt
+ prompt_for_input
@line[@line_no += 1] = $stdin.gets
end
@@ -74,7 +78,7 @@ def eof?
end
def gets
- print @prompt
+ prompt_for_input
@io.gets
end
end
@@ -92,7 +96,7 @@ def initialize
end
def gets
- if l = readline(@prompt, false)
+ if l = readline(prompt.prompt(*prompt_state), false)
HISTORY.push(l) unless l.empty?
@line[@line_no += 1] = l + "\n"
else

0 comments on commit c859635

Please sign in to comment.