Skip to content

Commit dda3a69

Browse files
authored
Merge pull request #44 from 0xacx/allow-input-from-redirected-prompt
Allow input from redirected prompt
2 parents 192a672 + 2d29a80 commit dda3a69

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

chatgpt.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,17 @@ if [ ! -f ~/.chatgpt_history ]; then
215215
fi
216216

217217
running=true
218-
# check input source
219-
# if prompt already entered, run on pipe mode (run once, no chat)
220-
# prompt from pipe
221-
if [ -p /dev/stdin ]; then
222-
pipe_mode_prompt+=$(cat -)
223-
# prompt from argument
224-
elif [ -n "$prompt" ]; then
218+
# check input source and determine run mode
219+
220+
# prompt from argument, run on pipe mode (run once, no chat)
221+
if [ -n "$prompt" ]; then
225222
pipe_mode_prompt=${prompt}
226-
else
223+
# if input file_descriptor is a terminal, run on chat mode
224+
elif [ -t 0 ]; then
227225
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m'."
226+
# prompt from pipe or redirected stdin, run on pipe mode
227+
else
228+
pipe_mode_prompt+=$(cat -)
228229
fi
229230

230231
while $running; do

0 commit comments

Comments
 (0)