Skip to content

Instantly share code, notes, and snippets.

@benyanke
Last active August 10, 2017 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benyanke/f858ef3962951d39348095946ce036fb to your computer and use it in GitHub Desktop.
Save benyanke/f858ef3962951d39348095946ce036fb to your computer and use it in GitHub Desktop.
Note-taking bash script for meetings - add to .bashrc
#!/bin/bash
# Meeting note bash functions - include in your .bashrc file.
# Commands:
# note - Lists all past notes and their IDs.
# note help - This.
# note cd- Change your working directory to the notes storage folder
# note uncd - Change your working directory to the last working directory (useful for getting out of the notes storage folder)
# note new - Create a new note document. Will request user info then open it.
# note [INT] - Open a past note by it's integer ID.
# Current note - can't have an underscore in the full path or it will break date-based sorting.
# TODO: output only last X notes except when -a/--all flag is set
# TODO: support default editor, instead of just nano
# Note: the IDs are not permanent, they can change whenever a new note doc is created.
# Change me to your default note path
defaultnotespath="$HOME/nextcloud/meeting-notes"
nicedefaultnotespath="~${defaultnotespath#$HOME}"
# Used internally by note() to make a new note file.
function notemake() {
echo "";
echo "Beginning new meeting note file"
# TODO: Specify another date
DATE=`date +%m-%d-%Y`
defaultpath="$defaultnotespath"
echo "Enter a meeting topic:"
read topic;
echo "";
# Spaces to dashes
topicslug=${topic// /-}
# Lowercase
topicslug=`echo $topicslug | tr '[:upper:]' '[:lower:]'`
filename="${topicslug}_${DATE}.md"
filepath=${defaultpath}/${filename}
# Check if file already exists
if [ ! -f $filepath ]; then
touch $defaultpath/$filename;
echo "# $topic" > ${filepath}
echo "## $DATE" >> ${filepath}
echo "" >> ${filepath}
echo "" >> ${filepath}
echo "Opening ${filename}";
# TODO: Wait for input to open nano, ask user
nano +4 ${defaultpath}/${filename};
else
echo "Be aware, this notepad already exists.";
echo "Opening ${filename}";
sleep 1;
# TODO: Wait for input to open nano, ask user
nano +10000 ${defaultpath}/${filename};
fi
echo "";
echo "Notefile stored at ${defaultpath}/${filename}";
cd ${defaultpath}
}
# Used internally, don't call directly
# This is used to ensure all functions sort the files in the same way
function getnotefiles() {
# Sorts files by date in name field
f=2;
ls $defaultnotespath/*.md | sort --numeric-sort --field-separator="_" --key=$f.7,$f.10 --key=$f.1,$f.3 --key=$f.4,$f.6
}
# Used internally, by note()
function notelist() {
# echo "Listing current meeting notes";
i=0
echo "$(
echo "::";
echo "ID:Name:Date"
echo "::";
files="`getnotefiles`";
for f in $files; do
file=`basename $f`
topicslug=`echo $file | awk -F "_" '{print $1}'`
date=`echo $file | awk -F '[_.]' '{print $2}'`
topic=${topicslug//-/ }
topic="`echo $topic | sed 's/[^ ]\+/\L\u&/g'`";
id=`printf "%02d\n" $i`
echo "$id:$date:$topic";
i=$((i+1))
done
)" | column -t -s ":" | sed -e 's/(null)//g'
}
function is_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1); }
function note() {
if [[ $1 = "new" ]] ; then
notemake;
elif [[ $1 = "help" || $1 = "man" || $1 = "?" || $1 = "-?" ]]; then
echo "";
echo "Notetaker Script";
echo "";
echo "Supported commands:";
echo "";
echo " note";
echo " Lists all past notes and their IDs.";
echo "";
echo " note help";
echo " This.";
echo "";
echo " note cd";
echo " Change your working directory to the notes storage folder";
echo "";
echo " note uncd";
echo " Change your working directory to the last working directory (useful for getting out of the notes storage folder)";
echo "";
echo " note new";
echo " Create a new note document. Will request user info then open it.";
echo "";
echo " note [INT]";
echo " Open a past note by it's integer ID.";
echo "";
return 0;
elif [[ $1 = "cd" ]] ; then
echo "";
echo "Changing to notes directory: $nicedefaultnotespath/"
echo "";
cd $defaultnotespath/;
elif [[ $1 = "uncd" ]] ; then
echo "";
echo "Changing to previous directory: $OLDPWD"
echo "";
cd $OLDPWD;
elif [[ $1 = "" ]] ; then
echo "No note specified. Select one from the list by it's ID";
notelist
elif $(is_int "${1}"); then
# Get ID from args
id=$1;
# Fix off-by-one issue with id
adjust=1
fileCount=$(expr $id + $adjust)
files="`getnotefiles`";
# Select note by id
fileToOpen="`echo $files | awk -v notefile=$fileCount '{ print $notefile }'`"
if [[ $fileToOpen = "" ]]; then
echo "Note ID $id does not exist";
return 1;
fi
# Check if note file exists or not
if [[ -f $fileToOpen ]]; then
echo "Opening note ID $id";
nano +100000 $fileToOpen;
echo "Finished with `basename $fileToOpen`"
echo "";
return 0;
else
echo "Note ID $id does not exist."
return 1;
fi
else
echo "";
echo "Command not recognized";
echo "";
return 1;
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment