]> Sergey Matveev's repositories - t.git/blob - t
Raised copyright years
[t.git] / t
1 #!/usr/bin/env zsh
2 # t -- simple notes manager
3 # Copyright (C) 2013-2022 Sergey Matveev <stargrave@stargrave.org>
4 # Current version is written on zsh. Previous was on POSIX shell.
5 #
6 # Usage:
7 # * t -- just briefly print all notes: their number and stripped first
8 #   line of content
9 # * t N -- print N note's contents
10 # * t a [X X X] -- add a new note to the end. If arguments are specified
11 #   then they will be the content. Otherwise $EDITOR is started
12 # * t d N -- delete note number N. Pay attention that other notes may
13 #   change their numbers!
14 # * t m N -- edit note N with $EDITOR
15 # Also you can specify $N environment variable that acts like some kind
16 # of namespace for the notes (prepare directory first!). For example:
17 #     $ N=work t a get job done
18 #     $ N=work t a # it starts $EDITOR
19 #     $ N=work t
20 #     [0] get job done (1)
21 #     [1] this is first line of 3-lines comment (3)
22 #     $ N=work t d 0
23 #     $ N=work t
24 #     [0] this is first line of 3-lines comment (3)
25 #     $ t
26 #     [0] some earlier default namespace note (1)
27
28 set -e
29 setopt NULL_GLOB
30 NOTES_DIR=$HOME/.t/$N
31 NOTES_DIR=${NOTES_DIR%/}
32
33 purge() {
34     local empties=($NOTES_DIR/*(.L0))
35     [[ $empties ]] && rm $empties || :
36 }
37
38 get_note() {
39     [[ "$1" = [0-9]* ]] || { print invalid note id ; exit 1 }
40     NOTE=($NOTES_DIR/*(.on[$(( $1 + 1 ))]))
41     [[ ${#NOTE} -eq 0 ]] && { print note not found >&2 ; exit 1 }
42     NOTE=${NOTE[1]}
43 }
44
45 [[ $# -gt 0 ]] || {
46     purge
47     local ctr=0
48     for note ($NOTES_DIR/*(.on)) {
49         read line < $note
50         print -n "[$ctr] ${line[1,70]} "
51         [[ ${#line} -le 70 ]] || print -n "... "
52         lines=$(wc -l < $note)
53         printf "(%d)\n" $lines
54         ctr=$(( ctr + 1 ))
55     }
56     exit
57 }
58
59 case $1 in
60 (a)
61     note=$NOTES_DIR/$(date "+%Y%m%d-%H%M%S")
62     [[ $# -gt 1 ]] && print -- ${@[2,-1]} > $note || $EDITOR $note
63     ;;
64 (d) get_note $2 ; rm -f $NOTE ;;
65 (m) get_note $2 ; $EDITOR $NOTE ;;
66 (*) get_note $1 ; cat $NOTE ;;
67 esac
68
69 purge