-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshellbot.sh
executable file
·51 lines (44 loc) · 1.36 KB
/
shellbot.sh
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
#!/bin/bash
# Set the static binary name
script_path="$(realpath "$0")"
script_dir="$(dirname "$script_path")"
binary_name="$script_dir/target/release/shellbot"
console_width=$(tput cols)
startfile=$(mktemp)
trap "rm -f $startfile" EXIT
transcript=$(mktemp)
trap "rm -f $transcript" EXIT
${EDITOR} "$startfile"
separator="─── ⋅ ∙ ∘ ༓ ∘ ⋅ ⋅ ───"
padding_width=$(( (console_width - ${#separator}) / 2 ))
padded_separator=$(printf "%*s%s" ${padding_width} '' "${separator}")
user_input=$(cat "$startfile")
echo -e "🧑 $USER"
echo "$user_input" | fold -s -w "$console_width"
while true; do
echo "===USER===" >> "$transcript"
echo "$user_input" >> "$transcript"
echo "${padded_separator}"
echo -e "🤖 shellbot"
echo "===ASSISTANT===" >> "$transcript"
response_file=$(mktemp)
# Use 'tee' to simultaneously capture the output and send it to 'fold'
"$binary_name" < "$transcript" | tee "$response_file" | fold -s -w "$console_width"
cat "$response_file" >> "$transcript"
rm "$response_file"
echo "${padded_separator}"
echo -e "🧑 $USER"
input=""
while true; do
read -r -e line
if [ -z "$line" ]; then
if [ -z "$input" ]; then
exit
else
break
fi
fi
input+="${line}"$'\n'
done
user_input=$input
done