import - notes - a console notes manager using git DIR Log DIR Files DIR Refs DIR Tags DIR LICENSE --- DIR commit cd514f9fb3d6f5753d014f908f89d142989fc7a7 HTML Author: Solene Rapenne <solene@perso.pw> Date: Thu, 12 Jul 2018 13:17:53 +0200 import Diffstat: A notes | 120 +++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+), 0 deletions(-) --- DIR diff --git a/notes b/notes @@ -0,0 +1,120 @@ +#!/bin/sh + +# tool for taking notes inside a git repository + +REPO=~/notes/ + +# default editor to vi +if [ -z "$EDITOR" ]; then + which nvi 2>/dev/null 1>/dev/null && EDITOR=nvi + test -z "$EDITOR" && which vim 2>/dev/null 1>/dev/null && EDITOR=vim + test -z "$EDITOR" && EDITOR=vi +fi + +usage() { + NAME=$(basename $0) + echo "$NAME [init|ls|edit|history|cat|rm] [path]" + echo "" + echo " $NAME init" + echo " - initialize the git repository" + echo "" + echo " $NAME [ls]" + echo " - show hierarchy tree" + echo "" + echo " $NAME edit path" + echo ' - start $EDITOR on file and auto commit' + echo "" + echo " $NAME history path" + echo " - start tig on file to display file history" + echo "" + echo " $NAME cat path" + echo " - output content of the file" + echo "" + echo " $NAME rm path" + echo " - delete file" +} + +# display a file tree of notes taken +display() { + colortree -C --prune --noreport "$REPO" +} + +# edit a file given as parameter +edit() { + DEST="$1" + DIRNAME=$(dirname "$DEST") + cd "$REPO" + + if [ ! -d "$DEST" ] + then + mkdir -p "${DIRNAME}" + $EDITOR "$DEST" + git add "$DEST" + git commit -m "editing by $USER" "$DEST" + else + echo "${DEST} is a folder. Aborting" + exit 1 + fi +} + +# show file history using tig program +histo() { + DEST="$1" + cd "$REPO" + + tig "$DEST" +} + +# output the content of a file +show_file() { + DEST="$1" + cd "$REPO" + cat "$DEST" +} + +# delete a file and commit in git +delete() { + DEST="$1" + cd "$REPO" + if [ -f "$DEST" ]; + then + git rm "$DEST" + git commit -m "deleted by $USER" "$DEST" + else + echo "${DEST} is a folder. Aborting." + exit 1 + fi +} + +# create a git repo +initialization() { + cd $REPO + if [ -d .git ] + then + echo "Git already initialized" + exit 3 + else + git init + fi +} + +mkdir -p ${REPO} +if [ $? -ne 0 ] +then + echo "Can't create ${REPO}. Aborting." + exit 2 +fi + +PARAM1=$1 +PARAM2=$2 + +if [ "$PARAM1" = "ls" ]; then display ; exit 0 ; fi +if [ "$PARAM1" = "init" ]; then initialization ; exit 0 ; fi +if [ "$PARAM1" = "" ]; then display ; exit 0 ; fi +if expr "$PARAM1" : "^e" >/dev/null && [ -n "$PARAM2" ]; then edit "$PARAM2" ; exit 0 ; fi +if expr "$PARAM1" : "^hi" >/dev/null && [ -n "$PARAM2" ]; then histo "$PARAM2" ; exit 0 ; fi +if expr "$PARAM1" : "^c" >/dev/null && [ -n "$PARAM2" ]; then show_file "$PARAM2" ; exit 0 ; fi +if [ "$PARAM1" = "rm" ] && [ -n "$PARAM2" ]; then delete "$PARAM2" ; exit 0 ; fi +if expr "$PARAM1" : "^he" >/dev/null ; then usage ; exit 0 ; fi + +