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

Add stdin input, make every prompt editable #93

Closed
wants to merge 8 commits into from

Conversation

Shakahs
Copy link

@Shakahs Shakahs commented Mar 24, 2023

This pull request adds support for optionally receiving a prompt from stdin. Also --editor now allows you to edit prompt regardless of prompt source.

Usage:

git diff | sgpt "create a commit message for this diff" 
cat base_prompt.txt | sgpt --editor "additional prompt" 

sgpt will use either stdin prompt or argument prompt, and combine them if both are present. The --stdin-before / -b argument reverses the ordering of the two prompts when combined.

Default ordering:
echo "stdin prompt" | sgpt "argument prompt"

argument prompt
stdin prompt

Reversed ordering:
echo "stdin prompt" | sgpt --stdin-before "argument prompt"

stdin prompt
argument prompt

sgpt/utils.py Outdated Show resolved Hide resolved
sgpt/utils.py Show resolved Hide resolved
sgpt/app.py Outdated Show resolved Hide resolved
sgpt/app.py Outdated Show resolved Hide resolved
prompt: str = typer.Argument(None, show_default=False, help="The prompt to generate completions for."),
prompt: Optional[str] = typer.Argument(
None, show_default=False, help="The prompt to generate completions for."),
stdin_before: bool = typer.Option(False, "--stdin-before", "-b", help="Place stdin prompt before argument prompt, default is after."),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine. After talking to ChatGPT for a bit it looks like it would be more complicated to make it a combo option so in the --help screen it wouldn't show --no-stdin-before

"User:
I'm looking for how do I coude the other side of a bool typed default. Example:

code: bool = typer.Option(False, help="Provide code as output.")

Adds this to the help statment:

│ --code                     --no-code                                              Provide code as output. [default: no-code]                                                                                                     │

Instead of --no-code as the opposite, how would I make that --anti-code
"

"Assistant:
To make the opposite flag --anti-code, you can provide the --code option with customized --anti-code flag using the show_default and opposite parameter of the typer.Option. Here's an example:

import typer

app = typer.Typer()

@app.command()
def main(code: bool = typer.Option(
    False, 
    help="Provide code as output.", 
    show_default="--anti-code", 
)):
    if code:
        typer.echo("Code")
    else:
        typer.echo("Anti-Code")

if __name__ == "__main__":
    app()

In this example, the show_default is set to --anti-code, and while it won't appear as an option in help, the output will show --anti-code as the default opposite flag. To include it as an actual alternative flag, you would need to use two options and process them in your function:

import typer

app = typer.Typer()

@app.command()
def main(
    code: bool = typer.Option(False, "--code", help="Provide code as output."),
    anti_code: bool = typer.Option(False, "--anti-code", help="Provide code as output.")
):
    result = "Neither"
    
    if code and not anti_code:
        result = "Code"
    elif not code and anti_code:
        result = "Anti-Code"
    elif code and anti_code:
        result = "Error: Both --code and --anti-code can't be used simultaneously."

    typer.echo(result)

if __name__ == "__main__":
    app()

Now you explicitly have --code and --anti-code available as options, and the function checks for their combinations and takes appropriate actions accordingly.
"

Copy link

@lpurdy01 lpurdy01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, and executes fine in codespace.

@TheR1D
Copy link
Owner

TheR1D commented Apr 10, 2023

Thank you for the PR, since we are rolling out new versions of sgpt 3-4 times per week, feature branches are getting outdated very fast, this feature was implemented in my branch and merged today #163

@TheR1D TheR1D closed this Apr 10, 2023
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

Successfully merging this pull request may close these issues.

None yet

4 participants