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

248
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,6 +120,134 @@ version_info() {
echo "Notes Manager version $NOTES_VER" echo "Notes Manager version $NOTES_VER"
} }
note_new() {
check_dir
check_arg
title_to_name
if note_exists
then
echo "Note '$note_arg' already exists! Use edit instead."
exit
fi
echo "$note_arg" > $note_file
note_old_md5=$(md5sum $note_file)
$EDITOR $note_file
note_new_title=$(head -1 $note_file)
note_new_md5=$(md5sum $note_file)
note_new_file=$NOTES_DIR/$(make_title $note_new_title)
check_md5 $note_file
check_empty delete
dprint "Original filename = " $note_file
dprint "New filename = " $note_new_file
# Check for a title rename
if [[ "$note_file" != "$note_new_file" ]]
then
mv -f $note_file $note_new_file
fi
cd $NOTES_DIR
gitfile=$(basename $note_new_file)
git add $gitfile
git commit -m "Create note '$note_new_title'"
echo "Created note '$note_new_title'"
}
note_edit() {
check_dir
check_arg
title_to_name
if ! note_exists
then
echo "Cannot find note '$note_arg'!"
exit
fi
note_old_title=$(head -1 $note_file)
note_old_md5=$(md5sum $note_file)
$EDITOR $note_file
note_new_title=$(head -1 $note_file)
note_new_md5=$(md5sum $note_file)
check_md5
check_empty restore
cd $NOTES_DIR
gitfile=$(basename $note_file)
commit_msg="Update note '$note_new_title'"
if [[ "$note_old_title" != "$note_new_title" ]]
then
note_new_file=$(make_title $note_new_title)
git mv $gitfile $note_new_file
git add $note_new_file
commit_msg="$commit_msg. Rename from '$note_old_title'"
else
git add $gitfile
fi
git commit -m "$commit_msg"
echo "Updated note '$note_new_title'"
}
note_delete() {
check_dir
check_arg
title_to_name
if ! note_exists
then
echo "Cannot find note '$note_arg'!"
exit
fi
cd $NOTES_DIR
gitfile=$(basename $note_file)
git rm $gitfile
git commit -m "Delete note '$note_arg'"
echo "Deleted note '$note_arg'"
}
note_history() {
check_dir
cd $NOTES_DIR
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() { parse_args() {
note_cmd=$1 note_cmd=$1
shift shift
@ -113,113 +259,31 @@ parse_args() {
;; ;;
"new") "new")
check_dir note_new
check_arg
title_to_name
if note_exists
then
echo "Note '$note_arg' already exists! Use edit instead."
exit
fi
echo "$note_arg" > $note_file
note_old_md5=$(md5sum $note_file)
$EDITOR $note_file
note_new_title=$(head -1 $note_file)
note_new_md5=$(md5sum $note_file)
note_new_file=$NOTES_DIR/$(make_title $note_new_title)
check_md5 $note_file
dprint "Original filename = " $note_file
dprint "New filename = " $note_new_file
# Check for a title rename
if [[ "$note_file" != "$note_new_file" ]]
then
mv -f $note_file $note_new_file
fi
cd $NOTES_DIR
gitfile=$(basename $note_new_file)
git add $gitfile
git commit -m "Create note '$note_new_title'"
echo "Created note '$note_new_title'"
;; ;;
"edit") "edit")
check_dir note_edit
check_arg
title_to_name
if ! note_exists
then
echo "Cannot find note '$note_arg'!"
exit
fi
note_old_title=$(head -1 $note_file)
note_old_md5=$(md5sum $note_file)
$EDITOR $note_file
note_new_title=$(head -1 $note_file)
note_new_md5=$(md5sum $note_file)
check_md5
cd $NOTES_DIR
gitfile=$(basename $note_file)
commit_msg="Update note '$note_new_title'"
if [[ "$note_old_title" != "$note_new_title" ]]
then
note_new_file=$(make_title $note_new_title)
git mv $gitfile $note_new_file
git add $note_new_file
commit_msg="$commit_msg. Rename from '$note_old_title'"
else
git add $gitfile
fi
git commit -m "$commit_msg"
echo "Updated note '$note_new_title'"
;; ;;
"del" | "delete") "del" | "delete")
check_dir note_delete
check_arg
title_to_name
if ! note_exists
then
echo "Cannot find note '$note_arg'!"
exit
fi
cd $NOTES_DIR
gitfile=$(basename $note_file)
git rm $gitfile
git commit -m "Delete note '$note_arg'"
echo "Deleted note '$note_arg'"
;; ;;
"hist"|"history") "hist"|"history")
check_dir note_history
cd $NOTES_DIR
git log --pretty=format:'%ai - %s' -- $note_arg
;; ;;
"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")