Class: ConsoleAgent::Executor
- Inherits:
-
Object
- Object
- ConsoleAgent::Executor
- Defined in:
- lib/console_agent/executor.rb
Constant Summary collapse
- CODE_REGEX =
/```ruby\s*\n(.*?)```/m
Instance Attribute Summary collapse
-
#binding_context ⇒ Object
readonly
Returns the value of attribute binding_context.
-
#on_prompt ⇒ Object
Returns the value of attribute on_prompt.
Instance Method Summary collapse
- #confirm_and_execute(code) ⇒ Object
- #display_response(response) ⇒ Object
- #execute(code) ⇒ Object
- #extract_code(response) ⇒ Object
-
#initialize(binding_context) ⇒ Executor
constructor
A new instance of Executor.
- #last_answer ⇒ Object
- #last_cancelled? ⇒ Boolean
- #last_output ⇒ Object
Constructor Details
#initialize(binding_context) ⇒ Executor
Returns a new instance of Executor.
49 50 51 |
# File 'lib/console_agent/executor.rb', line 49 def initialize(binding_context) @binding_context = binding_context end |
Instance Attribute Details
#binding_context ⇒ Object (readonly)
Returns the value of attribute binding_context.
46 47 48 |
# File 'lib/console_agent/executor.rb', line 46 def binding_context @binding_context end |
#on_prompt ⇒ Object
Returns the value of attribute on_prompt.
47 48 49 |
# File 'lib/console_agent/executor.rb', line 47 def on_prompt @on_prompt end |
Instance Method Details
#confirm_and_execute(code) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/console_agent/executor.rb', line 115 def confirm_and_execute(code) return nil if code.nil? || code.strip.empty? @last_cancelled = false @last_answer = nil $stdout.print colorize("Execute? [y/N/edit] ", :yellow) @on_prompt&.call answer = $stdin.gets.to_s.strip.downcase @last_answer = answer echo_stdin(answer) case answer when 'y', 'yes' execute(code) when 'e', 'edit' edited = open_in_editor(code) if edited && edited != code $stdout.puts colorize("# Edited code:", :yellow) $stdout.puts highlight_code(edited) $stdout.print colorize("Execute edited code? [y/N] ", :yellow) edit_answer = $stdin.gets.to_s.strip.downcase echo_stdin(edit_answer) if edit_answer == 'y' execute(edited) else $stdout.puts colorize("Cancelled.", :yellow) nil end else execute(code) end else $stdout.puts colorize("Cancelled.", :yellow) @last_cancelled = true nil end end |
#display_response(response) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/console_agent/executor.rb', line 58 def display_response(response) code = extract_code(response) explanation = response.gsub(CODE_REGEX, '').strip $stdout.puts $stdout.puts colorize(explanation, :cyan) unless explanation.empty? unless code.empty? $stdout.puts $stdout.puts colorize("# Generated code:", :yellow) $stdout.puts highlight_code(code) $stdout.puts end code end |
#execute(code) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/console_agent/executor.rb', line 75 def execute(code) return nil if code.nil? || code.strip.empty? captured_output = StringIO.new old_stdout = $stdout # Tee output: capture it and also print to the real stdout $stdout = TeeIO.new(old_stdout, captured_output) result = binding_context.eval(code, "(console_agent)", 1) $stdout = old_stdout $stdout.puts colorize("=> #{result.inspect}", :green) @last_output = captured_output.string result rescue SyntaxError => e $stdout = old_stdout if old_stdout $stderr.puts colorize("SyntaxError: #{e.message}", :red) @last_output = nil nil rescue => e $stdout = old_stdout if old_stdout $stderr.puts colorize("Error: #{e.class}: #{e.message}", :red) e.backtrace.first(3).each { |line| $stderr.puts colorize(" #{line}", :red) } @last_output = captured_output&.string nil end |
#extract_code(response) ⇒ Object
53 54 55 56 |
# File 'lib/console_agent/executor.rb', line 53 def extract_code(response) match = response.match(CODE_REGEX) match ? match[1].strip : '' end |
#last_answer ⇒ Object
107 108 109 |
# File 'lib/console_agent/executor.rb', line 107 def last_answer @last_answer end |
#last_cancelled? ⇒ Boolean
111 112 113 |
# File 'lib/console_agent/executor.rb', line 111 def last_cancelled? @last_cancelled end |
#last_output ⇒ Object
103 104 105 |
# File 'lib/console_agent/executor.rb', line 103 def last_output @last_output end |