Skip to content
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

Open
jayqi opened this issue Jan 14, 2025 · 1 comment
Open

Launching editors #298

jayqi opened this issue Jan 14, 2025 · 1 comment

Comments

@jayqi
Copy link

jayqi commented Jan 14, 2025

Click has the functionality for opening an editor. It would be nice to have that available in Cyclopts as well.

@BrianPugh
Copy link
Owner

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 edit function, and I'm not sure why it's so complicated. I think below is a "good enough" implementation, but let me know what you think! I also have not really ever implemented this in my own CLIs before, so let me know if there are tweaks that can make it a bit more ergonomic.

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=}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants