notes - notes - a console notes manager using git
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR LICENSE
---
notes (3372B)
---
1 #!/bin/sh
2
3 : ${REPO:=~/notes}
4
5 # search for a suitable editor
6 [ -z "$EDITOR" ] && \
7 for e in nvim vim emacs vi ed; do
8 if command -v $e >/dev/null 2>&1; then
9 EDITOR=$e
10 break
11 fi
12 done
13
14 if [ -z "$EDITOR" ]; then
15 printf 'Could not elect an editor. ED is the standard text editor!\n'
16 EDITOR=ed
17 exit 1
18 fi
19
20 usage() {
21 name=$(basename $0)
22 printf '%s\n' \
23 "$name [ls] | init | last | [history|cat|rm] PATH" \
24 "$name [ls]" \
25 ': show hierarchy tree' \
26 "$name init" \
27 ': initialize the git repository' \
28 "$name last [limit]" \
29 ': show files ordered by edition time' \
30 "$name history PATH" \
31 ': uses tig to display file history' \
32 "$name cat PATH" \
33 ': output content of the file' \
34 "$name rm PATH" \
35 ': delete file' \
36 "$name [edit] PATH" \
37 ": start \$EDITOR ($EDITOR) on file and auto commit"
38 exit 0
39 }
40
41 # display a file tree of notes taken
42 display() {
43 if type colortree >/dev/null 2>&1
44 then
45 colortree -C --prune --noreport "$REPO"
46 exit 0
47 else
48 cd "$REPO"
49 find . -name '.git' -prune -o -type f -print
50 exit 0
51 fi
52 }
53
54 # edit a file given as parameter
55 edit() {
56 cd "$REPO"
57 if [ ! -d "$1" ]
58 then
59 mkdir -p "$(dirname "$1")"
60 "$EDITOR" "$1"
61 if [ -f "$1" ]
62 then
63 git add "$1"
64 git commit -m "editing by $USER" "$1"
65 fi
66 exit 0
67 else
68 printf 'Aborting: "%s" is a directory.\n' "$1"
69 exit 1
70 fi
71 }
72
73 # show file history using tig program
74 histo() {
75 cd "$REPO"
76 if [ -f "$1" ]
77 then
78 if type tig >/dev/null 2>&1
79 then
80 tig "$1"
81 exit 0
82 else
83 printf 'Aborting: tig software is needed for history\n'
84 exit 6
85 fi
86 else
87 printf 'Aborting: "%s" file does not exist.\n' "$1"
88 exit 5
89 fi
90 }
91
92 # output the content of a file
93 show_file() {
94 cd "$REPO"
95 cat "$1"
96 exit 0
97 }
98
99 # delete a file and commit in git
100 delete() {
101 cd "$REPO"
102 if [ -f "$1" ];
103 then
104 git rm "$1"
105 git commit -m "deleted by $USER" "$1"
106 exit 0
107 else
108 printf 'Aborting: "%s" is a directory.\n' "$1"
109 exit 1
110 fi
111 }
112
113 # display the list of edited files ordered by time
114 last() {
115
116 cd "$REPO"
117
118 if [ -n "$1" ]; then
119 limit="$1"
120 else
121 limit=10
122 fi
123
124 git log --date=relative --name-only | \
125 awk '
126 /^commit / {
127 date="no"
128 next
129 }
130
131 /^Date/ {
132 date=substr($0,index($0,$2))
133 getline
134 for(s="x";s!="";) {
135 getline
136 s=$0
137 }
138 next
139 }
140
141 {
142 if(date!="no" &&
143 substr($0,0,1)!=" " &&
144 length($0)>1)
145 {
146 seen[$0]++
147 if(seen[$0]==1) {
148 print date" "$0
149 }
150 }
151 }' | head -n "$limit"
152 exit 0
153 }
154
155 # raw list of files for completion
156 _completion_list() {
157 if [ -d "$REPO" ]
158 then
159 cd "$REPO"
160 find ./${1} -name '.git' -prune -o -type f -print | sed 's,^\./,,'
161 exit 0
162 else
163 printf 'Aborting: "%s" does not exist.\n' "$REPO"
164 exit 4
165 fi
166 }
167
168 # create a git repository
169 initialization() {
170 cd "$REPO"
171 if [ -d .git ]
172 then
173 echo "Git already initialized"
174 exit 3
175 else
176 git init
177 exit 0
178 fi
179 }
180
181 if ! mkdir -p "$REPO"
182 then
183 printf 'Aborting: cannot create "%s".\n' "$REPO"
184 exit 2
185 fi
186
187 case "$1" in
188 '') display ;;
189 help) usage;;
190 ls) display ;;
191 init) initialization ;;
192 last) last "$2" ;;
193 e*) [ -n "$2" ] && edit "$2" ;;
194 hi*) [ -n "$2" ] && histo "$2" ;;
195 r*) [ -n "$2" ] && delete "$2" ;;
196 c*) [ -n "$2" ] && show_file "$2" ;;
197 _files) _completion_list "$2" ;;
198 esac
199
200 # if parameter doesn't match a command, it may be a file
201 edit "$1"