From 34fe5f12af224bc7455b3a00276e0575141cd111 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sat, 1 Apr 2023 18:16:56 +0800 Subject: Use coproc to print progress indicator Down side: steam mode doesn't work --- bin/gpt/gpt | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/bin/gpt/gpt b/bin/gpt/gpt index aa770bb..374a707 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt @@ -333,11 +333,24 @@ EOF # Append request body into cache echo "$body" >>"$cache" - # Use Yellow color to print completion from GPT - echo -en '\e[33m' - _API_call | _process_completion - echo -e '\e[0m\n' + # Run API call in coprocess + coproc API { + _API_call | _process_completion + } + # While API call does not end, print indicator onto terminal + while ps ${API_PID} &>/dev/null; do + printf '.' + sleep 1 + done & + + # Read response of API call from coprocess + response=$( cat <&"${API[0]}" ) + + # Use Yellow color to print response + echo -e "\r\e[K\e[33m$response\e[0m\n" + + # Print how many tokens used (if VERBOSE is set) if [ "$VERBOSE" = true ]; then tokens=$(jq -s '.[-1].usage? // empty | .total_tokens' "$cache") echo -e "$tokens token used.\n" -- cgit v1.2.3-70-g09d2