Skip to content

Commit 4d3aea8

Browse files
authored
feat: enable multi-step edit flows (#52)
1 parent 841c5a3 commit 4d3aea8

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ pipx install git-draft[openai]
2525
* Add configuration option to auto sync and `--no-sync` flag. Similar to reset.
2626
* Add "amend" commit when finalizing. This could be useful training data,
2727
showing what the bot did not get right.
28-
* Convenience functionality for simple cases: checkout option which applies the
29-
changes, and finalizes the draft if specified multiple times. For example `git
30-
draft -cc add-test symbol=foo`
28+
* Convenience `--accept` functionality for simple cases: checkout option which
29+
applies the changes, and finalizes the draft if specified multiple times. For
30+
example `git draft -aa add-test symbol=foo`

src/git_draft/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import logging
22

3-
from .bots import Action, Bot, Toolbox
3+
from .bots import Action, Bot, Goal, Toolbox
44

55

66
__all__ = [
77
"Action",
88
"Bot",
9+
"Goal",
910
"Toolbox",
1011
]
1112

src/git_draft/__main__.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)