@@ -131,26 +131,35 @@ def on_list_files(
131131 def on_read_file (
132132 self , path : PurePosixPath , _contents : str | None , _reason : str | None
133133 ) -> None :
134- print (f"Reading { path } ..." )
134+ print (f"Reading { path !r } ..." )
135135
136136 def on_write_file (
137137 self , path : PurePosixPath , _contents : str , _reason : str | None
138138 ) -> None :
139- print (f"Updated { path } ." )
139+ print (f"Updated { path !r } ." )
140140
141141 def on_delete_file (self , path : PurePosixPath , _reason : str | None ) -> None :
142- print (f"Deleted { path } ." )
142+ print (f"Deleted { path !r } ." )
143143
144144
145- def edit (path : Path , text : str | None = None ) -> str | None :
145+ def edit (* , path : Path | None = None , text : str | None = None ) -> str :
146146 if sys .stdin .isatty ():
147147 return open_editor (text or "" , path )
148148 else :
149- if text is not None :
150- with open (path , "w" ) as f :
151- f .write (text )
152- print (path )
153- return None
149+ # We exit with a custom code to allow the caller to act accordingly.
150+ # For example we can handle this from Vim by opening the returned path
151+ # or text in a buffer, to then continue to another command on save.
152+ # https://unix.stackexchange.com/q/604260
153+ if path is None :
154+ assert text , "Empty path and text"
155+ print (text )
156+ sys .exit (198 )
157+ else :
158+ if text is not None :
159+ with open (path , "w" ) as f :
160+ f .write (text )
161+ print (path )
162+ sys .exit (199 )
154163
155164
156165_PROMPT_PLACEHOLDER = "Enter your prompt here..."
@@ -185,8 +194,8 @@ def main() -> None:
185194 prompt = TemplatedPrompt .parse (args [0 ], * args [1 :])
186195 elif opts .edit :
187196 editable = False
188- prompt = open_editor (
189- drafter .latest_draft_prompt () or _PROMPT_PLACEHOLDER
197+ prompt = edit (
198+ text = drafter .latest_draft_prompt () or _PROMPT_PLACEHOLDER
190199 )
191200 else :
192201 prompt = sys .stdin .read ()
@@ -219,9 +228,9 @@ def main() -> None:
219228 tpl = Template .find (name )
220229 if opts .edit :
221230 if tpl :
222- edit (tpl .local_path (), text = tpl .source )
231+ edit (path = tpl .local_path (), text = tpl .source )
223232 else :
224- edit (Template .local_path_for (name ))
233+ edit (path = Template .local_path_for (name ))
225234 else :
226235 if not tpl :
227236 raise ValueError (f"No template named { name !r} " )
0 commit comments