From f2c0d434a366fdfb2db2548507fb943c409ce57f Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 17 Nov 2024 03:48:51 +0000 Subject: [PATCH] Initial version of the notes.sh rewrite --- LICENSE | 11 +++ README.md | 52 ++++++++++++++ notes.sh | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 notes.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b52357f --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2023 Konstantin Nazarov + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc2ecdc --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# Plain-text note taking system + +This is a script that helps you with taking notes and looking them up later. +It uses a simple system of putting notes in individual directories under `~/Notes`. +The directory names look like `20241116091658-some_note`, and each contains at least +one `note.md` file. + +The script doesn't have any user interface itself, but relies on a combination of +[fzf](https://github.com/junegunn/fzf) and your default `$EDITOR`. + +The previous iteration of this tool used to store notes in MIME envelopes, but that +created problems with opening the same note simultaneously from multiple places. So +this version uses plain directories. + +## Features + +- No need to manually assign directory names (they are chosen for you and contain the note title) +- Creating a new note is instant +- Search by title is very fast / optimized. Even if you have thousands of notes +- All notes have a unique ID which you can use to link between notes +- The code is written in bash with standard Unix tools, and is easy to read + +## Getting started + +- Put `notes.sh` file from this repo somewhere to your `$PATH` +- Install [fzf](https://github.com/junegunn/fzf) + +Then add the following aliases to your `~/.bashrc`: + +```bash +# Type "nn" to create a new note +alias nn="notes.sh -n" + +# Type "ne" to look up and edit an existing note +alias ne="notes.sh -l | fzf --tac --with-nth=\"2..-1\" | xargs -o notes.sh -e" +``` + +To add a new note: + +```bash +nn This is my new note +``` + +To open a previously created note: + +```bash +ne +``` + +## License + +Distributed under the terms of the BSD License diff --git a/notes.sh b/notes.sh new file mode 100755 index 0000000..a70991f --- /dev/null +++ b/notes.sh @@ -0,0 +1,211 @@ +#!/usr/bin/env bash + +set -e + +DATE_FORMAT="%a, %d %b %Y %H:%M:%S %z" +BASEDIR=~/Notes +EDITOR="${EDITOR:-vim}" + +if [ ! -d "$BASEDIR" ]; then + mkdir -p "$BASEDIR" +fi + +die() { + echo "$@" 1>&2; + exit 1 +} + +uuid() +{ + local N B T + + for (( N=0; N < 16; ++N )) + do + B=$(( RANDOM%255 )) + + if (( N == 6 )) + then + printf '4%x' $(( B%15 )) + elif (( N == 8 )) + then + local C='89ab' + printf '%c%x' ${C:$(( RANDOM%${#C} )):1} $(( B%15 )) + else + printf '%02x' $B + fi + + for T in 3 5 7 9 + do + if (( T == N )) + then + printf '-' + break + fi + done + done + + echo +} + +canonicalize() { + NAME="$1" + NAME="$(echo "$NAME" | tr '[:upper:]' '[:lower:]')" + NAME="$(echo "$NAME" | sed 's/[^a-z0-9]/_/g')" + + echo "$NAME" +} + +utc_timestamp() { + date -u +"%Y%m%d%H%M%S" +} + +find_file_by_id() { + ID="$1" + + { grep -l -r -m1 "^X-Note-Id: $ID$" "$BASEDIR" || true; } | sort| head -1 +} + +assert_find_file_by_id() { + FILE="$(find_file_by_id "$1")" + + if [ ! -f "$FILE" ]; then + die "Note with ID <$ID> not found" + fi + + echo "$FILE" +} + +list_entries() { + FILTER="\ + BEGIN { \ + message_id=0; \ + subject=0; \ + date=0; \ + } \ + match(\$0, /^X-Note-Id: .*$/) { \ + if (message_id != 0) { \ + if (subject !=0 && date != 0)\ + print date, message_id, subject; \ + subject = 0; \ + date = 0; \ + };\ + message_id = substr(\$0, 12, RLENGTH-11); \ + } \ + match(\$0, /^Subject: .*$/) { \ + if (subject != 0) { \ + if (message_id != 0 && date != 0)\ + print date, message_id, subject; \ + message_id = 0; \ + date = 0; \ + }; \ + subject = substr(\$0, 10, RLENGTH-9); \ + } \ + match(\$0, /^X-Date: .*$/) { \ + if (date != 0) { \ + if (message_id != 0 && subject != 0)\ + print date, message_id, subject; \ + subject = 0; \ + message_id = 0; \ + }; \ + date = substr(\$0, 9, RLENGTH-8); \ + } \ + END { \ + if (message_id != 0 && subject != 0 && date != 0)\ + print date, message_id, subject;\ + }\ + " + + grep -r -h "^Subject:\|^X-Note-Id:\|^X-Date:" "$BASEDIR" | awk "$FILTER" | sort | cut -d " " -f "2-" +} + + +new_entry() { + NAME="$1" + CANONICAL_NAME="$(canonicalize "$NAME")" + FULL_NAME="$(utc_timestamp)-$CANONICAL_NAME" + NOTE_ID="$(uuid)" + + NOTE_DIR="$BASEDIR/$FULL_NAME" + NOTE_FILE="$NOTE_DIR/note.md" + + mkdir -p "$NOTE_DIR" + + TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + + { + echo "X-Date: $TIMESTAMP" + echo "X-Note-Id: $NOTE_ID" + echo "Subject: $NAME" + echo + } >> "$NOTE_FILE" + + $EDITOR "$NOTE_FILE" +} + +edit_entry() { + ID="$1" + FILENAME="$(assert_find_file_by_id "$ID")" + + echo "$FILENAME" + + $EDITOR "$FILENAME" +} + +export_note() { + ID="$1" + FILENAME="$(assert_find_file_by_id "$ID")" + + DIR="$2" + if [ -z "$DIR" ]; then + cat "$FILENAME" + else + die "Directory export not implemented" + fi +} + + +usage() { + echo "$0 {--new,--list,--edit,--help}" +} + +while (( "$#" )); do + case "$1" in + -n|--new) + shift + new_entry "$*" + exit 0 + ;; + -l|--list) + list_entries + exit 0 + ;; + -e|--edit) + if [ -z "$2" ]; then + echo "Misssing argument for $1" + exit 1 + fi + edit_entry "$2" + exit 0 + ;; + -E|--export) + if [ -z "$2" ]; then + echo "Misssing arguments for $1" + exit 1 + fi + export_note "$2" "$3" + exit 0 + ;; + -g|--graph) + get_graph + exit 0 + ;; + *) + usage + exit 1 + ;; + esac + +done + +usage +exit 1