add my command and simple PR helper

This commit is contained in:
blob42 2023-01-07 01:05:04 +01:00
parent 3e41556b98
commit 2004bafba7

95
cgpt.py
View File

@ -5,18 +5,33 @@ import os
import sys import sys
import requests import requests
import csv import csv
import uuid
# We will need to import the `click` package to build the CLI. # We will need to import the `click` package to build the CLI.
import click import click
from github import Github
# We will also need to import the `fzf` package to use the `fzf` command. # We will also need to import the `fzf` package to use the `fzf` command.
from pyfzf.pyfzf import FzfPrompt as Fzf 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')) 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" 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") CACHE_LOCATION = os.path.join(XDG_DATA_HOME, "chatgpt-prompts", "prompts.csv")
PROMPTS_REPO_URL = "https://github.com/f/awesome-chatgpt-prompts" PROMPTS_REPO_URL = "https://github.com/f/awesome-chatgpt-prompts"
REPO_PROMPTS_RELPATH = "prompts.csv" 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 # save custom prompts to my-prompts.csv
CUSTOM_PROMPTS = os.path.join(XDG_DATA_HOME, "chatgpt-prompts", "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.write(header)
f.close() f.close()
def init_config():
if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR)
def load_prompts(custom_only=False): def load_prompts(custom_only=False):
''' '''
Loads the prompts from the cache. if the cache is empty, download the prompts. 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(): def cli():
pass pass
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"
def show(self):
return self.fzf.prompt(self.act_prompt, '--header={} --preview-window={} --preview {}'.format(self.header, self.preview_window, self.preview))
# select and return a tuple (act, prompt) # select and return a tuple (act, prompt)
def choose_prompt(custom_only=False): def choose_prompt(custom_only=False):
prompts = load_prompts(custom_only) prompts = load_prompts(custom_only)
f = Fzfer(prompts)
# load custom prompts act = f.show()
chooser = Fzf()
header = "'Select an act'"
# 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,))
prompt = prompts.get(act[0]) prompt = prompts.get(act[0])
return (act[0], prompt) return (act[0], prompt)
def show_prompts(): def show_prompts():
act, prompt = choose_prompt() act, prompt = choose_prompt()
print(prompt) print(prompt)
# update refreshes the prompts from the remote repo # update refreshes the prompts from the remote repo
@click.command() @click.command(help="update the prompts from the remote repo")
def update(): def update():
cache_prompts(update=True) cache_prompts(update=True)
# We will create a function called `pr` that will be executed when the `pr` def load_token():
# command is provided. token = None
@click.command() 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(): def pr():
print("[TODO] Opening a PR to add your prompt to the prompts repo") '''
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() @click.command()
def add(): def add():
act = input("Enter the act title: ") act = input("Enter the act title: ")
prompt = input("Enter the prompt: ") prompt = input("Enter the prompt: ")
save_prompt(act, 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(add)
cli.add_command(update) cli.add_command(update)
cli.add_command(pr) 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 __name__ == "__main__":
# if no command provided call show_prompts # if no command provided call show_prompts
if len(sys.argv) == 1: if len(sys.argv) == 1: