]> Sergey Matveev's repositories - t.git/blobdiff - t
Trivial style fixes
[t.git] / t
diff --git a/t b/t
index 4dec0a51c17846e82355656ede9dd57dafc0d7fb..1695e883a544dd14eb2ccb0e8bd94e9f65732a7e 100755 (executable)
--- a/t
+++ b/t
@@ -1,50 +1,69 @@
-#!/bin/sh -e
+#!/usr/bin/env zsh
 # t -- simple notes manager
-# Copyright (C) 2013-2019 Sergey Matveev <stargrave@stargrave.org>
-# Invoke the script without any arguments to briefly print all notes.
-# Otherwise you can specify the following ones:
-# a   -- add new note (either starts an editor if not arguments are specified,
-#        or save them inside the note silently)
-# d N -- delete note N
-# m N -- modify note N by starting an editor
-# N   -- print out note's N contents
+# Copyright (C) 2013-2024 Sergey Matveev <stargrave@stargrave.org>
+# Current version is written on Z shell. Previous was on POSIX shell.
+#
+# Usage:
+# * t -- just briefly print all notes: their number and stripped first
+#   line of content
+# * t N -- print N note's contents
+# * t a [X X X] -- add a new note to the end. If arguments are specified
+#   then they will be the content. Otherwise $EDITOR is started
+# * t d N -- delete note number N. Pay attention that other notes may
+#   change their numbers!
+# * t m N -- edit note N with $EDITOR
+# Also you can specify $N environment variable that acts like some kind
+# of namespace for the notes (prepare directory first!). For example:
+#     $ N=work t a get job done
+#     $ N=work t a # it starts $EDITOR
+#     $ N=work t
+#     [0] get job done (1)
+#     [1] this is first line of 3-lines comment (3)
+#     $ N=work t d 0
+#     $ N=work t
+#     [0] this is first line of 3-lines comment (3)
+#     $ t
+#     [0] some earlier default namespace note (1)
 
+setopt ERR_EXIT NULL_GLOB
 NOTES_DIR=$HOME/.t/$N
+NOTES_DIR=${NOTES_DIR%/}
 
-purge()
-{
-    find $NOTES_DIR -size 0 -delete
+purge() {
+    local empties=($NOTES_DIR/*(.L0))
+    [[ $empties ]] && rm $empties || :
 }
 
-get_note()
-{
-    find $NOTES_DIR -maxdepth 1 -type f | sort | sed -n $(($1 + 1))p
+get_note() {
+    [[ "$1" = [0-9]* ]] || { print invalid note id ; exit 1 }
+    NOTE=($NOTES_DIR/*(.on[$(( $1 + 1 ))]))
+    [[ ${#NOTE} -eq 0 ]] && { print note not found >&2 ; exit 1 }
+    NOTE=${NOTE[1]}
 }
 
-if [ -z "$1" ]; then
+[[ $# -gt 0 ]] || {
     purge
-    cnt=0
-    for n in $(find $NOTES_DIR -maxdepth 1 -type f | sort); do
-        echo "[$cnt]" "$(sed 's/^\(.\{1,70\}\).*$/\1/;q' $n)" "($(sed -n '$=' $n))"
-        cnt=$(($cnt + 1))
-    done
-    exit 0
-fi
+    local ctr=0
+    for note ($NOTES_DIR/*(.on)) {
+        read line < $note
+        print -n "[$ctr] ${line[1,70]} "
+        [[ ${#line} -le 70 ]] || print -n "... "
+        lines=$(wc -l < $note)
+        printf "(%d)\n" $lines
+        (( ctr = ctr + 1 ))
+    }
+    exit
+}
 
-case "$1" in
-a)
-    shift
-    note=$NOTES_DIR/$(date '+%s')
-    [ $# -gt 0 ] && echo "$@" > $note || $EDITOR $note
-    ;;
-d)
-    rm -f $(get_note $2)
-    ;;
-m)
-    $EDITOR $(get_note $2)
-    ;;
-*)
-    cat $(get_note $1)
+case $1 in
+(a)
+    zmodload -F zsh/datetime b:strftime
+    note=$NOTES_DIR/$(strftime %Y%m%d-%H%M%S)
+    [[ $# -gt 1 ]] && print -- ${@[2,-1]} > $note || $EDITOR $note
     ;;
+(d) get_note $2 ; rm -f $NOTE ;;
+(m) get_note $2 ; $EDITOR $NOTE ;;
+(*) get_note $1 ; cat $NOTE ;;
 esac
+
 purge