Skip to content

Commit 42d82b9

Browse files
authored
Merge pull request #85 from 0xacx/improve-UI
Make processing label disappear once response is received, enable glo…
2 parents de8aaaa + 9e98337 commit 42d82b9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

chatgpt.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ SYSTEM_PROMPT="You are ChatGPT, a large language model trained by OpenAI. Answer
88

99
COMMAND_GENERATION_PROMPT="You are a Command Line Interface expert and your task is to provide functioning shell commands. Return a CLI command and nothing else - do not send it in a code block, quotes, or anything else, just the pure text CONTAINING ONLY THE COMMAND. If possible, return a one-line bash command or chain many commands together. Return ONLY the command ready to run in the terminal. The command should do the following:"
1010

11-
CHATGPT_CYAN_LABEL="\n\033[36mchatgpt \033[0m"
11+
CHATGPT_CYAN_LABEL="\033[36mchatgpt \033[0m"
12+
PROCESSING_LABEL="\n\033[90mProcessing... \033[0m\033[0K\r"
13+
OVERWRITE_PROCESSING_LINE=" \033[0K\r"
1214

13-
PROCESSING_LABEL="\n\033[90mProcessing... \033[0m"
1415

1516
if [[ -z "$OPENAI_KEY" ]]; then
1617
echo "You need to set your OPENAI_KEY to use this script"
@@ -281,7 +282,7 @@ while $running; do
281282
echo -e "\nEnter a prompt:"
282283
read -e prompt
283284
if [ "$prompt" != "exit" ] && [ "$prompt" != "q" ]; then
284-
echo -e $PROCESSING_LABEL
285+
echo -ne $PROCESSING_LABEL
285286
fi
286287
else
287288
# set vars for pipe mode
@@ -296,6 +297,7 @@ while $running; do
296297
request_to_image "$prompt"
297298
handle_error "$image_response"
298299
image_url=$(echo $image_response | jq -r '.data[0].url')
300+
echo -e "$OVERWRITE_PROCESSING_LINE"
299301
echo -e "${CHATGPT_CYAN_LABEL}Your image was created. \n\nLink: ${image_url}\n"
300302

301303
if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
@@ -321,13 +323,15 @@ while $running; do
321323
-H "Authorization: Bearer $OPENAI_KEY")
322324
handle_error "$models_response"
323325
models_data=$(echo $models_response | jq -r -C '.data[] | {id, owned_by, created}')
326+
echo -e "$OVERWRITE_PROCESSING_LINE"
324327
echo -e "${CHATGPT_CYAN_LABEL}This is a list of models currently available at OpenAI API:\n ${models_data}"
325328
elif [[ "$prompt" =~ ^model: ]]; then
326329
models_response=$(curl https://api.openai.com/v1/models \
327330
-sS \
328331
-H "Authorization: Bearer $OPENAI_KEY")
329332
handle_error "$models_response"
330333
model_data=$(echo $models_response | jq -r -C '.data[] | select(.id=="'"${prompt#*model:}"'")')
334+
echo -e "$OVERWRITE_PROCESSING_LINE"
331335
echo -e "${CHATGPT_CYAN_LABEL}Complete details for model: ${prompt#*model:}\n ${model_data}"
332336
elif [[ "$prompt" =~ ^command: ]]; then
333337
# escape quotation marks
@@ -343,6 +347,7 @@ while $running; do
343347
response_data=$(echo $response | jq -r '.choices[].message.content')
344348

345349
if [[ "$prompt" =~ ^command: ]]; then
350+
echo -e "$OVERWRITE_PROCESSING_LINE"
346351
echo -e "${CHATGPT_CYAN_LABEL} ${response_data}" | fold -s -w $COLUMNS
347352
dangerous_commands=("rm" ">" "mv" "mkfs" ":(){:|:&};" "dd" "chmod" "wget" "curl")
348353

@@ -375,11 +380,12 @@ while $running; do
375380
handle_error "$response"
376381
response_data=$(echo "$response" | jq -r '.choices[].message.content')
377382

383+
echo -e "$OVERWRITE_PROCESSING_LINE"
378384
# if glow installed, print parsed markdown
379385
if command -v glow &>/dev/null; then
380-
formatted_text=$(echo "${response_data}" | glow)
381386
echo -e "${CHATGPT_CYAN_LABEL}"
382-
echo -e "${formatted_text}"
387+
echo "${response_data}" | glow -
388+
#echo -e "${formatted_text}"
383389
else
384390
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
385391
fi
@@ -402,11 +408,11 @@ while $running; do
402408
handle_error "$response"
403409
response_data=$(echo "$response" | jq -r '.choices[].text')
404410

411+
echo -e "$OVERWRITE_PROCESSING_LINE"
405412
# if glow installed, print parsed markdown
406413
if command -v glow &>/dev/null; then
407-
formatted_text=$(echo "${response_data}" | glow)
408414
echo -e "${CHATGPT_CYAN_LABEL}"
409-
echo -e "${formatted_text}"
415+
echo "${response_data}" | glow -
410416
else
411417
# else remove empty lines and print
412418
formatted_text=$(echo "${response_data}" | sed '1,2d; s/^A://g')

0 commit comments

Comments
 (0)