-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.sh
More file actions
executable file
·116 lines (105 loc) · 2.22 KB
/
Copy pathnote.sh
File metadata and controls
executable file
·116 lines (105 loc) · 2.22 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
cd $(dirname $0)
BASEDIR=$(pwd)
cd - > /dev/null
. $BASEDIR/config
if [ ! -d "$REPO" ]; then
echo "error: invalid config" >&2
exit 1
fi
TAGDIR=$REPO/.tags
mkdir -p $TAGDIR
# Helpers
#
function usage()
{
cat<<EoT
usage: $(basename $0) <action>
Actions:
new [TAG]... Create a new note and associate it with tags
ls, list [TAG]... List all notes associated with the specified tags
cat <NOTE> Print note to standard output
edit <NOTE> Open note in default editor
tags [NOTE] List all tags sorted by occurrence, or, if a note is
specified, show all tags associated with the note
EoT
}
# Main
#
ACTION=$1
shift
case $ACTION in
"new")
NOTE=$(date +%Y%m%d%H%M%S)
$EDITOR $REPO/$NOTE
if [ "$?" -eq "0" ]; then
for ARG in $@
do
TAGS="--tag $ARG $TAGS"
done
$BASEDIR/note-tag.sh --repo $REPO --note $NOTE $TAGS
fi
;;
"ls" | "list")
TAG=$1
if [ -z "$TAG" ]; then
ls $REPO | sort
else
if [ -e "$TAGDIR/$TAG" ]; then
for NOTE in $(cat $TAGDIR/$TAG)
do
TAGC=0
for TAG in $@
do
if [ -e "$TAGDIR/$TAG" ]; then
TAGC=$(expr $TAGC + $(grep -c $NOTE $TAGDIR/$TAG))
else
echo "error: tag does not exist"
exit 1
fi
done
if [ "$TAGC" -eq "$#" ]; then
echo $NOTE
fi
done
else
echo "error: tag does not exist"
exit 1
fi
fi
;;
"cat" | "edit")
NOTE=$REPO/$1
if [ ! -e "$NOTE" ]; then
echo "error: note does not exist"
exit 1
fi
if [ "$ACTION" == "edit" ]; then
$EDITOR $NOTE
else
cat $NOTE
fi
;;
"tags")
NOTE=$1
if [ -z "$NOTE" ]; then
for TAG in $(wc -l $TAGDIR/* | sort -r | sed -e 's/ *//' | cut -d\ -f2 | sed -e '1d')
do
echo $(basename $TAG)
done
else
if [ ! -e "$REPO/$NOTE" ]; then
echo "error: note does not exist"
exit 1
fi
for TAG in $(grep -l $NOTE $TAGDIR/* | sort)
do
echo $(basename $TAG)
done
fi
;;
*)
usage >&2
exit 1
;;
esac