add bash completion

keep-around/68d49fda1c6f08d738e590b49edb69d92d982b42
Ingolf Wagner 2017-05-30 20:36:38 +02:00
parent 53059600ed
commit 11572954d7
2 changed files with 162 additions and 0 deletions

49
bash_completion/memo Normal file
View File

@ -0,0 +1,49 @@
#!/bin/bash
_memo(){
local cur opt prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
case ${COMP_CWORD} in
1)
opts="add append search"
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}" ) )
return 0
;;
2)
prev="${COMP_WORDS[COMP_CWORD-1]}"
case ${prev} in
search)
return 1
;;
*)
entries=$( ls ~/memo )
COMPREPLY=( $(compgen -W "${entries}" -- "${cur}" ) )
return 0
;;
esac
;;
3)
operatior="${COMP_WORDS[COMP_CWORD-2]}"
case ${operatior} in
append)
COMPREPLY=( $(compgen -f "${cur}" ) )
return 0
;;
*)
return 1
;;
esac
;;
*)
return 1
;;
esac
}
complete -F _memo memo

113
memo Executable file
View File

@ -0,0 +1,113 @@
#!/bin/bash
#set -x
MEMO_FOLDER=~/memo
# functions
function preconditions_memo(){
mkdir -p ${MEMO_FOLDER}
}
function create_memo(){
memo_name=$1
shift
text=$@
memo_path=${MEMO_FOLDER}/${memo_name}
if [[ -d ${memo_path} ]]
then
echo "append to ${memo_name}"
cat >>${memo_path}/memo.md <<EOF
${text}
EOF
else
echo "create ${memo_name}"
mkdir -p ${memo_path}
cat >${memo_path}/memo.md <<EOF
# ${memo_name}
${text}
EOF
fi
}
function append_memo(){
memo_name=$1
shift
path=$@
memo_path=${MEMO_FOLDER}/${memo_name}
if [[ ! -d ${memo_path} ]]
then
create_memo ${memo_name} "init"
fi
echo "append '${path}' to memo '${memo_name}'"
cp -r "${path}" ${memo_path}/
}
function search_term(){
search_terms=$@
cd ${MEMO_FOLDER}
ack "${search_terms}"
}
function help() {
cat <<EOF
MeMo Tool:
memo <command> arguments
memo add <memo-title> [text]
----------------------------------------------
adds a memo.md in ${MEMO_FOLDER}/<memo-title>/
containing the text
memo append <memo-title> <file to add>
--------------------------------------
add a file to ${MEMO_FOLDER}/<memo-title>/
memo search <term to search>
----------------------------
search for a term in
${MEMO_FOLDER}/<memo-title>/
EOF
}
#
# main
#
command=$1
shift
preconditions_memo
case $command in
add) create_memo $@
;;
append)
append_memo $@
;;
search)
search_term $@
;;
*) help
;;
esac