commit f045d78ca7ef16d3acb4c288826846af479b9374
parent 6796c172f026ed13859c12bc60cc78c84a5770a5
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 24 Jan 2026 17:24:35 +0530
parent 6796c172f026ed13859c12bc60cc78c84a5770a5
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 24 Jan 2026 17:24:35 +0530
Add support for adding custom paths in 'new' Add 'edit' that takes relative file path as parameter Replace which usage with type Update .gitignore: Fix sample/
3 files changed, 74 insertions(+), 34 deletions(-)
diff --git a/.gitignore b/.gitignore @@ -1,2 +1,2 @@ test.log -sample +sample/
diff --git a/README b/README @@ -29,8 +29,8 @@ effortless. Start by, 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, - $ hb new -m Downloads/recipe.txt - $ hb new -c WorkingDir/somefile.txt + $ hb new -m . Downloads/recipe.txt + $ hb new -c . WorkingDir/somefile.txt -m does a move operations, and -c does a copy operation.
diff --git a/hb b/hb @@ -1,6 +1,6 @@ #!/bin/sh -# hb - minimalist handbook +# hb - simple notebook manager hb_fatal_error() { echo "hb: " @@ -9,31 +9,34 @@ hb_fatal_error() { } hb_browse() { - cd "${HB_PATH}" - file="$(find . -name '.git*' -prune -o -type f | cut -d"/" -f2- | ${FUZZER})" - [ "${file}" != "" ] && { - echo "${file}" >> ${HB_HIST} - ${EDITOR} "${file}"; - } + cd "${HB_PATH}" + if [ -d "${1}" ] && command -v "${FM}"; then + "${FM}" "${1}" + elif [ -e "${1}" ]; then + "${EDITOR}" "${1}" + fi } -hb_recent() { + +hb_fuzz() { cd "${HB_PATH}" - if [ -f "${HB_HIST}" ]; then - file="$(cat "${HB_HIST}" | tail -n 1)" - test -f "${file}" && { - ${EDITOR} "${file}"; - exit 0; - } - fi - hb_fatal_error "No recent history" + # 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}; } hb_sync() { - which git >/dev/null 2>/dev/null || hb_fatal_error "git not available, cannot sync" - cd "${HB_PATH}" + 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}'" git fetch - git add "${HB_PATH}" + git add . if [ -n "$1" ]; then git commit -m "$1" else @@ -48,15 +51,46 @@ hb_sync() { 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}" +} + hb_new() { - [ -n "$1" ] || hb_fatal_error "usage: hb new < <-c|-m> files... | filename >" - if [ $# -gt 1 ]; then case "$1" in - -c) shift; cp -v "$@" "$HB_PATH/" ;; - -m) shift; mv -v "$@" "$HB_PATH/" ;; - *) [ -n "$1" ] && hb_fatal_error "unknown command -- $@" "usage: hb new <-c|-m> files..." ;; + 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} "${HB_PATH}/$1"; then + if ${EDITOR} "${dest_dir}/$1"; then echo "${file}" >> "${HB_HIST}" fi } @@ -64,19 +98,21 @@ hb_new() { hb_usage() { [ -n "$1" ] && echo "$0: Unknown command $1" printf 'Usage: hb [OPTIONS] - n, new < <-c|-m> files... | filetocreate > + e, edit [file] + Open the file in EDITOR, if it exists + n, new [-c|-m files... | filename] Creates filetocreate 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" - r, recent Open the last file that was accessed h, help Prints this help message ' } -test -z "${EDITOR}" && { export EDITOR=vi; } +[ -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}" @@ -86,14 +122,18 @@ HB_PATH="${HB_PATH:=$XDG_DATA_HOME/notes}" : ${HB_PATH:=$XDG_DATA_HOME/notes} : ${HB_HIST:="$HB_PATH/.hbhistory"} -test -d "${HB_PATH}" || { hb_fatal_error "HB_PATH: ${HB_PATH} is not a directory"; exit 1; } +[ -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 - '') hb_browse ;; + '') file=$(hb_fuzz); hb_browse "${file}" ;; + b) hb_browse "." ;; + e|edit) hb_edit "$@" ;; n|new) hb_new "$@" ;; s|sync) hb_sync "$@" ;; - r|recent) hb_recent ;; h|help) hb_usage ;; *) hb_usage "$@" ;; esac
: nply/hb