diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-04-01 18:16:56 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-04-01 18:16:57 +0800 |
| commit | 34fe5f12af224bc7455b3a00276e0575141cd111 (patch) | |
| tree | 2c1dc88cff13a3944501f42b6a6182ee5bd86509 /bin | |
| parent | 4f4f292f16855f1e8b4bb068085b02e3f56492e8 (diff) | |
Use coproc to print progress indicatorcoproc
Down side: steam mode doesn't work
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/gpt/gpt | 21 |
1 files 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 | |||
| 333 | # Append request body into cache | 333 | # Append request body into cache |
| 334 | echo "$body" >>"$cache" | 334 | echo "$body" >>"$cache" |
| 335 | 335 | ||
| 336 | # Use Yellow color to print completion from GPT | 336 | # Run API call in coprocess |
| 337 | echo -en '\e[33m' | 337 | coproc API { |
| 338 | _API_call | _process_completion | 338 | _API_call | _process_completion |
| 339 | echo -e '\e[0m\n' | 339 | } |
| 340 | 340 | ||
| 341 | # While API call does not end, print indicator onto terminal | ||
| 342 | while ps ${API_PID} &>/dev/null; do | ||
| 343 | printf '.' | ||
| 344 | sleep 1 | ||
| 345 | done & | ||
| 346 | |||
| 347 | # Read response of API call from coprocess | ||
| 348 | response=$( cat <&"${API[0]}" ) | ||
| 349 | |||
| 350 | # Use Yellow color to print response | ||
| 351 | echo -e "\r\e[K\e[33m$response\e[0m\n" | ||
| 352 | |||
| 353 | # Print how many tokens used (if VERBOSE is set) | ||
| 341 | if [ "$VERBOSE" = true ]; then | 354 | if [ "$VERBOSE" = true ]; then |
| 342 | tokens=$(jq -s '.[-1].usage? // empty | .total_tokens' "$cache") | 355 | tokens=$(jq -s '.[-1].usage? // empty | .total_tokens' "$cache") |
| 343 | echo -e "$tokens token used.\n" | 356 | echo -e "$tokens token used.\n" |