mirror of https://github.com/nirenjan/dotfiles.git
				
				
				
			
		
			
				
	
	
		
			331 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			331 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
| #!/bin/bash
 | |
| # Notes manager for the command line
 | |
| 
 | |
| NOTES_VER="0.1a"
 | |
| NOTES_DIR="$HOME/.notes"
 | |
| 
 | |
| # Initialize with the default editor
 | |
| [ -z "$EDITOR" ] && EDITOR="/usr/bin/vim"
 | |
| 
 | |
| dprint() {
 | |
|     if [ ! -z $NOTES_DEBUG ]; then
 | |
|         echo $*
 | |
|     fi
 | |
| }
 | |
| 
 | |
| notes_init() {
 | |
|     # Check if the notes folder exists, if not, create it
 | |
|     if [ ! -d $NOTES_DIR ]
 | |
|     then
 | |
|         echo -n "Creating notes folder..."
 | |
|         mkdir -p $NOTES_DIR
 | |
|         cd $NOTES_DIR
 | |
|         echo "done."
 | |
| 
 | |
|         git init .
 | |
|     else
 | |
|         echo "Notes has already been initialized."
 | |
|     fi
 | |
| }
 | |
| 
 | |
| make_title() {
 | |
|     echo "$*" | sed 's/[^A-Za-z0-9_]\+/-/g' | \
 | |
|     sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
 | |
| }
 | |
| 
 | |
| title_to_name() {
 | |
|     note_file=$NOTES_DIR/$(make_title $note_arg)
 | |
| }
 | |
| 
 | |
| check_arg() {
 | |
|     if [ -z "$note_arg" ]
 | |
|     then
 | |
|         echo "Must specify a note title!"
 | |
|         exit
 | |
|     fi
 | |
| }
 | |
| 
 | |
| note_exists() {
 | |
|     [[ -f "$note_file" ]]
 | |
| }
 | |
| 
 | |
| check_md5() {
 | |
|     if [[ "$note_old_md5" == "$note_new_md5" ]]
 | |
|     then
 | |
|         echo "No changes to '$note_arg'"
 | |
| 
 | |
|         if [[ ! -z "$1" ]]
 | |
|         then
 | |
|             rm -f $1
 | |
|         fi
 | |
| 
 | |
|         exit
 | |
|     fi
 | |
| }
 | |
| 
 | |
| check_empty() {
 | |
|     if [[ ! -s "$note_file" ]]
 | |
|     then
 | |
|         if [[ "$1" == "restore" ]]
 | |
|         then
 | |
|             cd "$NOTES_DIR"
 | |
|             gitfile=$(basename "$note_file")
 | |
|             git checkout $gitfile
 | |
|             echo "Restored empty note '$note_arg'"
 | |
|         elif [[ "$1" == "delete" ]]
 | |
|         then
 | |
|             rm -f $note_file
 | |
|             echo "Deleted empty note '$note_arg'"
 | |
|         fi
 | |
| 
 | |
|         exit
 | |
|     fi
 | |
| }
 | |
| 
 | |
| check_dir() {
 | |
|     if [[ ! -d "$NOTES_DIR" ]]
 | |
|     then
 | |
|         echo "Notes not initialized. Run init first!"
 | |
|         exit
 | |
|     fi
 | |
| }
 | |
| 
 | |
| md5_sum() {
 | |
|     openssl dgst -md5 $*
 | |
| }
 | |
| 
 | |
| help_cmd() {
 | |
|     echo '
 | |
| Notes Manager for the Command Line
 | |
| ==================================
 | |
| 
 | |
| This is a bash script that lets you take notes which are maintained in a
 | |
| Git repository. You can run this script by typing the following command
 | |
| at the command line:
 | |
| 
 | |
| Usage: note <command> <arguments>
 | |
| 
 | |
| Commands
 | |
| --------
 | |
| You can use the first letter of each command as a shortcut key.
 | |
| 
 | |
|     Command     Usage
 | |
|     -------     -----
 | |
|     new         Follow this with the note name to create a new note
 | |
|     edit        Follow this with the note name to edit an existing note
 | |
|     show        Follow this with the note name to display an existing note
 | |
|     delete      Follow this with the note name to delete a note
 | |
|     find        Prints the list of available notes
 | |
|     log         Displays the note history
 | |
|     grep        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
 | |
| '
 | |
| }
 | |
| 
 | |
| version_info() {
 | |
|     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=$(md5_sum $note_file)
 | |
| 
 | |
|     $EDITOR $note_file
 | |
| 
 | |
|     note_new_title=$(head -1 $note_file)
 | |
|     note_new_md5=$(md5_sum $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=$(md5_sum $note_file)
 | |
| 
 | |
|     $EDITOR $note_file
 | |
| 
 | |
|     note_new_title=$(head -1 $note_file)
 | |
|     note_new_md5=$(md5_sum $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_show() {
 | |
|     check_dir
 | |
|     check_arg
 | |
|     title_to_name
 | |
|     if ! note_exists
 | |
|     then
 | |
|         echo "Cannot find note '$note_arg'!"
 | |
|         exit
 | |
|     fi
 | |
| 
 | |
|     less -R $note_file
 | |
| }
 | |
| 
 | |
| 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() {
 | |
|     note_cmd=$1
 | |
|     shift
 | |
|     note_arg="$*"
 | |
| 
 | |
|     case "$note_cmd" in
 | |
|     "h" | "help")
 | |
|         help_cmd
 | |
|         ;;
 | |
| 
 | |
|     "n" | "new")
 | |
|         note_new
 | |
|         ;;
 | |
| 
 | |
|     "e" | "edit")
 | |
|         note_edit
 | |
|         ;;
 | |
| 
 | |
|     "s" | "show")
 | |
|         note_show
 | |
|         ;;
 | |
| 
 | |
|     "d" | "delete")
 | |
|         note_delete
 | |
|         ;;
 | |
| 
 | |
|     "l" | "log")
 | |
|         note_history
 | |
|         ;;
 | |
|     
 | |
|     "i" | "init")
 | |
|         notes_init
 | |
|         ;;
 | |
|     
 | |
|     "f" | "find")
 | |
|         note_list $1
 | |
|         ;;
 | |
|     
 | |
|     "g" | "grep")
 | |
|         note_search $1
 | |
|         ;;
 | |
| 
 | |
|     "v" | "version")
 | |
|         version_info
 | |
|         ;;
 | |
| 
 | |
|     *)
 | |
|         echo "Unrecognized command '$note_cmd'. Use help."
 | |
|         ;;
 | |
|     esac
 | |
| }
 | |
| 
 | |
| if [[ $# -lt 1 ]]
 | |
| then
 | |
|     echo "Usage: note <command> <arguments>"
 | |
|     echo "Type 'note help' for detailed help"
 | |
| else
 | |
|     parse_args $*
 | |
| fi
 |