55import importlib .metadata
66import logging
77import optparse
8- from pathlib import PurePosixPath
8+ from pathlib import Path , PurePosixPath
99import sys
1010from typing import Sequence
1111
1212from .bots import load_bot
1313from .common import PROGRAM , Config , UnreachableError , ensure_state_home
1414from .drafter import Drafter
1515from .editor import open_editor
16- from .prompt import TemplatedPrompt , template_source , templates_table
16+ from .prompt import Template , TemplatedPrompt , templates_table
1717from .store import Store
1818from .toolbox import ToolVisitor
1919
@@ -54,9 +54,9 @@ def callback(_option, _opt, _value, parser) -> None:
5454
5555 add_command ("finalize" , help = "apply current draft to original branch" )
5656 add_command ("generate" , help = "start a new draft from a prompt" )
57- add_command ("history " , help = "show history drafts or prompts " )
58- add_command ("revert " , help = "discard the current draft " )
59- add_command ("templates" , help = "show template information" )
57+ add_command ("show-drafts " , short = "D" , help = "show draft history " )
58+ add_command ("show-prompts " , short = "P" , help = "show prompt history " )
59+ add_command ("show- templates" , short = "T " , help = "show template information" )
6060
6161 parser .add_option (
6262 "-b" ,
@@ -76,7 +76,12 @@ def callback(_option, _opt, _value, parser) -> None:
7676 help = "delete draft after finalizing or discarding" ,
7777 action = "store_true" ,
7878 )
79- # TODO: Add edit option. Works both for prompts and templates.
79+ parser .add_option (
80+ "-e" ,
81+ "--edit" ,
82+ help = "edit prompt or template" ,
83+ action = "store_true" ,
84+ )
8085 parser .add_option (
8186 "-j" ,
8287 "--json" ,
@@ -85,8 +90,8 @@ def callback(_option, _opt, _value, parser) -> None:
8590 )
8691 parser .add_option (
8792 "-r" ,
88- "--reset " ,
89- help = "reset index before generating a new draft" ,
93+ "--revert " ,
94+ help = "abandon any changes since draft creation " ,
9095 action = "store_true" ,
9196 )
9297 parser .add_option (
@@ -95,11 +100,23 @@ def callback(_option, _opt, _value, parser) -> None:
95100 help = "commit prior worktree changes separately" ,
96101 action = "store_true" ,
97102 )
103+
104+ parser .add_option (
105+ "--no-reset" ,
106+ help = "abort if there are any staged changes" ,
107+ dest = "reset" ,
108+ action = "store_false" ,
109+ )
110+ parser .add_option (
111+ "--reset" ,
112+ help = "reset index before generating a new draft" ,
113+ dest = "reset" ,
114+ action = "store_true" ,
115+ )
98116 parser .add_option (
99- "-t" ,
100117 "--timeout" ,
101118 dest = "timeout" ,
102- help = "bot generation timeout" ,
119+ help = "generation timeout" ,
103120 )
104121
105122 return parser
@@ -125,6 +142,17 @@ def on_delete_file(self, path: PurePosixPath, _reason: str | None) -> None:
125142 print (f"Deleted { path } ." )
126143
127144
145+ def edit (text : str | None , path : Path | None ) -> str | None :
146+ if sys .stdin .isatty ():
147+ return open_editor (text or "" , path )
148+ else :
149+ if path and text is not None :
150+ with open (path , "w" ) as f :
151+ f .write (text )
152+ print (path )
153+ return None
154+
155+
128156def main () -> None :
129157 config = Config .load ()
130158 (opts , args ) = new_parser ().parse_args ()
@@ -162,22 +190,35 @@ def main() -> None:
162190 bot ,
163191 bot_name = opts .bot ,
164192 tool_visitors = [ToolPrinter ()],
165- reset = opts .reset ,
193+ reset = config .auto_reset if opts .reset is None else opts .reset ,
194+ sync = opts .sync ,
166195 )
167196 print (f"Generated { name } ." )
168197 elif command == "finalize" :
169- name = drafter .finalize_draft ( clean = opts . clean , delete = opts . delete )
170- print ( f"Finalized { name } ." )
171- elif command == "revert" :
172- name = drafter . revert_draft ( delete = opts .delete )
173- print (f"Reverted { name } ." )
174- elif command == "history " :
198+ name = drafter .exit_draft (
199+ revert = opts . revert , clean = opts . clean , delete = opts . delete
200+ )
201+ verb = "Reverted" if opts .revert else "Finalized"
202+ print (f"{ verb } { name } ." )
203+ elif command == "show-drafts " :
175204 table = drafter .history_table (args [0 ] if args else None )
176205 if table :
177206 print (table .to_json () if opts .json else table )
178- elif command == "templates" :
207+ elif command == "show-prompts" :
208+ raise NotImplementedError () # TODO
209+ elif command == "show-templates" :
179210 if args :
180- print (template_source (args [0 ]))
211+ name = args [0 ]
212+ tpl = Template .find (name )
213+ if opts .edit :
214+ if tpl :
215+ edit (tpl .source , tpl .local_path ())
216+ else :
217+ edit ("" , Template .local_path_for (name ))
218+ else :
219+ if not tpl :
220+ raise ValueError (f"No template named { name !r} " )
221+ print (tpl .source )
181222 else :
182223 table = templates_table ()
183224 print (table .to_json () if opts .json else table )
0 commit comments