Module: Ripl::Shell::API
- Included in:
- Ripl::Shell
- Defined in:
- lib/ripl/shell.rb
Constant Summary collapse
- MESSAGES =
{ 'prompt' => 'Error while creating prompt', 'print_result' => 'Error while printing result' }
Instance Attribute Summary collapse
- #prompt ⇒ String
-
#result_prompt ⇒ Object
Returns the value of attribute result_prompt.
Instance Method Summary collapse
- #add_commands(obj) ⇒ Object
-
#after_loop ⇒ Object
Called after shell finishes looping.
-
#before_loop ⇒ Object
Sets up shell before looping by loading ~/.irbrc.
-
#eval_input(input) ⇒ Object
Sets @result to result of evaling input and print unexpected errors.
-
#format_error(err) ⇒ String
Formats errors raised by eval of user input.
-
#format_result(result) ⇒ String
Formats result using result_prompt.
-
#get_input ⇒ String?
When extending this method, ensure your plugin disables readline: Readline.config = false.
-
#handle_interrupt ⇒ Object
Handles interrupt (Control-C) by printing a newline.
- #in_loop ⇒ Object
-
#loop_eval(str) ⇒ Object
Evals user input using @binding, @name and @line.
-
#loop_once ⇒ Object
Runs through one loop iteration: gets input, evals and prints result.
-
#print_eval_error(err) ⇒ Object
Prints error formatted by #format_error to STDERR.
-
#print_result(result) ⇒ Object
Prints result using #format_result.
Instance Attribute Details
#prompt ⇒ String
98 99 100 |
# File 'lib/ripl/shell.rb', line 98 def prompt @prompt end |
#result_prompt ⇒ Object
Returns the value of attribute result_prompt.
47 48 49 |
# File 'lib/ripl/shell.rb', line 47 def result_prompt @result_prompt end |
Instance Method Details
#add_commands(obj) ⇒ Object
59 60 61 62 |
# File 'lib/ripl/shell.rb', line 59 def add_commands(obj) ![Symbol, Fixnum].include?(obj.class) ? obj.extend(Ripl::Commands) : obj.class.send(:include, Ripl::Commands) end |
#after_loop ⇒ Object
Called after shell finishes looping.
135 |
# File 'lib/ripl/shell.rb', line 135 def after_loop; end |
#before_loop ⇒ Object
Sets up shell before looping by loading ~/.irbrc. Can be extended to initialize plugins and their instance variables.
50 51 52 53 |
# File 'lib/ripl/shell.rb', line 50 def before_loop Ripl::Runner.load_rc(@irbrc) if @irbrc add_commands(eval("self", @binding)) end |
#eval_input(input) ⇒ Object
Sets @result to result of evaling input and print unexpected errors
79 80 81 82 83 84 85 86 87 |
# File 'lib/ripl/shell.rb', line 79 def eval_input(input) @result = loop_eval(input) eval("_ = Ripl.shell.result", @binding) rescue Exception => e @error_raised = true print_eval_error(e) ensure @line += 1 end |
#format_error(err) ⇒ String
Formats errors raised by eval of user input
127 |
# File 'lib/ripl/shell.rb', line 127 def format_error(err) Ripl::Runner.format_error(err) end |
#format_result(result) ⇒ String
Returns Formats result using result_prompt.
130 131 132 |
# File 'lib/ripl/shell.rb', line 130 def format_result(result) result_prompt + result.inspect end |
#get_input ⇒ String?
When extending this method, ensure your plugin disables readline: Readline.config = false.
92 93 94 95 |
# File 'lib/ripl/shell.rb', line 92 def get_input print prompt (input = $stdin.gets) ? input.chomp : input end |
#handle_interrupt ⇒ Object
Handles interrupt (Control-C) by printing a newline
76 |
# File 'lib/ripl/shell.rb', line 76 def handle_interrupt() puts end |
#in_loop ⇒ Object
55 56 57 |
# File 'lib/ripl/shell.rb', line 55 def in_loop catch(:ripl_exit) { loop_once while(true) } end |
#loop_eval(str) ⇒ Object
Evals user input using @binding, @name and @line
106 107 108 |
# File 'lib/ripl/shell.rb', line 106 def loop_eval(str) eval(str, @binding, "(#{@name})", @line) end |
#loop_once ⇒ Object
Runs through one loop iteration: gets input, evals and prints result
65 66 67 68 69 70 71 72 73 |
# File 'lib/ripl/shell.rb', line 65 def loop_once @error_raised = nil @input = get_input throw(:ripl_exit) if EXIT_WORDS.include?(@input) eval_input(@input) print_result(@result) rescue Interrupt handle_interrupt end |
#print_eval_error(err) ⇒ Object
Prints error formatted by #format_error to STDERR. Could be extended to handle certain exceptions.
113 114 115 |
# File 'lib/ripl/shell.rb', line 113 def print_eval_error(err) $stderr.puts format_error(err) end |
#print_result(result) ⇒ Object
Prints result using #format_result
118 119 120 121 122 |
# File 'lib/ripl/shell.rb', line 118 def print_result(result) puts(format_result(result)) unless @error_raised rescue StandardError, SyntaxError $stderr.puts "ripl: #{MESSAGES['print_result']}:", format_error($!) end |