#!/bin/bash # Configuration # # Directory where to hold the memos # MEMO_DIR=~/memo # Script MEMO_FOLDER="${MEMO_DIR:-$HOME/memo}" function precondition::provide_memo_folder(){ if [[ ! -d ${MEMO_FOLDER} ]] then echo "create memo folder : ${MEMO_FOLDER}" mkdir -p ${MEMO_FOLDER} fi } function precondition::provide_topic(){ topic_name=$1 topic_path=${MEMO_FOLDER}/${topic_name} topic_file=${topic_path}/memo.md if [[ ! -d ${topic_path} ]] then echo "create memo topic : ${topic_name}" mkdir -p ${topic_path} echo "# Memo" > ${topic_file} fi } # # 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} } # # add command # function add_memo(){ topic_name=$1 precondition::provide_topic ${topic_name} topic_path=${MEMO_FOLDER}/${topic_name} topic_file=${topic_path}/memo.md shift if [[ $# -gt 0 ]] then memo=$@ echo "append memo to ${topic_name}" cat >>${topic_file} <>${topic_file} cat >>${topic_file} - fi } # # copy command # function copy_file(){ topic_name=$1 shift path=$@ if [[ -z "${path}" ]] then show_help echo echo "!! path not given" echo exit 1 fi if [[ ! -e "${path}" ]] then echo "path '${path}' does not exist" exit 1 fi precondition::provide_topic ${topic_name} topic_path=${MEMO_FOLDER}/${topic_name} echo "copy '${path}' to '${topic_name}'" cp -r "${path}" ${topic_path}/ } # # search command # function search_term(){ search_terms=$@ if [[ -z "${search_terms}" ]] then show_help echo echo "!! no search terms given" echo exit 1 fi cd ${MEMO_FOLDER} ack "${search_terms}" } # # 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") | \ pandoc - -s -t man | \ man -l - echo fi # tree -a ${topic_path} } # # list command # function list_topics(){ echo echo "topics : " echo tree -L 1 ${MEMO_FOLDER} } # # show help command # function show_help() { cat < arguments Commands: add [memo] adds a memo.md in ${MEMO_FOLDER}// containing the text if memo is not given it will read from input so piping is possible. Example: cat file.txt | memo add topic copy copy a file to ${MEMO_FOLDER}// search search for a term in ${MEMO_FOLDER}// show show topic file and list all file in the ${MEMO_FOLDER}// edit edit topic ${MEMO_FOLDER}/.md list prints a list of all topics EOF } # # main # command=$1 shift precondition::provide_memo_folder case $command in add) add_memo $@ ;; copy) copy_file $@ ;; search) search_term $@ ;; show) show_topic $@ ;; edit) edit_memo $@ ;; list) list_topics ;; *) show_help ;; esac