Skip to content

Commit

Permalink
Merge pull request #81 from 0xacx/add-markdown-support
Browse files Browse the repository at this point in the history
Print with markdown if glow is installed
  • Loading branch information
0xacx committed Apr 8, 2023
2 parents 7627040 + 5fed80d commit 752d7b3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions chatgpt.sh
Expand Up @@ -10,7 +10,7 @@ COMMAND_GENERATION_PROMPT="Return a one-line bash command with the functionality

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

PROCESSING_LABEL="\n\033[90mprocessing... \033[0m"
PROCESSING_LABEL="\n\033[90mProcessing... \033[0m"

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

echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS

# if glow installed, print parsed markdown
if command -v glow &>/dev/null; then
formatted_text=$(echo "${response_data}" | glow)
echo -e "${CHATGPT_CYAN_LABEL}"
echo -e "${formatted_text}"
else
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
fi
escaped_response_data=$(echo "$response_data" | sed 's/"/\\"/g')
add_assistant_response_to_chat_message "$chat_message" "$escaped_response_data"

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

request_to_completions "$request_prompt"
handle_error "$response"
response_data=$(echo "$response" | jq -r '.choices[].text' | sed '1,2d; s/^A://g')
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
response_data=$(echo "$response" | jq -r '.choices[].text')

# if glow installed, print parsed markdown
if command -v glow &>/dev/null; then
formatted_text=$(echo "${response_data}" | glow)
echo -e "${CHATGPT_CYAN_LABEL}"
echo -e "${formatted_text}"
else
# else remove empty lines and print
formatted_text=$(echo "${response_data}" | sed '1,2d; s/^A://g')
echo -e "${CHATGPT_CYAN_LABEL}${formatted_text}" | fold -s -w $COLUMNS
fi

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

0 comments on commit 752d7b3

Please sign in to comment.