-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.bash
executable file
·201 lines (163 loc) · 4.43 KB
/
notes.bash
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#! /usr/bin/env bash
# DEFAULT_ROOT_CHANGE_ME #
NOTES_ROOT=${DEFAULT_ROOT:-"$HOME/.notes"}
DIGIT_REGEX="^[0-9]+$"
HELP_TEXT=$(cat <<-EOM
Prompt: notes
Last: notes last|l
Cat: notes cat|c
EOM)
function notes_default(){
last_note=$(cat $NOTES_ROOT/.last_note 2>/dev/null)
if [[ -z $last_note ]] || [[ ! -f $NOTES_ROOT/$last_note ]]; then
echo "notes.txt"
else
echo $last_note
fi
}
function notes_prompt() {
action=$1
target=$2
clear
until [[ $action == "q" ]]; do
index=0
for fname in $NOTES_ROOT/*.txt; do
let index+=1
file_name=$(basename $fname)
file_name=${file_name%.*}
if [[ $action == "search" ]]; then
regex=.*$(echo $target | fold -w1 | tr '\n' ' ' | sed 's| |.*|g')
if [[ $file_name =~ $regex ]]; then
echo -e "$index. $file_name"
fi
else
echo -e "$index. $file_name"
fi
done
echo ""
echo "To quit: q"
echo "To edit: <number>"
echo "To create: <name>"
echo "To search: s <name> | hit <enter> to reset"
echo "To remove: rm <number|name>"
echo "To rename: mv <number|name> <new name>"
echo ""
read ANSWER
action=$(echo $ANSWER | cut -d " " -f 1)
target=$(echo $ANSWER | cut -d " " -f 2)
new_name=$(echo $ANSWER | cut -d " " -f 3)
# edit a note
if [[ $action =~ $DIGIT_REGEX ]]; then
notes_view "edit" $action
# Rename a note
elif [[ $action == "mv" ]]; then
new_name=$3
notes_rm_or_mv "mv" $target $new_name
# Remove a note
elif [[ $action == "rm" ]]; then
notes_rm_or_mv "rm" $target
# Search for a note
elif [[ $action == "s" ]]; then
notes_prompt "search" $target
# create a note
elif [[ $action != "q" && ! -z $action ]]; then
notes_view "edit" $target
# reset
elif [[ -z $action ]]; then
notes_prompt
fi
clear
done
notes-sync
}
function notes_rm_or_mv(){
action=$1
target=$2
new_name=$3
index=0
if [[ ! -z $target ]]; then
if [[ $target =~ $DIGIT_REGEX ]]; then
for fname in $NOTES_ROOT/*.txt; do
let index+=1
if [[ $target == $index ]]; then
target=$fname
fi
done
else
target=$NOTES_ROOT/$target.txt
fi
if [[ $action == "rm" ]]; then
echo "Remove this note? $target: [y]"
read ANSWER
if [[ $ANSWER == "y" ]]; then
rm $target
fi
elif [[ $action == "mv" ]]; then
mv $target $NOTES_ROOT/$new_name.txt
fi
else
echo "Usage: notes rm <note name>"
exit 1
fi
notes-sync
}
function notes_view(){
action=$1
target=$2
index=0
if [[ $target =~ $DIGIT_REGEX ]]; then
for fname in $NOTES_ROOT/*.txt; do
let index+=1
if [[ $target == $index ]]; then
target=$(basename $fname)
fi
done
elif [[ ! -z $target ]]; then
target=$target.txt
else
target=$(notes_default)
fi
if [[ $action == "view" ]]; then
cat $NOTES_ROOT/$target
else
vim $NOTES_ROOT/$target || vi $NOTES_ROOT/$target
fi
echo $target > $NOTES_ROOT/.last_note
}
function notes_help(){
echo "$HELP_TEXT" | less
}
function notes_cat() {
for fname in $NOTES_ROOT/*.txt; do
let index+=1
file_name=$(basename $fname)
file_name=${file_name%.*}
contents+="==========\n"
contents+="$index. $file_name"
contents+="\n==========\n\n"
contents+=`cat $fname`
contents+="\n\n"
done
echo -e "$contents" | less
}
function main(){
notes-sync
action=$1
target=$2
# Last notes
if [[ $action == "last" ]] || [[ $action == "l" ]]; then
notes_view
# Cat notes
elif [[ $action == "cat" ]] || [[ $action == "c" ]]; then
notes_cat
# Help
elif [[ $action == "help" ]] || [[ $action == "h" ]]; then
notes_help
# View a note
elif [[ $action == "view" ]] || [[ $action == "v" ]]; then
notes_view "view" $target
else
notes_prompt
fi
}
main $1 $2 $3