Major internal rework by Quentin Rameau - notes - a console notes manager using git
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR LICENSE
---
DIR commit 901af4715f8b6dd389b9d047a4ff15e769b61723
DIR parent 15b0eb8c733c9fff27256068ba210d3d904bfadb
HTML Author: Solene Rapenne <solene@perso.pw>
Date: Sat, 14 Jul 2018 10:50:03 +0200
Major internal rework by Quentin Rameau
Diffstat:
M notes | 130 ++++++++++++++-----------------
1 file changed, 60 insertions(+), 70 deletions(-)
---
DIR diff --git a/notes b/notes
@@ -2,40 +2,40 @@
# tool for taking notes inside a git repository
-REPO=${REPO:=~/notes}
+: ${REPO:=~/notes}
+
+# search for a suitable editor
+[ -z "$EDITOR" ] && \
+for e in nvim vim emacs vi ed; do
+ if command -v $e >/dev/null 2>&1; then
+ EDITOR=$e
+ break
+ fi
+done
-# default editor to vi
if [ -z "$EDITOR" ]; then
- type nvim >/dev/null 2>&1 && EDITOR=nvim
- test -z "$EDITOR" && type vim >/dev/null 2>&1 && EDITOR=vim
- test -z "$EDITOR" && type emacs >/dev/null 2>&1 && EDITOR=emacs
- test -z "$EDITOR" && EDITOR=vi
+ printf 'Could not elect an editor. ED is the standard text editor!\n'
+ exit 1
fi
usage() {
- NAME=$(basename $0)
- echo "$NAME [init|ls|history|cat|rm] [path]"
- echo ""
- echo " $NAME init"
- echo " - initialize the git repository"
- echo ""
- echo " $NAME [ls]"
- echo " - show hierarchy tree"
- echo ""
- echo " $NAME 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"
- echo ""
- echo " $NAME last"
- echo " - show files ordered by edition time"
+ name=$(basename $0)
+ printf '%s\n' \
+ "$name [ls] | init | last | [history|cat|rm] PATH" \
+ "$name [ls]" \
+ ': show hierarchy tree' \
+ "$name init" \
+ ': initialize the git repository' \
+ "$name last" \
+ ': show files ordered by edition time' \
+ "$name history PATH" \
+ ': uses tig to display file history' \
+ "$name cat PATH" \
+ ': output content of the file' \
+ "$name rm PATH" \
+ ': delete file' \
+ "$name PATH" \
+ ": start \$EDITOR ($EDITOR) on file and auto commit"
}
# display a file tree of notes taken
@@ -45,54 +45,46 @@ display() {
# edit a file given as parameter
edit() {
- DEST="$1"
- DIRNAME=$(dirname "$DEST")
cd "$REPO"
-
- if [ ! -d "$DEST" ]
+ if [ ! -d "$1" ]
then
- mkdir -p "${DIRNAME}"
- $EDITOR "$DEST"
- git add "$DEST"
- git commit -m "editing by $USER" "$DEST"
+ mkdir -p "$(dirname "$destdir")"
+ "$EDITOR" "$1"
+ git add "$1"
+ git commit -m "editing by $USER" "$1"
else
- echo "${DEST} is a folder. Aborting"
+ printf 'Aborting: "%s" is a directory.\n' "$1"
exit 1
fi
}
# show file history using tig program
histo() {
- DEST="$1"
cd "$REPO"
-
- tig "$DEST"
+ tig "$1"
}
# output the content of a file
show_file() {
- DEST="$1"
cd "$REPO"
- cat "$DEST"
+ cat "$1"
}
# delete a file and commit in git
delete() {
- DEST="$1"
cd "$REPO"
- if [ -f "$DEST" ];
+ if [ -f "$1" ];
then
- git rm "$DEST"
- git commit -m "deleted by $USER" "$DEST"
+ git rm "$1"
+ git commit -m "deleted by $USER" "$1"
else
- echo "${DEST} is a folder. Aborting."
+ printf 'Aborting: "%s" is a directory.\n' "$1"
exit 1
fi
}
# display the list of edited files ordered by time
last() {
- DEST="$1"
cd "$REPO"
git log --pretty="%cr" --name-only | \
awk '{
@@ -113,12 +105,12 @@ _completion_list() {
cd "$REPO"
find . -name '.git' -prune -o -mindepth 2 -type f -print | sed 's,^\./,,'
else
- echo "$REPO does not exist. Aborting..."
+ printf 'Aborting: "%s" does not exist.\n' "$REPO"
exit 4
fi
}
-# create a git repo
+# create a git repository
initialization() {
cd "$REPO"
if [ -d .git ]
@@ -130,27 +122,25 @@ initialization() {
fi
}
-mkdir -p "${REPO}"
-if [ $? -ne 0 ]
+if ! mkdir -p "$REPO"
then
- echo "Can't create ${REPO}. Aborting."
+ printf 'Aborting: cannot create "%s".\n' "$REPO"
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 [ "$PARAM1" = "_files" ] ; then _completion_list ; exit 0 ; fi
-if [ "$PARAM1" = "rm" ] && [ -n "$PARAM2" ]; then delete "$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 expr "$PARAM1" : "^he" >/dev/null ; then usage ; exit 0 ; fi
-if expr "$PARAM1" : "^l" >/dev/null ; then last ; exit 0 ; fi
-
-if [ -f "${REPO}/${PARAM1}" ] ; then edit "$PARAM1" ; exit 0 ; fi
+if [ -f "$REPO/$1" ]; then
+ edit "$1"
+ exit 0
+fi
-# nothing matched
-echo "${PARAM1} is not a valid command or an existing filename" && exit 5
+case "$1" in
+ '') display ;;
+ "ls") display ;;
+ "i*") initialization ;;
+ "la*") last ;;
+ "hi*") [ -n "$2" ] && histo "$2" ;;
+ "c*") [ -n "$2" ] && show_file "$2" ;;
+ "r*") [ -n "$2" ] && delete "$2" ;;
+ "_files") _completion_list ;;
+ *) usage && exit 1 ;;
+esac