diff --git a/README.md b/README.md index 78ed0cd4..dab1f477 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,7 @@ The Chat REPL supports: .exit Exit the REPL Type ::: to begin multi-line editing, type ::: to end it. +Press Ctrl+O to open an editor to modify the current prompt. Press Ctrl+C to abort readline, Ctrl+D to exit the REPL ``` diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 832ffb4e..56fdddd5 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -20,6 +20,7 @@ use reedline::{ ColumnarMenu, EditMode, Emacs, KeyCode, KeyModifiers, Keybindings, Reedline, ReedlineEvent, ReedlineMenu, ValidationResult, Validator, Vi, }; +use std::{env, process}; const MENU_NAME: &str = "completion_menu"; @@ -292,7 +293,7 @@ Type ".help" for more information. let highlighter = ReplHighlighter::new(config); let menu = Self::create_menu(); let edit_mode = Self::create_edit_mode(config); - let editor = Reedline::create() + let mut editor = Reedline::create() .with_completer(Box::new(completer)) .with_highlighter(Box::new(highlighter)) .with_menu(menu) @@ -303,6 +304,13 @@ Type ".help" for more information. .with_validator(Box::new(ReplValidator)) .with_ansi_colors(true); + if let Ok(cmd) = env::var("VISUAL").or_else(|_| env::var("EDITOR")) { + let temp_file = + env::temp_dir().join(format!("aichat-{}.txt", chrono::Utc::now().timestamp())); + let command = process::Command::new(cmd); + editor = editor.with_buffer_editor(command, temp_file); + } + Ok(editor) } @@ -400,6 +408,7 @@ fn dump_repl_help() { r###"{head} Type ::: to begin multi-line editing, type ::: to end it. +Press Ctrl+O to open an editor to modify the current prompt. Press Ctrl+C to abort aichat, Ctrl+D to exit the REPL"###, ); }