Files
shell/scripts/utils/note
2025-11-14 10:44:57 +08:00

74 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Note taking tool
#
# Usage:
# note [name] - Create a new note with optional name (defaults to "New note")
# note -e|--edit [name] - Edit an existing note by name
#
# Arguments explanation:
# [name] - Note filename (without extension) and title
# -e, --edit [name] - Edit existing note by name
#
# Detailed usage:
# note - Creates "New note" with current timestamp
# note my-idea - Creates note titled "my-idea"
# note my-idea "My Great Idea" - Creates note file "my-idea" but titled "My Great Idea"
# note -e my-idea - Edits existing note with name "my-idea"
#
# Notes are stored as markdown files in ~/notes/ with timestamps
# When multiple notes have the same name, you'll be prompted to select which one to edit
arg1="$1"
arg2="$2"
path="$HOME/notes"
[[ ! -d "$path" ]] && mkdir -p "$path"
shopt -s nullglob
files=("$path"/*.md)
shopt -u nullglob
case "$arg1" in
-e|--edit)
[[ -z "$arg2" ]] && {
echo "Note name is required"
exit 1
}
# shellcheck disable=SC2207
found=($(echo "${files[@]}" | grep -P "[0-9]{10}-$arg2.md"))
[[ ${#found[@]} -eq 0 ]] && {
echo "Note with name '$arg2' not found."
echo "Create it with using 'note $arg2'"
exit
}
[[ ${#found[@]} -eq 1 ]] && {
nano "${found[0]}"
exit
}
PS3="Select a note to edit: "
select selection in "${found[@]}" "Exit"; do
[[ "$selection" == "Exit" ]] && exit
[[ -f "$selection" ]] && {
nano "$selection"
exit
}
continue
done
;;
*)
[[ -z "$arg2" ]] && arg2="${arg1:-New note}"
file="$path/$(date +%s)-$arg1.md"
cat <<EOF > "$file"
# $arg2
Note taken: $(date '+%d.%m.%Y %H:%M:%S')
EOF
nano "$file"
;;
esac