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
91 92 93 |
# File 'lib/ripl/shell.rb', line 91 def prompt @prompt end |
#result_prompt ⇒ Object
Returns the value of attribute result_prompt.
40 41 42 |
# File 'lib/ripl/shell.rb', line 40 def result_prompt @result_prompt end |
Instance Method Details
#add_commands(obj) ⇒ Object
52 53 54 55 |
# File 'lib/ripl/shell.rb', line 52 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.
128 |
# File 'lib/ripl/shell.rb', line 128 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.
43 44 45 46 |
# File 'lib/ripl/shell.rb', line 43 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
72 73 74 75 76 77 78 79 80 |
# File 'lib/ripl/shell.rb', line 72 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
120 |
# File 'lib/ripl/shell.rb', line 120 def format_error(err) Ripl::Runner.format_error(err) end |
#format_result(result) ⇒ String
Returns Formats result using result_prompt.
123 124 125 |
# File 'lib/ripl/shell.rb', line 123 def format_result(result) result_prompt + result.inspect end |
#get_input ⇒ String?
When extending this method, ensure your plugin disables readline: Readline.config = false.
85 86 87 88 |
# File 'lib/ripl/shell.rb', line 85 def get_input print prompt (input = $stdin.gets) ? input.chomp : input end |
#handle_interrupt ⇒ Object
Handles interrupt (Control-C) by printing a newline
69 |
# File 'lib/ripl/shell.rb', line 69 def handle_interrupt() puts end |
#in_loop ⇒ Object
48 49 50 |
# File 'lib/ripl/shell.rb', line 48 def in_loop catch(:ripl_exit) { loop_once while(true) } end |
#loop_eval(str) ⇒ Object
Evals user input using @binding, @name and @line
99 100 101 |
# File 'lib/ripl/shell.rb', line 99 def loop_eval(str) eval(str, @binding, "(#{@name})", @line) end |
#loop_once ⇒ Object
Runs through one loop iteration: gets input, evals and prints result
58 59 60 61 62 63 64 65 66 |
# File 'lib/ripl/shell.rb', line 58 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.
106 107 108 |
# File 'lib/ripl/shell.rb', line 106 def print_eval_error(err) warn format_error(err) end |
#print_result(result) ⇒ Object
Prints result using #format_result
111 112 113 114 115 |
# File 'lib/ripl/shell.rb', line 111 def print_result(result) puts(format_result(result)) unless @error_raised rescue StandardError, SyntaxError warn "ripl: #{MESSAGES['print_result']}:\n"+ format_error($!) end |