From 569d457db69589892d45f225384ffcde5beaa953 Mon Sep 17 00:00:00 2001 From: thek4n Date: Tue, 5 Nov 2024 10:44:21 +0300 Subject: [PATCH] add script t for fast notes --- TARGETS.sh | 2 +- home/user/.local/bin/t | 118 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100755 home/user/.local/bin/t diff --git a/TARGETS.sh b/TARGETS.sh index ef1b07f..7c5fa3e 100644 --- a/TARGETS.sh +++ b/TARGETS.sh @@ -1,7 +1,7 @@ readonly TARGETS="\ colors:.config/terminal-colors.d tmux:.config/tmux .tmux .config/systemd/user/tmux.service .local/bin/tmux_start_session.sh .local/bin/tmux_list_sessions.sh .local/bin/tmux_attach_session.sh .local/bin/tmux_kill_sessions.sh -zsh:.config/zsh .zshenv .inputrc %colors +zsh:.config/zsh .zshenv .inputrc .local/bin/t %colors alacritty:.config/alacritty nvim:.config/nvim .editorconfig .inputrc .editrc .local/bin/vim_askpass_helper ssh: diff --git a/home/user/.local/bin/t b/home/user/.local/bin/t new file mode 100755 index 0000000..5b78f7c --- /dev/null +++ b/home/user/.local/bin/t @@ -0,0 +1,118 @@ +#!/bin/sh + +set -o errexit +set -o nounset + + +readonly EXIT_SUCCESS=0 + +PROGRAM="$(basename "$0")" +readonly PROGRAM + +readonly NOTES_DIR_BASE="${HOME}/.t" + +NOTES_DIR="${NOTES_DIR_BASE}" +if [ -n "${N+x}" ]; then + NOTES_DIR="${NOTES_DIR}/${N}" +fi + +readonly NOTES_DIR + + +die() { + echo "${PROGRAM}: Error: ${1}" 1>&2 + exit "${2:-$EXIT_SUCCESS}" +} + +show_notes_with_indexes() { + SAVEIFS="${IFS}" + IFS=' +' + index=1 + for note in $(find "${NOTES_DIR}" -maxdepth 1 -type f | sort -n) + do + note_lines="$(wc -l < "${note}")" + printf "[%d] %s (%s)\n" "${index}" "$(basename "${note}")" "${note_lines}" + index="$((index+1))" + done + + IFS="${SAVEIFS}" +} + +find_note_name_by_index() { + note_index="${1}" + show_notes_with_indexes | grep "^\[${note_index}\]" | cut -d" " -f2- | rev | cut -d" " -f2- | rev +} + +cmd_add_note() { + note="${NOTES_DIR}/$*" + touch "${note}" + exit "$EXIT_SUCCESS" +} + +cmd_delete_note() { + note_index="${1}" + note="$(find_note_name_by_index "${note_index}")" + note_to_remove="${NOTES_DIR}/${note}" + + if [ ! -f "${note_to_remove}" ]; then + die "Note with index ${note_index} not found" 1 + fi + + rm "${note_to_remove}" + exit "$EXIT_SUCCESS" +} + +cmd_edit_note() { + note_index="${1}" + note="$(find_note_name_by_index "${note_index}")" + note_to_edit="${NOTES_DIR}/${note}" + + if [ ! -f "${note_to_edit}" ]; then + die "Note with index ${note_index} not found" 1 + fi + + $EDITOR "${note_to_edit}" + exit "$EXIT_SUCCESS" +} + +cmd_cat_note() { + note_index="${1}" + note="$(find_note_name_by_index "${note_index}")" + note_to_cat="${NOTES_DIR}/${note}" + + if [ ! -f "${note_to_cat}" ]; then + die "Note with index ${note_index} not found" 1 + fi + + cat "${note_to_cat}" + exit "$EXIT_SUCCESS" +} + + +remove_empty() { + find "${NOTES_DIR_BASE}" -type d -empty -exec rm -r {} \; 2>/dev/null +} + +trap remove_empty EXIT + + +if [ ! -d "${NOTES_DIR}" ]; then + mkdir -p "${NOTES_DIR}" +fi + + +if [ -z "${1+x}" ]; then + show_notes_with_indexes + exit "$EXIT_SUCCESS" +fi + + +case "$1" in + add|a) shift; cmd_add_note "$@" ;; + delete|d) shift; cmd_delete_note "$@" ;; + e|edit) shift; cmd_edit_note "$@" ;; + + *) cmd_cat_note "$@" ;; +esac +exit "$EXIT_SUCCESS" \ No newline at end of file