]> Sergey Matveev's repositories - t.git/blob - t
Trivial style fixes
[t.git] / t
1 #!/usr/bin/env zsh
2 # t -- simple notes manager
3 # Copyright (C) 2013-2021 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     NOTE=($NOTES_DIR/*(.on[$(( $1 + 1 ))]))
40     [[ ${#NOTE} -eq 0 ]] && { print note not found >&2 ; exit 1 }
41     NOTE=${NOTE[1]}
42 }
43
44 [[ $# -gt 0 ]] || {
45     purge
46     local ctr=0
47     for note ($NOTES_DIR/*(.on)) {
48         read line < $note
49         print -n "[$ctr] ${line[1,70]} "
50         [[ ${#line} -le 70 ]] || print -n "... "
51         lines=$(wc -l < $note)
52         printf "(%d)\n" $lines
53         ctr=$(( ctr + 1 ))
54     }
55     exit
56 }
57
58 case $1 in
59 (a)
60     note=$NOTES_DIR/$(date "+%Y%m%d-%H%M%S")
61     [[ $# -gt 1 ]] && print -- ${@[2,-1]} > $note || $EDITOR $note
62     ;;
63 (d) get_note $2 ; rm -f $NOTE ;;
64 (m) get_note $2 ; $EDITOR $NOTE ;;
65 (*) get_note $1 ; cat $NOTE ;;
66 esac
67
68 purge