nb

simple notebook manager
git clone git@getsh.org:nb.git
Log | Files | Refs | README | LICENSE

nb (2597B)


      1 #!/bin/sh
      2 
      3 # nb - simple notebook manager
      4 
      5 nb_fatal_error() {
      6 	echo "nb: "
      7 	while [ $# -gt 0 ]; do echo "$1"; shift; done
      8 	exit 1
      9 }
     10 
     11 nb_browse() {
     12 	cd "${NB_PATH}"
     13 	file="$(find . -name '.git*' -prune -o -type f | cut -d"/" -f2-  | ${FUZZER})"
     14 	[ "${file}" != "" ] && {
     15 		echo "${file}" >> ${NB_HIST}
     16 		${EDITOR} "${file}";
     17 	}
     18 }
     19 
     20 nb_recent() {
     21 	cd "${NB_PATH}"
     22 	if [ -f "${NB_HIST}" ]; then
     23 		file="$(cat "${NB_HIST}" | tail -n 1)"
     24 		test -f "${file}" && {
     25 			${EDITOR} "${file}";
     26 			exit 0;
     27 		}
     28 	fi
     29 	nb_fatal_error "No recent history"
     30 }
     31 
     32 nb_sync() {
     33 	which git >/dev/null 2>/dev/null || nb_fatal_error "git not available, cannot sync"
     34 	cd "${NB_PATH}"
     35 	git fetch
     36 	git add "${NB_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 nb_new() {
     52 	[ -n "$1" ] || nb_fatal_error "usage: nb new < <-c|-m> files... | filename >"
     53 	if [ $# -gt 1 ]; then case "$1" in
     54 		-c) shift; cp -v "$@" "$NB_PATH/" ;;
     55 		-m) shift; mv -v "$@" "$NB_PATH/" ;;
     56 		*) [ -n "$1" ] && nb_fatal_error "unknown command -- $@" "usage: nb new <-c|-m> files..." ;;
     57 	esac; fi
     58 	[ $? -ne 0 ] && exit 1;
     59 	if ${EDITOR} "${NB_PATH}/$1"; then
     60 		echo "${file}" >> "${NB_HIST}"
     61 	fi
     62 }
     63 
     64 nb_usage() {
     65 	[ -n "$1" ] && echo "$0: Unknown command $1"
     66 	printf 'Usage: nb [OPTIONS]
     67   n, new < <-c|-m> files... | filetocreate >
     68                   Creates filetocreate in $NB_PATH directory with $EDITOR
     69                   if -c option, files are copied to $NB_PATH
     70                   if -m option, files are moved to $NB_PATH
     71   s, sync [ "message" ]
     72                   Attempts a pull/commit/push cycle in $NB_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 NB_PATH="${NB_PATH:=$XDG_DATA_HOME/notes}"
     84 
     85 : ${XDG_DATA_HOME:=$HOME/.local/share}
     86 : ${NB_PATH:=$XDG_DATA_HOME/notes}
     87 : ${NB_HIST:="$NB_PATH/.nbhistory"}
     88 
     89 test -d "${NB_PATH}" || { nb_fatal_error "NB_PATH: ${NB_PATH} is not a directory"; exit 1; }
     90 nb_option=${1}
     91 [ $# -ge 1 ] && shift
     92 case $nb_option in
     93 	'') nb_browse ;;
     94 	n|new) nb_new "$@" ;;
     95 	s|sync) nb_sync "$@" ;;
     96 	r|recent) nb_recent ;;
     97 	h|help) nb_usage ;;
     98 	*) nb_usage "$@" ;;
     99 esac