mirror of https://github.com/nirenjan/dotfiles.git
Add search functionality to note
Also tweak new and edit commands to delete/restore note if the file is empty.master
parent
0ecbe0605d
commit
8d0349f999
108
note
108
note
|
@ -63,6 +63,22 @@ check_md5() {
|
|||
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() {
|
||||
if [[ ! -d "$NOTES_DIR" ]]
|
||||
then
|
||||
|
@ -92,6 +108,8 @@ Commands
|
|||
delete Follow this with the note name to delete a note
|
||||
list Prints the list of available notes
|
||||
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
|
||||
help Prints this help message
|
||||
version Prints the version information
|
||||
|
@ -102,17 +120,7 @@ version_info() {
|
|||
echo "Notes Manager version $NOTES_VER"
|
||||
}
|
||||
|
||||
parse_args() {
|
||||
note_cmd=$1
|
||||
shift
|
||||
note_arg="$*"
|
||||
|
||||
case "$note_cmd" in
|
||||
"help")
|
||||
help_cmd
|
||||
;;
|
||||
|
||||
"new")
|
||||
note_new() {
|
||||
check_dir
|
||||
check_arg
|
||||
title_to_name
|
||||
|
@ -132,6 +140,7 @@ parse_args() {
|
|||
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
|
||||
|
@ -148,10 +157,9 @@ parse_args() {
|
|||
git add $gitfile
|
||||
git commit -m "Create note '$note_new_title'"
|
||||
echo "Created note '$note_new_title'"
|
||||
}
|
||||
|
||||
;;
|
||||
|
||||
"edit")
|
||||
note_edit() {
|
||||
check_dir
|
||||
check_arg
|
||||
title_to_name
|
||||
|
@ -170,6 +178,7 @@ parse_args() {
|
|||
note_new_md5=$(md5sum $note_file)
|
||||
|
||||
check_md5
|
||||
check_empty restore
|
||||
|
||||
cd $NOTES_DIR
|
||||
gitfile=$(basename $note_file)
|
||||
|
@ -187,9 +196,9 @@ parse_args() {
|
|||
|
||||
git commit -m "$commit_msg"
|
||||
echo "Updated note '$note_new_title'"
|
||||
;;
|
||||
}
|
||||
|
||||
"del" | "delete")
|
||||
note_delete() {
|
||||
check_dir
|
||||
check_arg
|
||||
title_to_name
|
||||
|
@ -204,22 +213,77 @@ parse_args() {
|
|||
git rm $gitfile
|
||||
git commit -m "Delete note '$note_arg'"
|
||||
echo "Deleted note '$note_arg'"
|
||||
;;
|
||||
}
|
||||
|
||||
"hist"|"history")
|
||||
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() {
|
||||
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")
|
||||
notes_init
|
||||
;;
|
||||
|
||||
"list")
|
||||
check_dir
|
||||
cd $NOTES_DIR
|
||||
ls -1 $note_arg
|
||||
"list" | "find")
|
||||
note_list $1
|
||||
;;
|
||||
|
||||
"search")
|
||||
note_search $1
|
||||
;;
|
||||
|
||||
"ver" | "version")
|
||||
|
|
Loading…
Reference in New Issue