hb

A handbook for UNIX

commit aa5f084c3c25334763b92fe8a2d721d71b7e2546
parent 7964882a79717654bc830f18406807ce715fbdeb
Author: Bharatvaj <bharatvaj@yahoo.com>
Date: Fri, 9 Sep 2022 03:44:23 +0530

Add options cp to quickly copy to $NB_PATH

Implement `cat` notes feature
1 file changed, 17 insertions(+), 9 deletions(-)
M
nb
|
26
+++++++++++++++++---------
diff --git a/nb b/nb
@@ -4,11 +4,13 @@
 
 nb_fatal_error() {
     echo "nb: $1"
+    exit 1
 }
 
 nb_browse() {
-    file="$(fzf)"
-    [ -f "${file}" ] && ${EDITOR} "${file}"
+    cd "${NB_PATH}"
+    file="$(${FUZZER})"
+    [ "${file}" != "" ] && ${EDITOR} "${file}"
 }
 
 nb_sync() {

@@ -27,12 +29,19 @@ nb_sync() {
 
 nb_new() {
     [ -n "$1" ] || nb_fatal_error "usage: nb new <file_name>"
-    ${EDITOR} "${NB_PATH}/$1"
+    ${EDITOR} "${NB_PATH}/$2"
+}
+
+nb_cp() {
+   # Replace the final argument with $NB_PATH
+   [ -n "$1" ] || nb_fatal_error "usage: nb cp <file1>"
+   cp $* "$NB_PATH"
 }
 
 nb_usage() {
-    [ -n "$1" ] && echo "nb: unknown command $1"
+    [ -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
   h, help   Prints this help message

@@ -40,7 +49,7 @@ nb_usage() {
 }
 
 test -z "${EDITOR}" && { export EDITOR=vi; }
-# type on windows invokes a different command, this should be fixed before merging to main
+# TODO detect windows, type on windows invokes a different command, this should be fixed before merging to main
 where "${EDITOR}" >/dev/null 2>/dev/null || { export EDITOR=cat; }
 
 test -d "${NB_PATH}" || { nb_fatal_error "NB_PATH is not a directory"; exit 1; }

@@ -49,10 +58,9 @@ cd "${NB_PATH}"
 nb_option=${1}
 [ $? -gt 1 ] && shift
 case $nb_option in
-    #TODO pass params to nb_browse
-    '')nb_browse ;;
-    #TODO strip $1
-    n|new) nb_new "$@" ;;
+    '') nb_browse ;;
+    c|cp) shift; nb_cp "$@" ;;
+    n|new) shift; nb_new "$@" ;;
 	s|sync) nb_sync ;;
 	h|help) nb_usage ;;
 	*) nb_usage "$@" ;;