t script feats

This commit is contained in:
thek4n 2024-11-05 11:38:35 +03:00
parent 882e774bf7
commit 1499c8ff5f

View File

@ -1,19 +1,17 @@
#!/bin/sh #!/bin/sh
set -o errexit set -o errexit
set -o nounset set -o nounset
readonly EXIT_SUCCESS=0 readonly EXIT_SUCCESS=0
PROGRAM="$(basename "$0")"
readonly PROGRAM
readonly NOTES_DIR_BASE="${HOME}/.t" readonly NOTES_DIR_BASE="${HOME}/.t"
NOTES_DIR="${NOTES_DIR_BASE}" NOTES_DIR="${NOTES_DIR_BASE}"
if [ -n "${N+x}" ]; then if [ -n "${NS+x}" ]; then
NOTES_DIR="${NOTES_DIR}/${N}" NOTES_DIR="${NOTES_DIR}/${NS}"
fi fi
readonly NOTES_DIR readonly NOTES_DIR
@ -25,11 +23,11 @@ cmd_help() {
t - Show notes in format '[INDEX] NOTE NAME (LINES)' t - Show notes in format '[INDEX] NOTE NAME (LINES)'
t show - Show notes in format '[INDEX] NOTE NAME (LINES)' t show - Show notes in format '[INDEX] NOTE NAME (LINES)'
t add [X X X] - Add note with name X X X t namespaces - Show namespaces
t add (X X X) - Add note with name X X X
t edit (INDEX) - Edit note with INDEX by \$EDITOR t edit (INDEX) - Edit note with INDEX by \$EDITOR
t delete (INDEX) - Delete note with INDEX t delete (INDEX) - Delete note with INDEX
t namespaces - Show namespaces t --help - Show this message
t help - Show this message
t a - alias to add t a - alias to add
t e - alias to edit t e - alias to edit
@ -38,14 +36,14 @@ cmd_help() {
NAMESPACES NAMESPACES
N=work t a fix bug 211 # add note in workspace 'work' t namespaces # show namespaces
N=work t # show notes in workspace 'work' NS=work t a fix bug 211 # add note in workspace 'work'
" NS=work t # show notes in workspace 'work'"
} }
die() { die() {
echo "${PROGRAM}: Error: ${1}" 1>&2 echo "$(basename "${0}"): Error: ${1}" 1>&2
exit "${2:-$EXIT_SUCCESS}" exit "${2:-$EXIT_SUCCESS}"
} }
@ -119,13 +117,16 @@ cmd_cat_note() {
} }
cmd_show_namespaces() { cmd_show_namespaces() {
find "${NOTES_DIR_BASE}/" -mindepth 1 -type d -exec basename {} \; for ns in $(find "${NOTES_DIR_BASE}/" -mindepth 1 -maxdepth 1 -type d | sort -n)
do
namespace_notes_count="$(find "${ns}" -type f | wc -l)"
printf "%s (%s)\n" "$(basename "${ns}")" "${namespace_notes_count}"
done
} }
remove_empty_namespaces() { remove_empty_namespaces() {
find "${NOTES_DIR_BASE}" -type d -empty -exec rm -r {} \; 2>/dev/null || true find "${NOTES_DIR_BASE}" -type d -empty -exec rm -r {} \; 2>/dev/null || true
} }
trap remove_empty_namespaces EXIT trap remove_empty_namespaces EXIT
@ -140,13 +141,13 @@ if [ -z "${1+x}" ]; then
fi fi
case "$1" in case "${1}" in
show) shift; show_notes_with_indexes ;; show) shift; show_notes_with_indexes ;;
ns|namespaces) shift; cmd_show_namespaces ;; ns|namespaces) shift; cmd_show_namespaces ;;
add|a) shift; cmd_add_note "$@" ;; add|a) shift; cmd_add_note "$@" ;;
delete|d) shift; cmd_delete_note "$@" ;; delete|d) shift; cmd_delete_note "$@" ;;
e|edit) shift; cmd_edit_note "$@" ;; e|edit) shift; cmd_edit_note "$@" ;;
help) shift; cmd_help "$@" ;; --help) shift; cmd_help ;;
*) cmd_cat_note "$@" ;; *) cmd_cat_note "$@" ;;
esac esac