diff --git a/README.md b/README.md index 71c9c73..dee1a39 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,6 @@ pipx install git-draft[openai] * Add configuration option to auto sync and `--no-sync` flag. Similar to reset. * Add "amend" commit when finalizing. This could be useful training data, showing what the bot did not get right. -* Convenience functionality for simple cases: checkout option which applies the - changes, and finalizes the draft if specified multiple times. For example `git - draft -cc add-test symbol=foo` +* Convenience `--accept` functionality for simple cases: checkout option which + applies the changes, and finalizes the draft if specified multiple times. For + example `git draft -aa add-test symbol=foo` diff --git a/src/git_draft/__init__.py b/src/git_draft/__init__.py index 64b74e5..1c23420 100644 --- a/src/git_draft/__init__.py +++ b/src/git_draft/__init__.py @@ -1,11 +1,12 @@ import logging -from .bots import Action, Bot, Toolbox +from .bots import Action, Bot, Goal, Toolbox __all__ = [ "Action", "Bot", + "Goal", "Toolbox", ] diff --git a/src/git_draft/__main__.py b/src/git_draft/__main__.py index 7b5b82c..20093f2 100644 --- a/src/git_draft/__main__.py +++ b/src/git_draft/__main__.py @@ -131,26 +131,35 @@ def on_list_files( def on_read_file( self, path: PurePosixPath, _contents: str | None, _reason: str | None ) -> None: - print(f"Reading {path}...") + print(f"Reading {path!r}...") def on_write_file( self, path: PurePosixPath, _contents: str, _reason: str | None ) -> None: - print(f"Updated {path}.") + print(f"Updated {path!r}.") def on_delete_file(self, path: PurePosixPath, _reason: str | None) -> None: - print(f"Deleted {path}.") + print(f"Deleted {path!r}.") -def edit(path: Path, text: str | None = None) -> str | None: +def edit(*, path: Path | None = None, text: str | None = None) -> str: if sys.stdin.isatty(): return open_editor(text or "", path) else: - if text is not None: - with open(path, "w") as f: - f.write(text) - print(path) - return None + # We exit with a custom code to allow the caller to act accordingly. + # For example we can handle this from Vim by opening the returned path + # or text in a buffer, to then continue to another command on save. + # https://unix.stackexchange.com/q/604260 + if path is None: + assert text, "Empty path and text" + print(text) + sys.exit(198) + else: + if text is not None: + with open(path, "w") as f: + f.write(text) + print(path) + sys.exit(199) _PROMPT_PLACEHOLDER = "Enter your prompt here..." @@ -185,8 +194,8 @@ def main() -> None: prompt = TemplatedPrompt.parse(args[0], *args[1:]) elif opts.edit: editable = False - prompt = open_editor( - drafter.latest_draft_prompt() or _PROMPT_PLACEHOLDER + prompt = edit( + text=drafter.latest_draft_prompt() or _PROMPT_PLACEHOLDER ) else: prompt = sys.stdin.read() @@ -219,9 +228,9 @@ def main() -> None: tpl = Template.find(name) if opts.edit: if tpl: - edit(tpl.local_path(), text=tpl.source) + edit(path=tpl.local_path(), text=tpl.source) else: - edit(Template.local_path_for(name)) + edit(path=Template.local_path_for(name)) else: if not tpl: raise ValueError(f"No template named {name!r}")