From 85464f66b317a9b821d5c6259c64af59579fe114 Mon Sep 17 00:00:00 2001 From: "phansible (aamon)" Date: Thu, 8 May 2025 11:16:38 -0400 Subject: [PATCH] Added new note functions (notez & noteb) More robust notes functions to take quick notes in ZSH and Bash --- .shell_functions | 96 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/.shell_functions b/.shell_functions index 8013c8a..edea35f 100644 --- a/.shell_functions +++ b/.shell_functions @@ -1,30 +1,82 @@ # Shell Functions -# Shell note pad -note () { - # if file doesn't exist, create it - if [[ ! -f $HOME/.notes ]]; then - touch "$HOME/.notes" - fi +# Shell note pad for Bash +noteb () { + local notes_file="$HOME/.notes" + local timestamp=$(date +"%F %T") + local input_line - if ! (($#)); then - # no arguments, print file - cat "$HOME/.notes" + if [[ $# -eq 0 ]]; then + # No arguments, display the notes file + cat "$notes_file" + return 0 + elif [[ "$1" == "-a" ]]; then + # Add a note with prompting (using Bash's read -p) + read -r -p "$(date +"%F %T | ") " input_line + if [[ -n "$input_line" ]]; then + echo "$timestamp | $input_line" >> "$notes_file" + echo "Note added to $notes_file" + return 0 + else + echo "No note entered." + return 1 + fi elif [[ "$1" == "-c" ]]; then - # clear file - echo -n "Clear notes file? [Y/N] " - read replace - if [ "${replace}" = "Y" ] || [ "${replace}" = "y" ]; then - echo "Emptying notes file..." - printf "%s" > "$HOME/.notes" - echo "Done." - else - echo "Notes file preserved." - fi + # Clear the notes file + read -r -p "Clear notes file? [Y/n] " clear_response + if [[ "$clear_response" =~ ^[Yy]$ ]]; then + > "$notes_file" # Truncate the file + echo "Notes file cleared." + else + echo "Notes file not cleared." + fi + return 0 else - # add all arguments to file - echo -n $(date +"%F %T | ") >> "$HOME/.notes" - printf "%s\n" "$*" >> "$HOME/.notes" + # Treat arguments as the note (default action if not -a or -c) + input_line="$*" + echo "$timestamp | $input_line" >> "$notes_file" + echo "Note added to $notes_file" + return 0 + fi +} + +# Shell Note Pad for ZSH +notez () { + local notes_file="$HOME/.notes" + local timestamp=$(date +"%F %T") + local input_line + + if [[ $# -eq 0 ]]; then + # No arguments, display the notes file + cat "$notes_file" + return 0 + elif [[ "$1" == "-a" ]]; then + # Add a note with prompting + read -r "?$(date +"%F %T | ") " input_line + if [[ -n "$input_line" ]]; then + echo "$timestamp | $input_line" >> "$notes_file" + echo "Note added to $notes_file" + return 0 + else + echo "No note entered." + return 1 + fi + elif [[ "$1" == "-c" ]]; then + # Clear the notes file + read -r "?Clear notes file? [Y/n] " clear_response + if [[ "$clear_response" =~ ^[Yy]$ ]]; then + > "$notes_file" # Truncate the file + echo "Notes file cleared." + else + echo "Notes file not cleared." + fi + return 0 + else + # Treat arguments as the note (default action if not -a or -c) + input_line="$*" + echo "$timestamp | $input_line" >> "$notes_file" + echo "Note added to $notes_file" + return 0 fi }