diff --git a/src/smarttools/ui_urwid.py b/src/smarttools/ui_urwid.py index 84cb043..8138628 100644 --- a/src/smarttools/ui_urwid.py +++ b/src/smarttools/ui_urwid.py @@ -1788,7 +1788,54 @@ Return ONLY the Python code, no explanations or markdown fencing.""" def on_cancel(_): self.close_overlay() + def on_external_edit(_): + """Open code in external editor ($EDITOR).""" + import os + import subprocess + import tempfile + + current_code = code_edit.edit_text + + # Stop the urwid loop temporarily + if self.loop: + self.loop.stop() + + try: + # Create temp file with current code + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: + f.write(current_code) + temp_path = f.name + + # Get editor from environment + editor = os.environ.get('EDITOR', os.environ.get('VISUAL', 'nano')) + + # Run editor + subprocess.run([editor, temp_path], check=True) + + # Read back the edited code + with open(temp_path, 'r') as f: + new_code = f.read() + + # Update the code editor + code_edit.set_edit_text(new_code) + status_text.set_text(('success', f"Code updated from {editor}")) + + # Clean up temp file + os.unlink(temp_path) + + except subprocess.CalledProcessError: + status_text.set_text(('error', "Editor exited with error")) + except FileNotFoundError: + status_text.set_text(('error', f"Editor '{editor}' not found")) + except Exception as e: + status_text.set_text(('error', f"Edit error: {e}")) + finally: + # Restart the urwid loop + if self.loop: + self.loop.start() + load_btn = Button3DCompact("Load", on_load) + edit_btn = Button3DCompact("$EDITOR", on_external_edit) # Code editor in a box - use ListBox for proper focus handling and scrolling # Wrap in DOSScrollBar for DOS-style scrollbar with arrow buttons @@ -1814,6 +1861,8 @@ Return ONLY the Python code, no explanations or markdown fencing.""" ('weight', 1, urwid.AttrMap(file_edit, 'edit', 'edit_focus')), ('pack', urwid.Text(" ")), ('pack', load_btn), + ('pack', urwid.Text(" ")), + ('pack', edit_btn), ])), ('pack', status_text), ('weight', 1, main_columns),