-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.sh
executable file
·98 lines (87 loc) · 1.25 KB
/
note.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
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
#!/usr/bin/bash
set -euo pipefail
if [[ ${NOTES_DIR:-x} == x ]]; then
NOTES_DIR="$HOME/documents/notes"
fi
if [[ ${VISUAL:-x} == x ]]; then
VISUAL="${EDITOR:-vi}"
fi
readonly argcount="$#"
check_args() {
if [[ $argcount -ne $1 ]]; then
usage_and_exit
fi
}
usage() {
echo "Usage:"
echo " ${0##*/} list"
echo " ${0##*/} edit <note>"
echo " ${0##*/} new <note>"
echo " ${0##*/} show|view <note>"
echo " ${0##*/} del|delete|rm|remove <note>..."
echo " ${0##*/} commit"
echo " ${0##*/} git <command>"
}
usage_and_exit() {
usage
exit 1
}
list_files() {
local files=()
for filename in *.md; do
files+=("${filename%.*}")
done
tr ' ' '\n' <<< "${files[@]}" | column -x
}
cd "$NOTES_DIR"
[[ $# -eq 0 ]] && usage_and_exit
case "$1" in
help)
usage
;;
list)
check_args 1
list_files
;;
edit)
check_args 2
"$VISUAL" "$2.md"
;;
new)
check_args 2
if [[ -f "$2.md" ]]; then
echo "Note '$2' already exists!"
exit 1
else
"$VISUAL" "$2"
fi
;;
show|view)
check_args 2
cat "$2.md"
;;
del|delete|rm|remove)
shift
rm "$@"
;;
commit)
git add .
git commit
;;
status)
git status
;;
push)
git push
;;
pull)
git pull
;;
git)
shift
git "$@"
;;
*)
usage_and_exit
;;
esac