add `my` command and simple PR helper

master
blob42 1 year ago
parent 3e41556b98
commit 2004bafba7

@ -5,18 +5,33 @@ import os
import sys
import requests
import csv
import uuid
# We will need to import the `click` package to build the CLI.
import click
from github import Github
# We will also need to import the `fzf` package to use the `fzf` command.
from pyfzf.pyfzf import FzfPrompt as Fzf
XDG_CONFIG_HOME = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
CONFIG_DIR = os.path.join(XDG_CONFIG_HOME, "cgpt")
XDG_DATA_HOME = os.environ.get('XDG_DATA_HOME', os.path.expanduser('~/.local/share'))
PROMPTS_URL = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
CACHE_LOCATION = os.path.join(XDG_DATA_HOME, "chatgpt-prompts", "prompts.csv")
PROMPTS_REPO_URL = "https://github.com/f/awesome-chatgpt-prompts"
REPO_PROMPTS_RELPATH = "prompts.csv"
TOKEN_FILE = os.path.join(CONFIG_DIR, "token")
PR_HELP = """
helper to create a PR from a custom prompt. The output can be appended to the prompts.csv file in the prompts repo. You can
pipe the output of this command to pbcopy or xclip to copy it to your
clipboard.
For example: `./cgpt.py pr | xclip -selection cliboard`
"""
# save custom prompts to my-prompts.csv
CUSTOM_PROMPTS = os.path.join(XDG_DATA_HOME, "chatgpt-prompts", "my-prompts.csv")
@ -39,6 +54,11 @@ def cache_prompts(update=False):
f.write(header)
f.close()
def init_config():
if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR)
def load_prompts(custom_only=False):
'''
Loads the prompts from the cache. if the cache is empty, download the prompts.
@ -85,58 +105,83 @@ def save_prompt(act, prompt):
def cli():
pass
# select and return a tuple (act, prompt)
def choose_prompt(custom_only=False):
prompts = load_prompts(custom_only)
# load custom prompts
class Fzfer:
def __init__(self, prompts):
self.act_prompt = prompts
self.fzf = Fzf()
self.header = "act,prompt"
self.preview = f"'cat {CACHE_LOCATION} | grep {{1}} | cut -d, -f 2'"
self.preview_window = "right:50%,wrap"
chooser = Fzf()
header = "'Select an act'"
def show(self):
return self.fzf.prompt(self.act_prompt, '--header={} --preview-window={} --preview {}'.format(self.header, self.preview_window, self.preview))
# preview the prompt of the selected act
preview = f"'cat {CACHE_LOCATION} | grep {{1}} | cut -d, -f 2'"
preview_window = "right:50%,wrap"
act = chooser.prompt(prompts.keys(), '--header={} --preview-window={} --preview {}'.format(header, preview_window, preview,))
# select and return a tuple (act, prompt)
def choose_prompt(custom_only=False):
prompts = load_prompts(custom_only)
f = Fzfer(prompts)
act = f.show()
prompt = prompts.get(act[0])
return (act[0], prompt)
def show_prompts():
act, prompt = choose_prompt()
print(prompt)
# update refreshes the prompts from the remote repo
@click.command()
@click.command(help="update the prompts from the remote repo")
def update():
cache_prompts(update=True)
# We will create a function called `pr` that will be executed when the `pr`
# command is provided.
@click.command()
def pr():
print("[TODO] Opening a PR to add your prompt to the prompts repo")
def load_token():
token = None
if not os.path.exists(TOKEN_FILE):
token = input("Please enter your GitHub token: ")
with open(TOKEN_FILE, "w") as f:
f.write(token)
f.close()
else:
with open(TOKEN_FILE, "r") as f:
token = f.readline().strip()
f.close()
return token
@click.command(help=PR_HELP)
def pr():
'''
Helper to create a PR from a custom prompt
'''
# select a custom prompt
act, prompt = choose_prompt(custom_only=True)
prompt = f'{act},{prompt}'
print(prompt)
# The add command allows the user to define a new prompt by providing the act and the prompt
@click.command()
def add():
act = input("Enter the act title: ")
prompt = input("Enter the prompt: ")
save_prompt(act, prompt)
@click.command(help="Show my custom prompts")
def my():
'''
Pick custom prompts
'''
prompts = load_prompts(custom_only=True)
f = Fzfer(prompts)
act = f.show()
prompt = prompts.get(act[0])
print(prompt)
# We will add the `add`, `update`, and `pr` functions to the `show_prompts`
# function.
cli.add_command(add)
cli.add_command(update)
cli.add_command(pr)
cli.add_command(my)
# We will execute the `show_prompts` function if the script is executed directly
# without any command provided
if __name__ == "__main__":
# if no command provided call show_prompts
if len(sys.argv) == 1:

Loading…
Cancel
Save