-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovim
executable file
·104 lines (81 loc) · 1.51 KB
/
govim
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
shopt -s extglob;
_isFlag() {
case "${1}" in
*(-)[${2}${2^^}] )
return 0;
;;
* )
return 1;
;;
esac;
}
_clear() {
[ -e "${TMP}" ] &&
rm "${TMP}";
cat << EOF >> "${TMP}"
func main() {
}
EOF
}
_write() {
[ -e "${FILE}" ] && rm "${FILE}";
cat << EOF >> "${FILE}"
package main
import (
"fmt"
"bytes"
)
var (
buffer bytes.Buffer
)
// Pass to fmt.Println().
func print(a ...interface{}) {
fmt.Println(a...)
}
// Pass to fmt.Printf().
func printf(format string, a ...interface{}) {
fmt.Printf(format, a...)
}
// Concatenate strings together into one string.
func concat(slc ...string) string {
b := bytes.NewBuffer(nil)
defer b.Reset()
for _, s := range slc {
b.WriteString(s)
}
return b.String()
}
// Convert a string into a slice.
func slc(args ...string) []string {
return args
}
EOF
cat "${TMP}" >> "${FILE}";
}
_run() {
"${VIM}" -n -X -c :wq "${FILE}";
go run "${FILE}";
}
_edit() {
"${VIM}" -c 'let g:go_fmt_command="gofmt" | let g:go_fmt_fail_silently=1 | star' +2 "${TMP}";
_write;
}
declare -r VIM="vim";
#declare -r VIM="nvim";
declare -r FILE="govim.go";
declare -r TMP=".govimtmp.go";
pushd "${GOPATH}" &>/dev/null;
[ -n "${1}" ] ||
_clear;
if _isFlag "${1}" "s"; then
"${VIM}" "${FILE}";
exit $?;
fi;
declare OLD="$(cat "${TMP}")";
_edit;
declare NEW="$(cat "${TMP}")";
[[ "${OLD}" != "${NEW}" || -n "${1}" ]] ||
exit $?;
_run;
popd &>/dev/null;