Skip to content

Commit 4deb784

Browse files
committed
Add awesome command to display all-awesome-prompts
1 parent 55b8b80 commit 4deb784

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/pytgpt/console.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,11 @@ def generate(
11091109
provider,
11101110
quiet,
11111111
)
1112+
if with_copied:
1113+
sep = "\n" if prompt else ""
1114+
prompt = prompt + sep + clipman.get()
1115+
assert prompt.strip() != sep, "No copied text found, issue prompt"
1116+
11121117
if not prompt:
11131118
# Let's try to read piped input
11141119
import signal
@@ -1137,8 +1142,6 @@ def timeout_handler(signum, frame):
11371142
# Just incase the previous timeout was not 0
11381143

11391144
clear_history_file(filepath, new)
1140-
if with_copied:
1141-
prompt = prompt + "\n" + clipman.get()
11421145
prompt = Optimizers.code(prompt) if code else prompt
11431146
prompt = Optimizers.shell_command(prompt) if shell else prompt
11441147
busy_bar.spin_index = busy_bar_index
@@ -1263,6 +1266,57 @@ def delete(name, case_sensitive, file):
12631266
return AwesomePrompts().delete_prompt(name, case_sensitive)
12641267

12651268

1269+
@awesome.command()
1270+
@click.option(
1271+
"-j",
1272+
"--json",
1273+
is_flag=True,
1274+
help="Display prompts in json format",
1275+
)
1276+
@click.option(
1277+
"-i",
1278+
"--indent",
1279+
type=click.IntRange(1, 20),
1280+
help="Json format indentation level",
1281+
default=4,
1282+
)
1283+
@click.option(
1284+
"-x",
1285+
"--index",
1286+
is_flag=True,
1287+
help="Display prompts with their corresponding indexes",
1288+
)
1289+
@click.option("-c", "--color", help="Prompts stdout font color")
1290+
@click.option("-o", "--output", type=click.Path(), help="Path to save the prompts")
1291+
@click.help_option("-h", "--help")
1292+
def all(json, indent, index, color, output):
1293+
"""Stdout all awesome prompts"""
1294+
ap = AwesomePrompts()
1295+
awesome_prompts = ap.all_acts if index else ap.get_acts()
1296+
from json import dumps
1297+
1298+
formatted_awesome_prompts = dumps(awesome_prompts)
1299+
if json:
1300+
# click.secho(formatted_awesome_prompts, fg=color)
1301+
rich.print_json(formatted_awesome_prompts,indent=indent)
1302+
1303+
else:
1304+
awesome_table = Table(show_lines=True, title="All Awesome-Prompts")
1305+
awesome_table.add_column("index", justify="center", style="yellow")
1306+
awesome_table.add_column("Act Name/Index", justify="left", style="cyan")
1307+
awesome_table.add_column(
1308+
"Prompt",
1309+
style=color,
1310+
)
1311+
for index, key_value in enumerate(awesome_prompts.items()):
1312+
awesome_table.add_row(str(index), str(key_value[0]), key_value[1])
1313+
rich.print(awesome_table)
1314+
1315+
if output:
1316+
with open(output, "w") as fh:
1317+
fh.write(formatted_awesome_prompts)
1318+
1319+
12661320
tgpt2_.add_command(
12671321
webchatgpt, "webchatgpt"
12681322
) # Intergration with WebChatGPT https://github.com/Simatwa/WebChatGPT

src/pytgpt/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def __search_key(self, key: str, raise_not_found: bool = False) -> str:
205205
if raise_not_found:
206206
raise KeyError(f"Zero awesome prompt found with key - `{key}`")
207207

208-
def __get_acts(self):
208+
def get_acts(self):
209209
"""Retrieves all awesome-prompts"""
210210
with open(self.awesome_prompt_path) as fh:
211211
prompt_dict = json.load(fh)
@@ -225,7 +225,7 @@ def update_prompts_from_online(self, override: bool = False):
225225
response.raise_for_status
226226
resp.update(response.json())
227227
if os.path.isfile(self.awesome_prompt_path) and not override:
228-
resp.update(self.__get_acts())
228+
resp.update(self.get_acts())
229229
self.__is_prompt_updated = True
230230
with open(self.awesome_prompt_path, "w") as fh:
231231
json.dump(resp, fh, indent=4)
@@ -243,9 +243,9 @@ def all_acts(self) -> dict:
243243
resp = {}
244244
if not os.path.isfile(self.awesome_prompt_path):
245245
self.update_prompts_from_online()
246-
resp.update(self.__get_acts())
246+
resp.update(self.get_acts())
247247

248-
for count, key_value in enumerate(self.__get_acts().items()):
248+
for count, key_value in enumerate(self.get_acts().items()):
249249
# Lets map also index to the value
250250
resp.update({count: key_value[1]})
251251

@@ -289,7 +289,7 @@ def add_prompt(self, name: str, prompt: str) -> bool:
289289
Returns:
290290
bool: is_successful report
291291
"""
292-
current_prompts = self.__get_acts()
292+
current_prompts = self.get_acts()
293293
with open(self.awesome_prompt_path, "w") as fh:
294294
current_prompts[name] = prompt
295295
json.dump(current_prompts, fh, indent=4)
@@ -308,7 +308,7 @@ def delete_prompt(
308308
bool: is_successful report
309309
"""
310310
name = self.__search_key(name, raise_not_found) if case_insensitive else name
311-
current_prompts = self.__get_acts()
311+
current_prompts = self.get_acts()
312312
is_name_available = (
313313
current_prompts[name] if raise_not_found else current_prompts.get(name)
314314
)

0 commit comments

Comments
 (0)