zn/zn

124 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
##############################################################################
#
# Basic structure derived from the note taking application named zetz;
# - https://gist.github.com/kutacoder/de2c0e624477efce920904125f0fb7b0
#
# Prerequisites:
# - tmux, fzf, bat, rg
#
# Note File Format
# - Notes should have a .md extention.
# - Notes are created with a date-time formatted filename, set to the creation date in the follwing format: %Y%m%d%H%M%S
# - Notes should have a title on the *first* line prefixed by one hash symbol. (title example: "# Cool Title")
# - Notes should have one or more tags, starting on line 3. The tags should be prefixed by a hash
# symbol without a space between it and the tag. Tags should also not have spaces in them. Use a
# dash or underscore instead. (tag example: "#Cool-Tag")
#
# Key Bindings:
# - ctrl-r: reload list
# - ctrl-x: delete note
# - ctrl-i: add new note
#
##############################################################################
#################################################
# default note path if not specified on the command line
NOTEPATH="$HOME/.notes/notes"
#################################################
# DOTAGS is false by default
DOTAGS="false"
#################################################
# Usage Function
usage () {
echo ""
echo "Usage: $0 [-p <path to note directory>] [-t]"
echo ""
echo "-p Path to desired note directory. Can be relative or absolute."
echo " ie, \"-p \$HOME/.notes/notes\""
echo ""
echo "-t Use tag search (default is to use title search)"
echo ""
}
#################################################
# Parse the CLA
parse_args() {
while getopts "p:t" opt; do
case $opt in
p)
NOTEPATH=${OPTARG}
;;
t)
DOTAGS="true"
;;
\?)
usage
exit 1
;;
esac
done
}
#################################################
main() {
parse_args "$@"
#############################################
# check if the supplied note path exists
if [ ! -d "$NOTEPATH" ];
then
echo ""
echo "Error: Directory \"${NOTEPATH}\" does not exist!"
usage
exit 1
fi
cd "$NOTEPATH"
if [ "$DOTAGS" == "false" ];
then
#########################################
# Run against titles
FZF_DEFAULT_COMMAND="zn-title-list.sh" \
fzf --no-sort --height=100% --preview-window=right:65%:wrap \
--preview 'bat --language=md --style=plain --color=always $(rg -slF {2..})' \
--bind 'enter:execute:$EDITOR $(rg -slF {2..})' \
--bind 'ctrl-r:reload:$SHELL -c "${FZF_DEFAULT_COMMAND}"' \
--bind 'ctrl-x:execute(rm -i $(rg -slF {2..}))+reload:$SHELL -c "${FZF_DEFAULT_COMMAND}"' \
--bind 'ctrl-i:execute:tmux split-window "FZF_DEFAULT_COMMAND=\"${FZF_DEFAULT_COMMAND}\" $EDITOR $(date +"%Y%m%d%H%M%S").md"'
else
#########################################
# Run against tags
FZF_DEFAULT_COMMAND="zn-tag-list.sh" \
fzf --no-sort --exact --height=100% --preview-window=right:65%:wrap \
--preview 'bat --language=md --style=plain --color=always $(rg -slF {3..})' \
--bind 'enter:execute:$EDITOR $(rg -slF {3..})' \
--bind 'ctrl-r:reload:$SHELL -c "${FZF_DEFAULT_COMMAND}"' \
--bind 'ctrl-x:execute(rm -i $(rg -slF {3..}))+reload:$SHELL -c "${FZF_DEFAULT_COMMAND}"' \
--bind 'ctrl-i:execute:tmux split-window "FZF_DEFAULT_COMMAND=\"${FZF_DEFAULT_COMMAND}\" $EDITOR $(date +"%Y%m%d%H%M%S").md"'
fi
#############################################
# check changes made into git
git add . && git commit -q -m "checking in changes made at $(date +%Y%m%d-%H%M%S)"
#cd -
}
#################################################
# This is the way...
main "$@"
# EOF