Skip to content

Commit 752d7b3

Browse files
authored
Merge pull request #81 from 0xacx/add-markdown-support
Print with markdown if glow is installed
2 parents 7627040 + 5fed80d commit 752d7b3

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

chatgpt.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ COMMAND_GENERATION_PROMPT="Return a one-line bash command with the functionality
1010

1111
CHATGPT_CYAN_LABEL="\n\033[36mchatgpt \033[0m"
1212

13-
PROCESSING_LABEL="\n\033[90mprocessing... \033[0m"
13+
PROCESSING_LABEL="\n\033[90mProcessing... \033[0m"
1414

1515
if [[ -z "$OPENAI_KEY" ]]; then
1616
echo "You need to set your OPENAI_KEY to use this script"
@@ -375,8 +375,14 @@ while $running; do
375375
handle_error "$response"
376376
response_data=$(echo "$response" | jq -r '.choices[].message.content')
377377

378-
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
379-
378+
# if glow installed, print parsed markdown
379+
if command -v glow &>/dev/null; then
380+
formatted_text=$(echo "${response_data}" | glow)
381+
echo -e "${CHATGPT_CYAN_LABEL}"
382+
echo -e "${formatted_text}"
383+
else
384+
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
385+
fi
380386
escaped_response_data=$(echo "$response_data" | sed 's/"/\\"/g')
381387
add_assistant_response_to_chat_message "$chat_message" "$escaped_response_data"
382388

@@ -394,8 +400,18 @@ while $running; do
394400

395401
request_to_completions "$request_prompt"
396402
handle_error "$response"
397-
response_data=$(echo "$response" | jq -r '.choices[].text' | sed '1,2d; s/^A://g')
398-
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
403+
response_data=$(echo "$response" | jq -r '.choices[].text')
404+
405+
# if glow installed, print parsed markdown
406+
if command -v glow &>/dev/null; then
407+
formatted_text=$(echo "${response_data}" | glow)
408+
echo -e "${CHATGPT_CYAN_LABEL}"
409+
echo -e "${formatted_text}"
410+
else
411+
# else remove empty lines and print
412+
formatted_text=$(echo "${response_data}" | sed '1,2d; s/^A://g')
413+
echo -e "${CHATGPT_CYAN_LABEL}${formatted_text}" | fold -s -w $COLUMNS
414+
fi
399415

400416
if [ "$CONTEXT" = true ]; then
401417
escaped_response_data=$(echo "$response_data" | sed 's/"/\\"/g')

0 commit comments

Comments
 (0)