Skip to content

Commit

Permalink
Spinner in zsh script
Browse files Browse the repository at this point in the history
  • Loading branch information
CoMfUcIoS committed Sep 25, 2023
1 parent 91b143a commit 15121b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pull:
test:
curl $(LOCALAI)/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "gpt4all-j", "messages": [{"role": "user", "content": "How are you?"}],"temperature": 0.1 }'

SILENT:
.SILENT:
get_models:
curl $(LOCALAI)/models/apply -H "Content-Type: application/json" -d '{"url": "github:go-skynet/model-gallery/gpt4all-j.yaml","name": "gpt4all-j"}'
rm -rf models/*.tmpl
11 changes: 0 additions & 11 deletions zsh-local-ai/create_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import subprocess
import sys
import time


ai_url = 'http://localhost:8085/v1'
Expand Down Expand Up @@ -39,18 +38,8 @@
shell=True,
)

spinner = ['-', '/', '|', '\\']
i = 0
while process.poll() is None:
sys.stdout.write('\r' + spinner[i % len(spinner)])
i += 1
time.sleep(0.1)


stdout, stderr = process.communicate()

sys.stdout.write('\r')

# print the output
if process.returncode == 0:
completed_command = eval(stdout)['choices'][0]['message']['content']
Expand Down
18 changes: 16 additions & 2 deletions zsh-local-ai/zsh-local-ai.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@

create_completion() {
text=${BUFFER}
completion=$(echo -n "$text" | $ZSH_CUSTOM/plugins/zsh-local-ai/create_completion.py $CURSOR)
text_before_cursor=${text:0:$CURSOR}
text_after_cursor=${text:$CURSOR}
spinner="/-\|"
i=0
set +m
echo -ne "|"
while [ true ]
do
i=$(( (i+1) %4 ))
echo -ne "\b${spinner:$i:1}"
sleep .1
done &
spinner_pid=$!
completion=$(echo -n "$text" | $ZSH_CUSTOM/plugins/zsh-local-ai/create_completion.py $CURSOR)
kill $spinner_pid
wait $spinner_pid 2>/dev/null
echo -ne "\b"
BUFFER="${text_before_cursor}${completion}${text_after_cursor}"
prefix_and_completion="${text_before_cursor}${completion}"
CURSOR=${#prefix_and_completion}
}

zle -N create_completion
zle -N create_completion

0 comments on commit 15121b9

Please sign in to comment.