#!/bin/bash # memos # written 2020 by konomo # all rights reversed; copy what you like; community over copyright # define memo array memos=("") # define memo name array memonames=("") # get the arguments args=("$@") # memo directory memodir=$HOME/.memos # define the length at which a line is wrapped wraplength=32 readmemos() { # check for empty memo directory if [ -z "$(ls -A ${memodir}/)" ]; then echo -e "there are no memos.\ncreate one by passing the 'new' argument\n-~-~-MEMOS-~-~-" exit 0 fi for m in $memodir/* do # wish i could just use memos+= to be honest, but # for some reason i can't. memos[${#memos[@]}]=$(cat $m) memonames[${#memonames[@]}]=$(echo "${m}" | tr -d '\n') done } printmemos() { count=0 for i in "${memos[@]}" do echo -n "${i}" | fold -w "${wraplength}" if [ -n "${memonames[${count}]}" ]; then echo -e " -> ${memonames[${count}]}" fi ((count++)) done } newmemo() { echo "please enter the filename (will write to ${memodir})" read memopath if [ -f "${memodir}/${memopath}" ]; then echo "this memo already exists" newmemo fi # create file touch "${memodir}/${memopath}" echo -e "\nplease enter the memo now (will be single lined, but wrapped at ${wraplength} characters)\nbe sure to choose a fitting filename" read memoline # write to file without newline echo -n "${memoline}" > "${memodir}/${memopath}" # memos are private! chmod 400 "${memodir}/${memopath}" exit } rmmemo() { if [ -z "$(ls -A ${memodir}/)" ]; then echo -e "there are no memos.\ncreate one by passing the 'new' argument" exit 0 fi echo "you can remove these memos:" ls $memodir read memochoose if [ -f "${memodir}/${memochoose}" ]; then rm -i "${memodir}/${memochoose}" fi exit } case ${args[0]} in new) newmemo ;; remove | rm) rmmemo ;; *) echo -e "-~-~-MEMOS-~-~-" readmemos printmemos echo "-~-~-MEMOS-~-~-" ;; esac