: nply/hb

A handbook for UNIX

commit 39d955857236822a16a6bbe3ebda16f8cc3f0afb
parent f045d78ca7ef16d3acb4c288826846af479b9374
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Mon, 6 Apr 2026 02:54:54 +0530

Fix hb_new

Remove hb_browse and move functionality to hb_edit

Use FM if input is a directory in hb_edit

Remove where and type usage in favour of command

Handle missing FUZZER gracefully

Update AUTHOR name and email

Update hb.1

Update README with current instructions

Modify logo in README

Add version command

Add TODO
5 files changed, 151 insertions(+), 115 deletions(-)
M
COPYING
|
2
+-
M
README
|
50
+++++++++++++++++++++++++++++++++++++-------------
A
TODO
|
3
+++
M
hb
|
173
++++++++++++++++++++++++++++++++++++-------------------------------------------
M
hb.1
|
38
+++++++++++++++++++++++++++++++-------
diff --git a/COPYING b/COPYING
@@ -1,6 +1,6 @@
 hb is in the public domain.
 
-To the extent possible under law, Bharatvaj <bharatvaj@nonplanar.org>
+To the extent possible under law, Bharatvaj Hemanth <bhp@nply.org>
 has waived all copyright and related or neighboring rights to this work.
 
 http://creativecommons.org/publicdomain/zero/1.0/
diff --git a/README b/README
@@ -1,16 +1,16 @@
 hb
 ==
