Initial version of the notes.sh rewrite
This commit is contained in:
commit
f2c0d434a3
3 changed files with 274 additions and 0 deletions
11
LICENSE
Normal file
11
LICENSE
Normal file
|
@ -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.
|
52
README.md
Normal file
52
README.md
Normal file
|
@ -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
|
211
notes.sh
Executable file
211
notes.sh
Executable file
|
@ -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
|
Loading…
Reference in a new issue