memo/memo

274 lines
4.2 KiB
Plaintext
Raw Normal View History

2017-05-30 20:36:38 +02:00
#!/bin/bash
2017-06-02 00:10:06 +02:00
# Configuration
#
# Directory where to hold the memos
# MEMO_DIR=~/memo
2018-05-17 19:50:01 +02:00
# commands
tree_cmd=tree
pandoc_cmd=pandoc
ack_cmd=ack
man_cmd=man
2017-06-02 00:10:06 +02:00
# Script
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
MEMO_FOLDER="${MEMO_DIR:-$HOME/memo}"
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
function precondition::provide_memo_folder(){
if [[ ! -d ${MEMO_FOLDER} ]]
then
echo "create memo folder : ${MEMO_FOLDER}"
mkdir -p ${MEMO_FOLDER}
fi
}
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
function precondition::provide_topic(){
topic_name=$1
topic_path=${MEMO_FOLDER}/${topic_name}
topic_file=${topic_path}/memo.md
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
if [[ ! -d ${topic_path} ]]
then
echo "create memo topic : ${topic_name}"
mkdir -p ${topic_path}
echo "# Memo" > ${topic_file}
2017-06-02 00:10:06 +02:00
fi
2017-05-30 20:36:38 +02:00
}
#
# edit command
#
function edit_memo() {
topic_name=$1
precondition::provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
if [[ -z ${EDITOR} ]]
then
echo "you have to define the ${EDITOR} variable"
exit 1
fi
${EDITOR} ${topic_file}
}
2017-06-02 00:10:06 +02:00
#
# add command
#
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
function add_memo(){
topic_name=$1
precondition::provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
topic_file=${topic_path}/memo.md
2017-06-15 23:18:34 +02:00
shift
if [[ $# -gt 0 ]]
then
memo=$@
echo "append memo to ${topic_name}"
cat >>${topic_file} <<EOF
2017-06-02 00:10:06 +02:00
${memo}
2017-05-30 20:36:38 +02:00
EOF
2017-06-15 23:18:34 +02:00
else
echo "append input to ${topic_name}"
echo "" >>${topic_file}
cat >>${topic_file} -
2017-06-15 23:18:34 +02:00
fi
2017-05-30 20:36:38 +02:00
}
2017-06-02 00:10:06 +02:00
#
# copy command
#
function copy_file(){
topic_name=$1
2017-05-30 20:36:38 +02:00
shift
path=$@
2017-06-02 00:10:06 +02:00
if [[ -z "${path}" ]]
then
show_help
echo
echo "!! path not given"
echo
exit 1
fi
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
if [[ ! -e "${path}" ]]
2017-05-30 20:36:38 +02:00
then
2017-06-02 00:10:06 +02:00
echo "path '${path}' does not exist"
exit 1
2017-05-30 20:36:38 +02:00
fi
2017-06-02 00:10:06 +02:00
precondition::provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
echo "copy '${path}' to '${topic_name}'"
cp -r "${path}" ${topic_path}/
2017-05-30 20:36:38 +02:00
}
2017-06-02 00:10:06 +02:00
#
# search command
#
2017-05-30 20:36:38 +02:00
function search_term(){
search_terms=$@
2017-06-02 00:10:06 +02:00
if [[ -z "${search_terms}" ]]
then
show_help
echo
echo "!! no search terms given"
echo
exit 1
fi
2017-05-30 20:36:38 +02:00
cd ${MEMO_FOLDER}
2018-05-17 19:50:01 +02:00
${ack_cmd} "${search_terms}"
2017-05-30 20:36:38 +02:00
}
2017-06-02 00:10:06 +02:00
#
# show command
#
function show_topic(){
topic_name=$1
topic_path=${MEMO_FOLDER}/${topic_name}
topic_file=${topic_path}/memo.md
if [[ ! -d ${topic_path} ]]
then
echo "${topic_name} does not exist"
exit 1
fi
if [[ -f ${topic_file} ]]
then
echo
#cat ${topic_file}
cat <(
echo "% ${topic_name}" && \
echo "% $( whoami )" && \
echo "% $( date +%Y-%m-%d )" && \
cat ${topic_file} && \
echo && \
echo "# Folders" && \
echo && \
find ${topic_path} -printf "* %p\n") | \
2018-05-17 19:50:01 +02:00
${pandoc_cmd} - -s -t man | \
${man_cmd} -l -
2017-06-02 00:10:06 +02:00
echo
fi
}
#
# list command
#
function list_topics(){
echo
echo "topics : "
echo
2018-05-17 19:50:01 +02:00
${tree_cmd} -L 1 ${MEMO_FOLDER}
2017-06-02 00:10:06 +02:00
}
#
# show help command
#
function show_help() {
2017-05-30 20:36:38 +02:00
cat <<EOF
2017-06-02 00:10:06 +02:00
/\/\ ___ /\/\ ___ /__ \___ ___ / /
/ \ / _ \/ \ / _ \ / /\/ _ \ / _ \ / /
/ /\/\ \ __/ /\/\ \ (_) | / / | (_) | (_) / /___
\/ \/\___\/ \/\___/ \/ \___/ \___/\____/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
Usage:
memo <command> arguments
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
Commands:
add <topic> [memo]
adds a memo.md in ${MEMO_FOLDER}/<topic>/
containing the text
2017-06-15 23:18:34 +02:00
if memo is not given it will read from input
so piping is possible.
Example: cat file.txt | memo add topic
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
copy <topic> <file to copy>
copy a file to ${MEMO_FOLDER}/<topic>/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
search <term to search>
search for a term in
${MEMO_FOLDER}/<memo-title>/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
show <topic>
show topic file and list all file in the
${MEMO_FOLDER}/<topic>/
2017-05-30 20:36:38 +02:00
edit <topic>
edit topic ${MEMO_FOLDER}/<topic>.md
2017-06-02 00:10:06 +02:00
list
prints a list of all topics
2017-05-30 20:36:38 +02:00
EOF
}
#
# main
#
command=$1
shift
2017-06-02 00:10:06 +02:00
precondition::provide_memo_folder
2017-05-30 20:36:38 +02:00
case $command in
2017-06-02 00:10:06 +02:00
add) add_memo $@
2017-05-30 20:36:38 +02:00
;;
2017-06-02 00:10:06 +02:00
copy)
copy_file $@
2017-05-30 20:36:38 +02:00
;;
search)
search_term $@
;;
2017-06-02 00:10:06 +02:00
show)
show_topic $@
;;
edit)
edit_memo $@
;;
2017-06-02 00:10:06 +02:00
list)
list_topics
;;
*) show_help
2017-05-30 20:36:38 +02:00
;;
esac