-    _______ 
-   |x--|   |
-   |o--|1--|
-   |   |2--|
+    ______.--.
+   |x--|  |-@|
+   |o--|1-|%$|
+   |   |2-`--'
    |___|__/ 
 
 hb can be used to hold txt databases such as ledger and todo.txt.
 
 hb provides syncing capabilities to your notebook. Either via VCS or rsync.
 
-Additional sync capabilities can be provided by 
+Additional sync capabilities can be provided in future versions.
 
 You may like hb if you do any of the following,
 

@@ -21,28 +21,52 @@ You may like hb if you do any of the following,
 Usage
 -----
 
-hb is a framework that makes storing and retrieving text related information
+hb is a tool that makes storing and retrieving text related information
 effortless. Start by,
 
-     $ hb new relative-folder/note.txt
+     $ hb new note.txt
 
-This creates a note.txt under relative-folder/. Sometimes you may want to
-import a note from your filesystem under hb, To do that use,
+This creates a note.txt under $HB_PATH
+
+$HB_PATH is the directory where all notes ends up. If $HB_PATH is not provided,
+".local/share/notes" is used as $HB_PATH.
+
+Sometimes you may want to import a note from your filesystem under hb.
+To do that use,
     
     $ hb new -m . Downloads/recipe.txt
     $ hb new -c . WorkingDir/somefile.txt
 
 -m does a move operations, and -c does a copy operation.
 
+This simply utilizes the mv and cp commands in UNIX.
+
+You may find the -m and -c options to edit the directories cumbersome, and
+there is currently no way to add new subdirectories, to do it manually, you
+can use,
+
+    $ hb e .
+
+This will open the $HB_PATH dir in $FM.
+
 After populating files inside hb, you can access them with,
 
     $ hb
 
-Will start a fuzzy matcher if $FUZZER is set.  If $FUZZER is empty or not
-found in $PATH. hb simply does `cd` to "$HB_PATH"
+This will start a fuzzy matcher if $FUZZER is set. If $FUZZER is empty or not
+found in $PATH. hb will fail with an error.
+
+To access a particular file,
+
+    $ hb e recipe.txt
+    $ hb e somefile.txt
+
+Every edit operation gets recorded in $HB_HIST, which by default sits in
+$HB_PATH/.hbhistory. To edit the last edited file,
+
+    $ hb e
 
-$HB_PATH should be pointed to the directory, where all the notes will end up.
-If $HB_PATH is not provided, the ".local/share/notes" is used as $HB_PATH.
+And finally,
 
     $ hb sync
 
diff --git a/TODO b/TODO
@@ -0,0 +1,3 @@
+2026-04-06: Support external sync via plugins
+2026-04-06: Improve git sync functionality
+# Check for conflicts and if conflict exists, checkout to a  different unique branch and pull after fetch seems, though this can be easily solved by merge driverso
diff --git a/hb b/hb
@@ -1,110 +1,93 @@
 #!/bin/sh
 
-# hb - simple notebook manager
+# hb - handbook for unix
+HB_VERSION=0.4
 
 hb_fatal_error() {
-	echo "hb: "
-	while [ $# -gt 0 ]; do echo "$1"; shift; done
+	echo "hb: $*"
 	exit 1
 }
 
-hb_browse() {
-    cd "${HB_PATH}"
-    if [ -d "${1}" ] && command -v "${FM}"; then
-        "${FM}" "${1}"
-    elif [ -e "${1}" ]; then
-        "${EDITOR}" "${1}"
-    fi
-}
-
-
 hb_fuzz() {
-	cd "${HB_PATH}"
-    # TODO Add git ls-files -o --exclude-standard output to FUZZER as well
-    {
-        if [ -d ".git" ] && command -v git; then
-            command git ls-files;
-        else
-            find . -type f \( -path '*/.git*' -prune -o -print \) | cut -d"/" -f2-;
-        fi
-    } | ${FUZZER} | tee -a ${HB_HIST};
+	cd "$HB_PATH"
+	if [ -d ".git" ] && command -v git >/dev/null; then
+		command git ls-files | "$FUZZER"
+	else
+		command find . -type f \( -path '*/.git*' -prune -o -print \) |
+			cut -d"/" -f2- | "$FUZZER"
+	fi
 }
 
 hb_sync() {
-	type git >/dev/null 2>/dev/null ||
-        hb_fatal_error "git not available, cannot sync"
-	cd "${HB_PATH}" ||
-        hb_fatal_error "Cannot change directory to '${HB_PATH}'"
+	cd "$HB_PATH"
+	command -v git >/dev/null ||
+		hb_fatal_error "git not available, cannot sync"
+
 	git fetch
 	git add .
 	if [ -n "$1" ]; then
 		git commit -m "$1"
 	else
-		git commit -m "$(uname)"
+		git commit -m "$(uname -n)"
 	fi
 	git pull
-	#TODO check for conflicts
-	# if conflict exists, checkout to a
-	# different unique branch
-	# And pull after fetch seems
-	# redundant, replace with merge
 	git push
 }
 
 hb_edit() {
-    hb_edit_usage='usage: hb edit [file]'
-	[ "$#" -gt 1  ] && hb_fatal_error "${hb_edit_usage}"
-
-    edit_file="$1"
-    if [ "$#" -eq 0 ]; then
-        [ -f "${HB_HIST}" ] || hb_fatal_error "No recent history"
-        edit_file="$(cat "${HB_HIST}" | tail -n 1)"
-    fi
-    "${EDITOR}" "${HB_PATH}/${edit_file}" ||
-        hb_fatal_error "Failed to open '${edit_file}'"
-    echo "${edit_file}" >> "${HB_HIST}"
+	cd "$HB_PATH"
+	hb_edit_usage='usage: hb edit [file | directory]'
+	case $# in
+	0)
+		[ -f "$HB_HIST" ] || hb_fatal_error "No recent history"
+		ef="$(tail -n 1 "$HB_HIST")"
+		;;
+	1)
+		ef="$1"
+		;;
+	*)
+		hb_fatal_error "${hb_edit_usage}"
+		;;
+	esac
+
+	_editor="$EDITOR"
+	[ -d "$ef" ] && _editor="$FM"
+
+	"$_editor" "$ef" || hb_fatal_error "Failed to open '$ef'"
+
+	echo "$ef" >>"$HB_HIST"
 }
 
 hb_new() {
-    hb_new_usage='usage: hb new [-c|-m dest] filename'
-	[ $# -gt 3 ] && hb_fatal_error "${hb_new_usage}"
-
-    if [ $# -eq 1 ]; then
-        src_file="$1"
-        dest_dir="$HB_PATH"
-        basedir="${src_file%/*}"
-        if [ ! "${src_file}" = "${basedir}" ]; then
-            mkdir -p "${basedir}"
-        fi
-    elif [ $# -eq 3 ]; then
-        src_file="$3"
-        dest_dir="$HB_PATH/$2"
-    else
-        hb_fatal_error "${hb_new_usage}"
-    fi
-
-	if [ $# -eq 3 ]; then case "$1" in
-		-c) shift; cp -v "${src_file}" "${dest_dir}" ;;
-		-m) shift; mv -v "${src_file}" "${dest_dir}" ;;
-		*) [ -n "$1" ] &&
-            hb_fatal_error "unknown command -- $@" "${hb_new_usage}" ;;
-	esac; fi
-	[ $? -ne 0 ] && exit 1;
-	if ${EDITOR} "${dest_dir}/$1"; then
-		echo "${file}" >> "${HB_HIST}"
+	hb_new_usage='usage: hb new [-c|-m dest] filename'
+	ef="$1"
+	if [ $# -eq 3 ]; then
+		arg="$1"
+		ef="$2"
+		shift; shift;
+		case "$arg" in
+		-c) cp -rv "$@" "$HB_PATH/$ef" ;;
+		-m) mv -v  "$@" "$HB_PATH/$ef" ;;
+		*) hb_fatal_error "Expected -c or -m\n${hb_new_usage}" ;;
+		esac
+	elif [ $# -ne 1 ]; then
+		hb_fatal_error "${hb_new_usage}"
 	fi
+
+	[ $# -eq 1 ] && hb_edit "$ef"
 }
 
 hb_usage() {
 	[ -n "$1" ] && echo "$0: Unknown command $1"
 	printf 'Usage: hb [OPTIONS]
-  e, edit [file]
+  e, edit [file|directory]
                   Open the file in EDITOR, if it exists
-  n, new  [-c|-m files... | filename]
-                  Creates filetocreate in $HB_PATH directory with $EDITOR
+                  If argument is a directory, open in FM
+                  Else recently opened resource
+  n, new [-c|-m dest ] filename
+                  Creates filename in $HB_PATH directory with $EDITOR
                   if -c option, files are copied to $HB_PATH
                   if -m option, files are moved to $HB_PATH
-                  If no file is specified, opens the last entry
   s, sync [ "message" ]
                   Attempts a pull/commit/push cycle in $HB_PATH
                   if "message" is present, commit with "message"

@@ -112,28 +95,30 @@ hb_usage() {
 '
 }
 
-[ -z "${EDITOR}" ] && { export EDITOR=vi; }
-# TODO detect windows, type on windows invokes a different command
-which "${EDITOR}" >/dev/null 2>/dev/null || { export EDITOR=cat; }
-XDG_DATA_HOME="${XDG_DATA_HOME:=$HOME}"
-HB_PATH="${HB_PATH:=$XDG_DATA_HOME/notes}"
-
 : ${XDG_DATA_HOME:=$HOME/.local/share}
 : ${HB_PATH:=$XDG_DATA_HOME/notes}
 : ${HB_HIST:="$HB_PATH/.hbhistory"}
-
-[ -d "${HB_PATH}" ] || {
-    hb_fatal_error "HB_PATH: '${HB_PATH}' is not a directory"; exit 1;
-}
-
-hb_option=${1}
-[ $# -ge 1 ] && shift
-case $hb_option in
-    '') file=$(hb_fuzz); hb_browse "${file}" ;;
-	b) hb_browse "." ;;
-	e|edit) hb_edit "$@" ;;
-	n|new) hb_new "$@" ;;
-	s|sync) hb_sync "$@" ;;
-	h|help) hb_usage ;;
-	*) hb_usage "$@" ;;
+: ${EDITOR:=vi}
+: ${FM:=cd}
+: ${FUZZER:=fzf}
+
+command -v "$EDITOR" >/dev/null || { EDITOR=cat; }
+
+[ -d "$HB_PATH" ] ||
+	hb_fatal_error "HB_PATH: '$HB_PATH' is not a directory"
+
+hbcmd=$1 && shift
+case $hbcmd in
+'')
+	command -v $FUZZER >/dev/null ||
+		hb_fatal_error "\$FUZZER: '$FUZZER' not found"
+	file="$(hb_fuzz)"
+	hb_edit "$file"
+	;;
+e | edit) hb_edit "$@" ;;
+n | new) hb_new "$@" ;;
+s | sync) hb_sync "$@" ;;
+h | help) hb_usage ;;
+v | version) echo "hb v$HB_VERSION" ;;
+*) hb_usage "$@" ;;
 esac
diff --git a/hb.1 b/hb.1
@@ -1,19 +1,23 @@
-.Dd October 06, 2021
+.Dd April 06, 2026
 .Dt HB 1
 .Os
 .Sh NAME
 .Nm hb
-.Nd simple notebook manager
+.Nd Handbook for UNIX
 .Sh SYNOPSIS
 .Nm
+.Op e|edit
+.Ar [file|directory]
+.Nm
 .Op n|new
-.Ar file_name.md
+.Ar [-c|-m] filename
 .Nm
 .Op s|sync
+.Ar [commit message]
+.Nm
 .Sh DESCRIPTION
-The
 .Nm
-provides a lightweight interface for accessing and adding notes on command line.
+provides a lightweight interface for accessing and adding notes on the command line.
 .Pp
 .Nm
 doesn't complain much and tries to work with what is available.

@@ -33,10 +37,30 @@ attempts a add/commit/push cycle on HB_PATH via
 .Sh ENVIRONMENT
 .Bl -hang -width "HB_PATH"
 .It Ev HB_PATH
-The location of your notebook folder.
+The location of your notebook folder. Default: $XDG_DATA_HOME/notes
+.It Ev HB_HIST
+The location of the history file. Default: $HB_PATH/.hbhistory
+.El
+.Sh Other variables
+.Bl -hang -width "HB_PATH"
+.It Ev FM
+If
+.Op edit
+receives a directory, FM is used. Default: cd
+.It Ev FUZZER
+Program used for picking files. Default: fzf
 .El
 .Sh AUTHORS
-.An Bharatvaj Aq Mt bharatvaj@nonplanar.org
+.An Bharatvaj Hemanth Aq Mt bhp@nply.org
+.Sh EXAMPLES
+# Open last edited file. This will open the last entry in $HB_HIST
+    $ hb e
+
+# Browse notes
+    $ hb e .
+
+# Move files to notes/zettle and edit the zettle/ folder
+    $ hb n -m zettle/ idea1.org idea2.md
 .Sh LICENSE
 .Nm
 is in the public domain.