-
Notifications
You must be signed in to change notification settings - Fork 2
/
code
executable file
·78 lines (59 loc) · 1.88 KB
/
code
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
#!/usr/bin/env bash
set -eu -o pipefail
if ! command gpt --help &> /dev/null; then
echo >&2 "Error: gpt is not installed or is not on your PATH"
echo >&2 " - See https://github.com/sysread/bash-gpt for installation instructions"
exit 1
fi
usage() {
local exit_code="${1:-0}"
cat << 'EOF'
usage: code --language <language> --code <code> --prompt <prompt>
-h, --help Display this help message
-p, --prompt <prompt> The instruction to code
-l, --language <language> Optional; the programming language to code in
-c, --code <code> Optional; the code to start with
EOF
exit "$exit_code"
}
PROMPT=""
ARGS=()
while (("$#")); do
case "$1" in
--help | -h)
usage 0
;;
--language | --lang | -l)
ARGS+=(-u "programming language: $2")
shift 2
;;
--code | -c)
ARGS+=(-u "starting code: $2")
shift 2
;;
--prompt | -p)
shift
PROMPT="$*"
break
;;
*)
echo >&2 "error: unrecognized option: $1"
usage 1
;;
esac
done
if [ -z "$PROMPT" ]; then
echo "error: a prompt is required"
usage 1
fi
#who='you are an expert programming assistant'
who="you generate programming code completions"
what='respond ONLY in code and code comments, NO explanation'
# xargs: remove leading and trailing whitespace
# sed: remove ANSI escape sequences
response=$(gpt -s "$who" -u "$what" "${ARGS[@]}" -u "$PROMPT" | gum format)
# shellcheck disable=SC2001
stripped=$(echo "$response" | sed 's/\x1b\[[0-9;]*m//g') # remove ANSI escape sequences
stripped=$(echo -e "$stripped" | awk 'BEGIN {RS = ""; ORS = ""} { gsub(/^[\n\t ]*/, "", $0); print $0 }') # remove leading whitespace
stripped=$(echo -e "$stripped" | awk 'BEGIN {RS = ""; ORS = ""} { gsub(/[\n\t ]*$/, "", $0); print $0 }') # remove trailing whitespace
echo "$stripped"