@@ -84,13 +84,8 @@ impl EditorArgs {
8484 }
8585}
8686
87- /// Opens the user's preferred editor to compose a prompt
88- pub fn open_editor ( initial_text : Option < String > ) -> Result < String , ChatError > {
89- // Create a temporary file with a unique name
90- let temp_dir = std:: env:: temp_dir ( ) ;
91- let file_name = format ! ( "q_prompt_{}.md" , Uuid :: new_v4( ) ) ;
92- let temp_file_path = temp_dir. join ( file_name) ;
93-
87+ /// Launch the user's preferred editor with the given file path
88+ fn launch_editor ( file_path : & std:: path:: Path ) -> Result < ( ) , ChatError > {
9489 // Get the editor from environment variable or use a default
9590 let editor_cmd = std:: env:: var ( "EDITOR" ) . unwrap_or_else ( |_| "vi" . to_string ( ) ) ;
9691
@@ -104,11 +99,6 @@ pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> {
10499
105100 let editor_bin = parts. remove ( 0 ) ;
106101
107- // Write initial content to the file if provided
108- let initial_content = initial_text. unwrap_or_default ( ) ;
109- std:: fs:: write ( & temp_file_path, & initial_content)
110- . map_err ( |e| ChatError :: Custom ( format ! ( "Failed to create temporary file: {}" , e) . into ( ) ) ) ?;
111-
112102 // Open the editor with the parsed command and arguments
113103 let mut cmd = std:: process:: Command :: new ( editor_bin) ;
114104 // Add any arguments that were part of the EDITOR variable
@@ -117,14 +107,37 @@ pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> {
117107 }
118108 // Add the file path as the last argument
119109 let status = cmd
120- . arg ( & temp_file_path )
110+ . arg ( file_path )
121111 . status ( )
122112 . map_err ( |e| ChatError :: Custom ( format ! ( "Failed to open editor: {}" , e) . into ( ) ) ) ?;
123113
124114 if !status. success ( ) {
125115 return Err ( ChatError :: Custom ( "Editor exited with non-zero status" . into ( ) ) ) ;
126116 }
127117
118+ Ok ( ( ) )
119+ }
120+
121+ /// Opens the user's preferred editor to edit an existing file
122+ pub fn open_editor_file ( file_path : & std:: path:: Path ) -> Result < ( ) , ChatError > {
123+ launch_editor ( file_path)
124+ }
125+
126+ /// Opens the user's preferred editor to compose a prompt
127+ pub fn open_editor ( initial_text : Option < String > ) -> Result < String , ChatError > {
128+ // Create a temporary file with a unique name
129+ let temp_dir = std:: env:: temp_dir ( ) ;
130+ let file_name = format ! ( "q_prompt_{}.md" , Uuid :: new_v4( ) ) ;
131+ let temp_file_path = temp_dir. join ( file_name) ;
132+
133+ // Write initial content to the file if provided
134+ let initial_content = initial_text. unwrap_or_default ( ) ;
135+ std:: fs:: write ( & temp_file_path, & initial_content)
136+ . map_err ( |e| ChatError :: Custom ( format ! ( "Failed to create temporary file: {}" , e) . into ( ) ) ) ?;
137+
138+ // Launch the editor
139+ launch_editor ( & temp_file_path) ?;
140+
128141 // Read the content back
129142 let content = std:: fs:: read_to_string ( & temp_file_path)
130143 . map_err ( |e| ChatError :: Custom ( format ! ( "Failed to read temporary file: {}" , e) . into ( ) ) ) ?;
0 commit comments