1313from .common import PROGRAM , Config , UnreachableError , ensure_state_home
1414from .drafter import Drafter
1515from .editor import open_editor
16- from .prompt import Template , TemplatedPrompt , templates_table
16+ from .prompt import Template , TemplatedPrompt , find_template , templates_table
1717from .store import Store
1818from .toolbox import ToolVisitor
1919
@@ -47,6 +47,7 @@ def callback(
4747 _value : object ,
4848 parser : optparse .OptionParser ,
4949 ) -> None :
50+ assert parser .values
5051 parser .values .command = name
5152
5253 parser .add_option (
@@ -133,20 +134,20 @@ class ToolPrinter(ToolVisitor):
133134 def on_list_files (
134135 self , _paths : Sequence [PurePosixPath ], _reason : str | None
135136 ) -> None :
136- pass
137+ print ( "Listing available files..." )
137138
138139 def on_read_file (
139140 self , path : PurePosixPath , _contents : str | None , _reason : str | None
140141 ) -> None :
141- pass
142+ print ( f"Reading { path !r } ..." )
142143
143144 def on_write_file (
144145 self , path : PurePosixPath , _contents : str , _reason : str | None
145146 ) -> None :
146- pass
147+ print ( f"Updated { path !r } ." )
147148
148149 def on_delete_file (self , path : PurePosixPath , _reason : str | None ) -> None :
149- pass
150+ print ( f"Deleted { path !r } ." )
150151
151152
152153def edit (* , path : Path | None = None , text : str | None = None ) -> str :
@@ -158,23 +159,26 @@ def edit(*, path: Path | None = None, text: str | None = None) -> str:
158159 # https://unix.stackexchange.com/q/604260
159160 elif path is None :
160161 assert text , "Empty path and text"
162+ print (text )
161163 sys .exit (198 )
162164 else :
163165 if text is not None :
164166 with open (path , "w" ) as f :
165167 f .write (text )
168+ print (path )
166169 sys .exit (199 )
167170
168171
169172_PROMPT_PLACEHOLDER = "Enter your prompt here..."
170173
171174
172- def main () -> None : # noqa: PLR0912
175+ def main () -> None : # noqa: PLR0912 PLR0915
173176 config = Config .load ()
174177 (opts , args ) = new_parser ().parse_args ()
175178
176179 log_path = ensure_state_home () / "log"
177180 if opts .log :
181+ print (log_path )
178182 return
179183 logging .basicConfig (level = config .log_level , filename = str (log_path ))
180184
@@ -212,36 +216,43 @@ def main() -> None: # noqa: PLR0912
212216 reset = config .auto_reset if opts .reset is None else opts .reset ,
213217 sync = opts .sync ,
214218 )
219+ print (f"Refined { name } ." )
215220 elif command == "finalize" :
216221 name = drafter .exit_draft (
217222 revert = opts .revert , clean = opts .clean , delete = opts .delete
218223 )
224+ verb = "Reverted" if opts .revert else "Finalized"
225+ print (f"{ verb } { name } ." )
219226 elif command == "show-drafts" :
220227 table = drafter .history_table (args [0 ] if args else None )
221228 if table :
222- pass
229+ print ( table . to_json () if opts . json else table )
223230 elif command == "show-prompts" :
224231 raise NotImplementedError () # TODO: Implement
225232 elif command == "show-templates" :
226233 if args :
227234 name = args [0 ]
228- tpl = Template . find (name )
235+ tpl = find_template (name )
229236 if opts .edit :
230237 if tpl :
231238 edit (path = tpl .local_path (), text = tpl .source )
232239 else :
233240 edit (path = Template .local_path_for (name ))
234- elif not tpl :
235- raise ValueError (f"No template named { name !r} " )
241+ else :
242+ if not tpl :
243+ raise ValueError (f"No template named { name !r} " )
244+ print (tpl .source )
236245 else :
237246 table = templates_table ()
247+ print (table .to_json () if opts .json else table )
238248 else :
239249 raise UnreachableError ()
240250
241251
242252if __name__ == "__main__" :
243253 try :
244254 main ()
245- except Exception :
255+ except Exception as err :
246256 _logger .exception ("Program failed." )
257+ print (f"Error: { err } " , file = sys .stderr )
247258 sys .exit (1 )
0 commit comments