Add search functionality to note

Also tweak new and edit commands to delete/restore note if the file
is empty.
master
nirenjan 2013-01-30 19:25:32 -08:00
parent 0ecbe0605d
commit 8d0349f999
1 changed files with 156 additions and 92 deletions

108
note
View File

@ -63,6 +63,22 @@ check_md5() {
fi fi
} }
check_empty() {
if [[ ! -s "$note_file" ]]
then
if [[ "$1" == "restore" ]]
then
gitfile=$(basename "$note_file")
git checkout $gitfile
elif [[ "$1" == "delete" ]]
then
rm -f $note_file
fi
exit
fi
}
check_dir() { check_dir() {
if [[ ! -d "$NOTES_DIR" ]] if [[ ! -d "$NOTES_DIR" ]]
then then
@ -92,6 +108,8 @@ Commands
delete Follow this with the note name to delete a note delete Follow this with the note name to delete a note
list Prints the list of available notes list Prints the list of available notes
history Displays the note history history Displays the note history
find Finds all notes with the specified keyword in the filename
search Finds all notes with the specified keyword in the text
init Run this the very first time to set up the folders init Run this the very first time to set up the folders
help Prints this help message help Prints this help message
version Prints the version information version Prints the version information
@ -102,17 +120,7 @@ version_info() {
echo "Notes Manager version $NOTES_VER" echo "Notes Manager version $NOTES_VER"
} }
parse_args() { note_new() {
note_cmd=$1
shift
note_arg="$*"
case "$note_cmd" in
"help")
help_cmd
;;
"new")
check_dir check_dir
check_arg check_arg
title_to_name title_to_name
@ -132,6 +140,7 @@ parse_args() {
note_new_md5=$(md5sum $note_file) note_new_md5=$(md5sum $note_file)
note_new_file=$NOTES_DIR/$(make_title $note_new_title) note_new_file=$NOTES_DIR/$(make_title $note_new_title)
check_md5 $note_file check_md5 $note_file
check_empty delete
dprint "Original filename = " $note_file dprint "Original filename = " $note_file
dprint "New filename = " $note_new_file dprint "New filename = " $note_new_file
@ -148,10 +157,9 @@ parse_args() {
git add $gitfile git add $gitfile
git commit -m "Create note '$note_new_title'" git commit -m "Create note '$note_new_title'"
echo "Created note '$note_new_title'" echo "Created note '$note_new_title'"
}
;; note_edit() {
"edit")
check_dir check_dir
check_arg check_arg
title_to_name title_to_name
@ -170,6 +178,7 @@ parse_args() {
note_new_md5=$(md5sum $note_file) note_new_md5=$(md5sum $note_file)
check_md5 check_md5
check_empty restore
cd $NOTES_DIR cd $NOTES_DIR
gitfile=$(basename $note_file) gitfile=$(basename $note_file)
@ -187,9 +196,9 @@ parse_args() {
git commit -m "$commit_msg" git commit -m "$commit_msg"
echo "Updated note '$note_new_title'" echo "Updated note '$note_new_title'"
;; }
"del" | "delete") note_delete() {
check_dir check_dir
check_arg check_arg
title_to_name title_to_name
@ -204,22 +213,77 @@ parse_args() {
git rm $gitfile git rm $gitfile
git commit -m "Delete note '$note_arg'" git commit -m "Delete note '$note_arg'"
echo "Deleted note '$note_arg'" echo "Deleted note '$note_arg'"
;; }
"hist"|"history") note_history() {
check_dir check_dir
cd $NOTES_DIR cd $NOTES_DIR
git log --pretty=format:'%ai - %s' -- $note_arg git log --pretty=format:'%ai - %s' -- $note_arg
}
note_list() {
check_dir
cd $NOTES_DIR
if [[ -z "$1" ]]
then
ls -1
else
ls -1 | grep -i $1
fi
}
note_search() {
check_dir
if [[ -z "$1" ]]; then
echo "Must specify a pattern to search titles!"
exit
fi
cd $NOTES_DIR
for file in *
do
grep --color=always -il "\<$1\>" $file
grep --color=always -inhT -m2 -C1 "\<$1\>" $file
done
}
parse_args() {
note_cmd=$1
shift
note_arg="$*"
case "$note_cmd" in
"help")
help_cmd
;;
"new")
note_new
;;
"edit")
note_edit
;;
"del" | "delete")
note_delete
;;
"hist"|"history")
note_history
;; ;;
"init") "init")
notes_init notes_init
;; ;;
"list") "list" | "find")
check_dir note_list $1
cd $NOTES_DIR ;;
ls -1 $note_arg
"search")
note_search $1
;; ;;
"ver" | "version") "ver" | "version")