hb (2597B)
1 #!/bin/sh 2 3 # hb - simple notebook manager 4 5 hb_fatal_error() { 6 echo "hb: " 7 while [ $# -gt 0 ]; do echo "$1"; shift; done 8 exit 1 9 } 10 11 hb_browse() { 12 cd "${HB_PATH}" 13 file="$(find . -name '.git*' -prune -o -type f | cut -d"/" -f2- | ${FUZZER})" 14 [ "${file}" != "" ] && { 15 echo "${file}" >> ${HB_HIST} 16 ${EDITOR} "${file}"; 17 } 18 } 19 20 hb_recent() { 21 cd "${HB_PATH}" 22 if [ -f "${HB_HIST}" ]; then 23 file="$(cat "${HB_HIST}" | tail -n 1)" 24 test -f "${file}" && { 25 ${EDITOR} "${file}"; 26 exit 0; 27 } 28 fi 29 hb_fatal_error "No recent history" 30 } 31 32 hb_sync() { 33 which git >/dev/null 2>/dev/null || hb_fatal_error "git not available, cannot sync" 34 cd "${HB_PATH}" 35 git fetch 36 git add "${HB_PATH}" 37 if [ -n "$1" ]; then 38 git commit -m "$1" 39 else 40 git commit -m "$(uname)" 41 fi 42 git pull 43 #TODO check for conflicts 44 # if conflict exists, checkout to a 45 # different unique branch 46 # And pull after fetch seems 47 # redundant, replace with merge 48 git push 49 } 50 51 hb_new() { 52 [ -n "$1" ] || hb_fatal_error "usage: hb new < <-c|-m> files... | filename >" 53 if [ $# -gt 1 ]; then case "$1" in 54 -c) shift; cp -v "$@" "$HB_PATH/" ;; 55 -m) shift; mv -v "$@" "$HB_PATH/" ;; 56 *) [ -n "$1" ] && hb_fatal_error "unknown command -- $@" "usage: hb new <-c|-m> files..." ;; 57 esac; fi 58 [ $? -ne 0 ] && exit 1; 59 if ${EDITOR} "${HB_PATH}/$1"; then 60 echo "${file}" >> "${HB_HIST}" 61 fi 62 } 63 64 hb_usage() { 65 [ -n "$1" ] && echo "$0: Unknown command $1" 66 printf 'Usage: hb [OPTIONS] 67 n, new < <-c|-m> files... | filetocreate > 68 Creates filetocreate in $HB_PATH directory with $EDITOR 69 if -c option, files are copied to $HB_PATH 70 if -m option, files are moved to $HB_PATH 71 s, sync [ "message" ] 72 Attempts a pull/commit/push cycle in $HB_PATH 73 if "message" is present, commit with "message" 74 r, recent Open the last file that was accessed 75 h, help Prints this help message 76 ' 77 } 78 79 test -z "${EDITOR}" && { export EDITOR=vi; } 80 # TODO detect windows, type on windows invokes a different command 81 which "${EDITOR}" >/dev/null 2>/dev/null || { export EDITOR=cat; } 82 XDG_DATA_HOME="${XDG_DATA_HOME:=$HOME}" 83 HB_PATH="${HB_PATH:=$XDG_DATA_HOME/notes}" 84 85 : ${XDG_DATA_HOME:=$HOME/.local/share} 86 : ${HB_PATH:=$XDG_DATA_HOME/notes} 87 : ${HB_HIST:="$HB_PATH/.hbhistory"} 88 89 test -d "${HB_PATH}" || { hb_fatal_error "HB_PATH: ${HB_PATH} is not a directory"; exit 1; } 90 hb_option=${1} 91 [ $# -ge 1 ] && shift 92 case $hb_option in 93 '') hb_browse ;; 94 n|new) hb_new "$@" ;; 95 s|sync) hb_sync "$@" ;; 96 r|recent) hb_recent ;; 97 h|help) hb_usage ;; 98 *) hb_usage "$@" ;; 99 esac