Skip to content

Commit b7a45a3

Browse files
committed
Add placeholder for piped & copied inputs - #13
1 parent d9fd31e commit b7a45a3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,5 @@ Assume role of the LLM and give your response.*
185185
</details>
186186

187187
- Combine both of piped and explicitly issued prompt #13
188-
- Support piping input in Windows. #13
188+
- Support piping input in Windows. #13
189+
- Placeholder for piped input `{{stream}}` and copied text `{{copied}}`.

src/pytgpt/console.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,16 +1184,23 @@ def generate(
11841184
provider,
11851185
quiet,
11861186
)
1187+
prompt = prompt if prompt else ""
11871188
if with_copied:
1188-
sep = "\n" if prompt else ""
1189-
prompt = prompt + sep + clipman.get()
1190-
assert prompt.strip() != sep, "No copied text found, issue prompt"
1189+
copied_placeholder = "{{copied}}"
1190+
last_copied_text = clipman.get()
1191+
assert last_copied_text, "No copied text found, issue prompt"
1192+
1193+
if copied_placeholder in prompt:
1194+
prompt = prompt.replace(copied_placeholder, last_copied_text)
1195+
else:
1196+
sep = "\n" if prompt else ""
1197+
prompt = prompt + sep + last_copied_text
11911198

11921199
if not prompt and sys.stdin.isatty(): # No prompt issued and no piped input
11931200
help_info = (
11941201
"Usage: pytgpt generate [OPTIONS] PROMPT\n"
11951202
"Try 'pytgpt generate --help' for help.\n"
1196-
"Error: Missing argument 'PROMPT'.\n"
1203+
"Error: Missing argument 'PROMPT'."
11971204
)
11981205
click.secho(
11991206
help_info
@@ -1202,8 +1209,12 @@ def generate(
12021209

12031210
if not sys.stdin.isatty(): # Piped input detected - True
12041211
# Let's try to read piped input
1205-
stdin = click.get_text_stream("stdin").read()
1206-
prompt = prompt + "\n" + stdin if prompt else stdin
1212+
stream_text = click.get_text_stream("stdin").read()
1213+
stream_placeholder = "{{stream}}"
1214+
if stream_placeholder in prompt:
1215+
prompt = prompt.replace(stream_placeholder, stream_text)
1216+
else:
1217+
prompt = prompt + "\n" + stream_text if prompt else stream_text
12071218

12081219
clear_history_file(filepath, new)
12091220
prompt = Optimizers.code(prompt) if code else prompt

0 commit comments

Comments
 (0)