Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A fish function for creating, editing, archiving, and searching for notes
function notes
argparse 'a/archive' 'e/editor=' 'h/help' 'i/interactive' 'o/open' 'p/path' 'rm' 's/suffix=' -- $argv
or return 1
set -q _flag_i _flag_o _flag_p
if [ $status -lt 3 ]
if [ (count $argv) -gt 0 ]
echo "Must not provide an argument to the --interactive, --open, or --path flags"
return 1
end
end
if set -q _flag_h
echo "USAGE"
echo " notes [-h/--help] \ "
echo " [-a/--archive] \ "
echo " [-e/--editor <editor>] \ "
echo " [-i/--interactive] \ "
echo " [-o/--open] \ "
echo " [-p/--path ] \ "
echo " [--rm] \ "
echo " [<name>]"
echo ""
echo "ABOUT"
echo " Open a note named <name> for editing in \$EDITOR, where <name> will have spaces converted to underscores."
echo " If there's no '.' in name, a '.md' suffix will be added. Notes are created in \$NOTES_DIRECTORY, if it's"
echo " set, and \$HOME/Documents/notes if it isn't. When a note is opened, it's searched for \$NOTES_ENTRY_HEADER,"
echo " which indicates where note-taking started on a particular day. (Default = \"\#\# %Y-%m-%d dawns!\") If no"
echo " header exists, it's inserted at the end of the note."
echo " If you're using vi as your editor, notes will jump you to the end of the file when it is opened."
echo ""
echo "OPTIONS"
echo " -a/--archive : Before editing, copy the text of the current note (even if empty) to an archival note"
echo " with a _%Y%m%d%H%M suffix."
echo " -e/--editor : Editor to use. Defaults to \$EDITOR, and if that isn't set then tries vi."
echo " -i/--interactive : Search interactively for an existing note to open. (Requires fzf to be installed)"
echo " -o/--open : Open the notes directory in the GUI."
echo " -p/--path : Prints the path to the notes folder, as specified by the NOTES_DIRECTORY variable."
echo " (Default = HOME/Documents/notes)"
echo " --rm : Remove specified note. (Use in combination with -a/--archive to archive and remove)"
echo ""
return 0
end
if ! set -q NOTES_DIRECTORY
set NOTES_DIRECTORY "$HOME/Documents/notes"
end
set -l archive_dir $NOTES_DIRECTORY/archive
if ! set -q NOTES_ENTRY_HEADER
set NOTES_ENTRY_HEADER "## %Y-%m-%d dawns!"
end
set -l dateline (date +$NOTES_ENTRY_HEADER)
if set -q _flag_p
echo $NOTES_DIRECTORY
return 0
end
if set -q _flag_i
if ! type -q fzf
echo "fzf must be installed to use --interactive, so exiting"
return 1
end
end
if ! set -q _flag_s
set _flag_s ".md"
end
set -l name (string join "_" $argv)
if [ (string length "$name") -lt 1 ]
set name "scratch"
end
if ! string match -r -q '\.' $name
set name $name.md
end
if ! set -q _flag_e
set _flag_e $EDITOR
end
if [ (string length "$_flag_e") -lt 1 ]
set _flag_e vim
end
if [ $_flag_e = vim ]
set _flag_e vim "+normal G\$"
end
set -l file_path "$NOTES_DIRECTORY/$name"
mkdir -p $NOTES_DIRECTORY
if set -q _flag_o
open $NOTES_DIRECTORY
return 0
end
if set -q _flag_i
set -l curdir (pwd)
cd $NOTES_DIRECTORY
set file_name (fzf)
cd $curdir
if [ (string length "$file_name") -lt 1 ]
echo "Exiting since we couldn't find a matching note"
return 1
else
set file_path $NOTES_DIRECTORY/$file_name
end
end
if [ ! -f $file_path ]
touch $file_path
end
if set -q _flag_a
set -l grps (string match -r -g '(.+)(\.[^.]*$)' $name)
if [ (count $grps) -ne 2 ]
echo "Failed to split file name correctly, exiting"
echo $grps
return 1
end
set -l archive_path $archive_dir/$grps[1](date +_%Y%m%d%H%M%S)$grps[2]
mkdir -p $archive_dir
cp -v $file_path $archive_path
end
if set -q _flag_rm
rm $file_path
set -l rm_status $status
if [ $rm_status -ne 0 ]
echo "rm $file_path failed"
else
echo "rm $file_path"
end
return $rm_status
end
grep --text --quiet $dateline $file_path
if [ $status -ne 0 ]
if [ (du -s $file_path | cut -f 1) -gt 0 ]
printf "\n\n" >> $file_path
end
printf "%s\n\n" $dateline >> $file_path
end
$_flag_e $file_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment