hb

Log | Files | Refs | README | LICENSE

commit e48e293e20f34ccb496da986b929328df5f4d7fb
parent 0229dd46673e8d29893241da586174e3418716ac
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date:   Mon, 12 Aug 2024 05:01:53 +0530

Add test case framework in test

Add option for sync to have custom messages in commit

Change default sync commit message

Move nb_cp to nb new -c

Add option -m to new to move a file

Use variable $NB_HIST to point to history file

Add .gitignore

Diffstat:
A.gitignore | 2++
Mnb | 57++++++++++++++++++++++++++++++++++-----------------------
Atest | 23+++++++++++++++++++++++
3 files changed, 59 insertions(+), 23 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +test.log +sample diff --git a/nb b/nb @@ -3,7 +3,8 @@ # nb - simple notebook manager nb_fatal_error() { - echo "nb: $1" + echo "nb: " + while [ $# -gt 0 ]; do echo "$1"; shift; done exit 1 } @@ -11,15 +12,15 @@ nb_browse() { cd "${NB_PATH}" file="$(find . -name '.git*' -prune -o -type f | cut -d"/" -f2- | ${FUZZER})" [ "${file}" != "" ] && { - echo "${file}" >> .nbhistory; + echo "${file}" >> ${NB_HIST} ${EDITOR} "${file}"; } } nb_recent() { cd "${NB_PATH}" - if [ -f ".nbhistory" ]; then - file="$(cat .nbhistory | tail -n 1)" + if [ -f "${NB_HIST}" ]; then + file="$(cat "${NB_HIST}" | tail -n 1)" test -f "${file}" && { ${EDITOR} "${file}"; exit 0; @@ -30,9 +31,14 @@ nb_recent() { nb_sync() { which git >/dev/null 2>/dev/null || nb_fatal_error "git not available, cannot sync" + cd "${NB_PATH}" git fetch git add "${NB_PATH}" - git commit -m "$(uname -a)" + if [ -n "$1" ]; then + git commit -m "$1" + else + git commit -m "$(uname)" + fi git pull #TODO check for conflicts # if conflict exists, checkout to a @@ -43,25 +49,31 @@ nb_sync() { } nb_new() { - [ -n "$1" ] || nb_fatal_error "usage: nb new <file_name>" - ${EDITOR} "${NB_PATH}/$1" -} - -nb_cp() { - # Replace the final argument with $NB_PATH - [ -n "$1" ] || nb_fatal_error "usage: nb cp <file1>" - cp $* "$NB_PATH" + [ -n "$1" ] || nb_fatal_error "usage: nb new < <-c|-m> files... | filename >" + if [ $# -gt 1 ]; then case "$1" in + -c) shift; cp -v "$@" "$NB_PATH/" ;; + -m) shift; mv -v "$@" "$NB_PATH/" ;; + *) [ -n "$1" ] && nb_fatal_error "unknown command -- $@" "usage: nb new <-c|-m> files..." ;; + esac; fi + [ $? -ne 0 ] && exit 1; + if ${EDITOR} "${NB_PATH}/$1"; then + echo "${file}" >> "${NB_HIST}" + fi } nb_usage() { [ -n "$1" ] && echo "$0: Unknown command $1" - printf "Usage: nb [OPTIONS] - c, cp cp <options> file - n, new Opens a file in the NB_PATH directory with EDITOR - s, sync Attempts a add/commit/push cycle in NB_PATH - r, recent open the last file that was accessed - h, help Prints this help message -" + printf 'Usage: nb [OPTIONS] + n, new < <-c|-m> files... | filetocreate > + Creates filetocreate in $NB_PATH directory with $EDITOR + if -c option, files are copied to $NB_PATH + if -m option, files are moved to $NB_PATH + s, sync [ "message" ] + Attempts a pull/commit/push cycle in $NB_PATH + if "message" is present, commit with "message" + r, recent Open the last file that was accessed + h, help Prints this help message +' } test -z "${EDITOR}" && { export EDITOR=vi; } @@ -72,16 +84,15 @@ NB_PATH="${NB_PATH:=$XDG_DATA_HOME/notes}" : ${XDG_DATA_HOME:=$HOME/.local/share} : ${NB_PATH:=$XDG_DATA_HOME/notes} +: ${NB_HIST:="$NB_PATH/.nbhistory"} test -d "${NB_PATH}" || { nb_fatal_error "NB_PATH: ${NB_PATH} is not a directory"; exit 1; } -cd "${NB_PATH}" || nb_fatal_error "NB_PATH is not a directory" nb_option=${1} [ $# -ge 1 ] && shift case $nb_option in '') nb_browse ;; - c|cp) nb_cp "$@" ;; n|new) nb_new "$@" ;; - s|sync) nb_sync ;; + s|sync) nb_sync "$@" ;; r|recent) nb_recent ;; h|help) nb_usage ;; *) nb_usage "$@" ;; diff --git a/test b/test @@ -0,0 +1,23 @@ +#!/bin/sh + +logfile=test.log + +export NB_PATH=sample +export EDITOR=ls +export FUZZER="xargs" + +test_browse() { FUZZER="echo good_file.txt" ./nb; } + +test_browsefail() { ! FUZZER="echo bad_file.txt" ./nb; } + +test_nonexistentfile() { ! ./nb n -c nonexistentfile; } + +test_existentfile() { ./nb n -c COPYING; } + +set -- browse browsefail nonexistentfile existentfile +date +"==========[%Y/%m/%d %H:%M:%S]==========" >> $logfile +while [ $# -gt 0 ]; do + echo "# Running test ... $1" >> $logfile + echo "# Running test ... $1" $(if 1>>$logfile 2>>$logfile "test_$1" ; then echo "Passed"; else echo "Falied"; fi) + shift +done