-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Launching editors #298
Comments
I'm not 100% opposed to the idea, but I like to typically think that it's outside the scope of Cyclopts. For example, for most interactive CLI prompts, I would recommend using questionary. However, this isn't 100% in the scope of questionary either. With all that said, I took a brief look at Click's import tempfile
import subprocess
import os
from pathlib import Path
class DidNotSaveError(Exception):
"""User did not save upon exiting."""
def edit(initial_text="", default_editor="vim", save=True):
"""Get text input from a user via their default editor."""
editor = os.environ.get("EDITOR", default_editor)
with tempfile.NamedTemporaryFile(suffix=".txt", mode="w", delete=False) as tf:
path = Path(tf.name)
tf.write(initial_text)
start_mtime = path.stat().st_mtime
try:
subprocess.call([editor, path])
end_mtime = path.stat().st_mtime
if save and end_mtime <= start_mtime:
raise DidNotSaveError
edited_text = path.read_text()
finally:
path.unlink()
return edited_text
result = edit()
print(f"{result=}") |
This is probably generically useful enough, and not exactly available from a focused package. Implementing it had enough "sharp corners" that it's non-trivial enough to include in Cyclopts. Added in v3.3.0 |
Click has the functionality for opening an editor. It would be nice to have that available in Cyclopts as well.
The text was updated successfully, but these errors were encountered: