-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzsh-llm-suggestions-demo.zsh
84 lines (72 loc) · 2.45 KB
/
zsh-llm-suggestions-demo.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# To record a demo, it's helpful to not have the LLMs give random answers, so I
# saved some real answers in the demo folder. This function will cycle through
# them for a demo.
# To record:
# - ZSH_LLM_SUGGESTIONS_DEMO_NR=0
# - ctrl+l
# - start recording
# - type: who contributed the most to this git repository?
# - ctrl+o
# - ctrl+o
# - enter
# - ctrl+l
# - type: tell me a joke
# - ctrl+o
# - ctrl+u
# - type: delete the git submodule called modules/zsh-async
# - ctrl+o
# - ctrl+o
# - ctrl+u
# I use ScreenToGif to record and add subtitles.
# Subtitles:
# - Press ctrl+p to ask an LLM for a suggestion
# - The LLM result is shown directly in the prompt
# - Great, but what exactly does this command do? Press ctrl+shift+o to ask the LLM to explain.
# - Explanation appears above the prompt. Hit enter to run the command if it looks good.
# - Several LLMs are supported, e.g. ctrl+o for OpenAI's GPT4, ctrl+p for GitHub Copilot.
# - Sometimes, LLMs give bad suggestions. Press ctrl+p again to get a new suggestion for the same query. Or press ctrl+o to switch to a different LLM.
zsh_llm_suggestions_spinner() {
local delay=0.1
local spinstr='|/-\'
for ((i=0; i<20; i++)); do
local temp=${spinstr#?}
printf " [%c]" "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b"
done
printf " \b\b\b\b"
}
SCRIPT_DIR=$( cd -- "$( dirname -- "$0" )" &> /dev/null && pwd )
zsh_llm_suggestions_demo() {
ZSH_LLM_SUGGESTIONS_DEMO_NR=$((ZSH_LLM_SUGGESTIONS_DEMO_NR+1))
if [[ $ZSH_LLM_SUGGESTIONS_DEMO_NR -gt 5 ]]; then
ZSH_LLM_SUGGESTIONS_DEMO_NR=1
fi
suggestion=$(cat $SCRIPT_DIR/demo/git-commits-explain)
if [[ $ZSH_LLM_SUGGESTIONS_DEMO_NR -eq 1 ]]; then
suggestion=$(cat $SCRIPT_DIR/demo/git-commits)
fi
if [[ $ZSH_LLM_SUGGESTIONS_DEMO_NR -eq 2 ]]; then
suggestion=$(cat $SCRIPT_DIR/demo/git-commits-explain)
zsh_llm_suggestions_spinner
echo ""
eval "echo -e \"$suggestion\""
echo ""
zle reset-prompt
return
fi
if [[ $ZSH_LLM_SUGGESTIONS_DEMO_NR -eq 3 ]]; then
suggestion=$(cat $SCRIPT_DIR/demo/joke)
fi
if [[ $ZSH_LLM_SUGGESTIONS_DEMO_NR -eq 4 ]]; then
suggestion=$(cat $SCRIPT_DIR/demo/git-delete-submodule)
fi
if [[ $ZSH_LLM_SUGGESTIONS_DEMO_NR -eq 5 ]]; then
suggestion=$(cat $SCRIPT_DIR/demo/git-delete-submodule-2)
fi
zsh_llm_suggestions_spinner
BUFFER="$suggestion"
CURSOR=${#BUFFER}
}
zle -N zsh_llm_suggestions_demo