-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidea
More file actions
426 lines (383 loc) · 9.33 KB
/
idea
File metadata and controls
426 lines (383 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/bin/bash
# A bash script for quick note/idea taking
# Ideas are stored as files (under the name "x", with x being their index) in the .idea folder, in their respective category, in the home directory: ~/.idea/*
# https://github.com/telos-matter/idea
# 2022_12, HEMMOUDA Aymane
help () {
echo
echo "Quick note/idea taking"
echo
echo "Usage: idea [[-c category] | [-c category] ... | -h | -l [category] [index] | -i [category] [index] | -e [category] index | -r [category] [index]]"
echo
echo "Options:"
echo " -c category"
echo " Prompt the user to record an idea to the specified category until CTRL+D. Note that a line is only recorded after an enter/return"
echo " -c category ..."
echo " Record the passed idea to the specified category"
echo " -h"
echo " Print this help"
echo " -l [category] [index]"
echo " List all ideas of the specified category. Use an ideas' index to list it in its entirety. Use '.' as the category to list all categories"
echo " -i [category] [index]"
echo " Exactly ame as -l, but with information about the ideas and categories"
echo " -e [category] index"
echo " Edit the specified idea of the specified category trough vim."
echo " -r [category] [index]"
echo " Remove the specified category (and thus all of its ideas). Use an ideas' index to remove it alone. Use '.' as the category to remove all categories"
echo
echo "Notes:"
echo " . All options are mutually exclusive"
echo " . The category by default (meaning if omitted) is note"
echo " . If a category doesn't already exist it's created on the go"
echo " . A category name cannot be a number, otherwise it's just interpreted as the index"
echo
echo "Examples:"
echo " Record a short idea in the \`default\` category:"
echo " $ idea this is a quick note"
echo
echo " Record a short idea in the project category:"
echo " $ idea -c project this is an idea for a new project"
echo
echo " Record a longer idea to the journal category:"
echo " $ idea -c journal"
echo
echo " Record a longer idea the \`default\` category:"
echo " $ idea"
echo
echo " List all categories:"
echo " $ idea -l ."
echo
echo " List all ideas of the \`default\` category:"
echo " $ idea -l"
echo
echo " List all ideas of the todo category:"
echo " $ idea -l todo"
echo
echo " List idea #2 of the \`default\` category:"
echo " $ idea -l 2"
echo
echo " List idea #3 of the project category:"
echo " $ idea -l project 3"
echo
echo " Edit idea #1 of the \`default\` category:"
echo " $ idea -e 1"
echo
echo " Edit idea #10 of the thought category:"
echo " $ idea -e thought 10"
echo
echo " Remove all categories, and thus all their ideas: " # Time.set(1500)
echo " $ idea -r ."
echo
echo " Remove all ideas of the \`default\` category:"
echo " $ idea -r"
echo
echo " Remove idea #1 of the todo category:"
echo " $ idea -r todo 1"
echo
echo " Remove the journal category and thus all of its ideas:"
echo " $ idea -r journal"
echo
echo "Repo: https://github.com/telos-matter/idea"
echo
}
DIR="$HOME/.idea"
CAT='note' # Category by default
COUNT=-1
initDIR () {
if [ ! -d "$DIR/$CAT" ]
then
mkdir -p "$DIR/$CAT"
fi
}
updateCOUNT () {
COUNT=`ls "$DIR/$CAT" | wc -l`
}
incCOUNT () {
COUNT=$(( $COUNT +1 ))
}
validateCAT () {
if [[ "$CAT" =~ ^[0-9]+$ ]]
then
>&2 echo 'ERROR: A category name be a number'
exit
fi
}
recordInput () {
initDIR
updateCOUNT
incCOUNT
echo "Recording [$CAT : $COUNT].."
cat > "$DIR/$CAT/$COUNT"
echo "Recorded `wc -l < $DIR/$CAT/$COUNT | sed 's/ //g'` line(s) in [$CAT : $COUNT]"
}
recordIdea () {
initDIR
updateCOUNT
incCOUNT
for ARG in "$@"
do
echo -n "$ARG " >> "$DIR/$CAT/$COUNT"
shift 1
done
echo "Recorded [$CAT : $COUNT]"
}
ideaInfo () {
echo
printf '%s\n' "`cat "$DIR/$CAT/$1"`"
LINE=`ls -l $DIR/$CAT/$1`
echo
echo "Last modified: `echo "$LINE" | rev | cut -d ' ' -f 2,3,4 | rev`"
echo "Size in bytes: `echo "$LINE" | rev | cut -d ' ' -f 5 | rev`"
echo "Number of lines: `wc -l < $DIR/$CAT/$1 | sed 's/ //g'`"
echo "Number of words: `wc -w < $DIR/$CAT/$1 | sed 's/ //g'`"
echo "Number of characters: `wc -m < $DIR/$CAT/$1 | sed 's/ //g'`"
echo
}
noIndexError () {
>&2 echo 'ERROR: An integer index should be specified.'
exit
}
rmIdea () {
read -p "Are you sure you want to remove [$CAT : $1]? (Y/n)" ANSWER
if [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ]
then
initDIR
updateCOUNT
if [ $1 -eq 0 ]
then
exit
elif [ $1 -lt $COUNT ]
then
for (( i = $1; i < $COUNT; i++ ))
do
mv -f "$DIR/$CAT/$(( $i +1 ))" "$DIR/$CAT/$i"
done
else
rm "$DIR/$CAT/$1"
fi
echo "Removed [$CAT : $1]"
fi
}
# -------------------------------------------------------------------
initDIR
if [ $# -gt 0 ]
then
if [ "$1" == "-c" ] || [ "$1" == "-C" ] # Recording in a cat
then
if [ $# -eq 1 ]
then
>&2 echo 'ERROR: The category was not specified'
exit
else
CAT="$2"
validateCAT
if [ $# -eq 2 ]
then
recordInput
else
shift 2
recordIdea "$@"
fi
fi
elif [ "$1" == "-h" ] || [ "$1" == "-H" ] # Help
then
help
elif [ "$1" == "-l" ] || [ "$1" == "-L" ] # List
then
if [ $# -gt 1 ]
then
if [ "$2" == "." ]
then
echo
ls -l "$DIR" | grep '^d' | rev | cut -d ' ' -f 1 | rev
echo
exit
elif [[ "$2" =~ ^[0-9]+$ ]]
then
echo
printf '%s\n' "`cat "$DIR/$CAT/$2"`"
echo
exit
fi
CAT="$2"
fi
if [ $# -gt 2 ]
then
echo
printf '%s\n' "`cat "$DIR/$CAT/$3"`"
echo
else
initDIR
updateCOUNT
if [ $COUNT -ne 0 ]
then
echo
for (( I = 1; I <= $COUNT; I++ ))
do
NUM=`wc -l < $DIR/$CAT/$I`
if [ $NUM -gt 1 ]
then
echo "#$I "`head -n 1 "$DIR/$CAT/$I"`".. and $(( $NUM -1 )) more line(s)"
else
echo "#$I "`cat "$DIR/$CAT/$I"`
fi
done
echo
else
echo
echo "No $CAT"'s. Think of something!'
echo
fi
fi
elif [ "$1" == "-i" ] || [ "$1" == "-I" ] # Info
then
if [ $# -gt 1 ]
then
if [ "$2" == "." ]
then
FMT='%-14s| %-14s| %-12s| %-13s\n'
NAME='Category name'
LAST='Last modified'
NUM='Ideas count'
SIZE='Size (bytes)'
echo
printf "$FMT" "$NAME" "$LAST" "$NUM" "$SIZE"
CATS_COUNT=0
IDEAS_COUNT=0
SIZE_COUNT=0
OLD="$IFS"
IFS='
'
for LINE in `ls -l "$DIR" | grep '^d'`
do
NAME=`echo "$LINE" | rev | cut -d ' ' -f 1 | rev`
LAST=`echo "$LINE" | rev | cut -d ' ' -f 2,3,4 | rev`
NUM=`ls "$DIR/$NAME" | wc -l | sed 's/ //g'`
SIZE=`echo "$LINE" | rev | cut -d ' ' -f 5 | rev`
printf "$FMT" "$NAME" "$LAST" "$NUM" "$SIZE"
CATS_COUNT=$(( $CATS_COUNT +1 ))
IDEAS_COUNT=$(( $IDEAS_COUNT + $NUM ))
SIZE_COUNT=$(( $SIZE_COUNT + $SIZE ))
done
IFS="$OLD"
echo
echo "Total number of categories: $CATS_COUNT"
echo "Total number of ideas: $IDEAS_COUNT"
echo "Total size in bytes: $SIZE_COUNT"
echo
exit
elif [[ "$2" =~ ^[0-9]+$ ]]
then
ideaInfo $2
exit
fi
CAT="$2"
fi
if [ $# -gt 2 ]
then
ideaInfo $3
else
initDIR
updateCOUNT
if [ $COUNT -ne 0 ]
then
MAX=30
FMT='%-6s| %-'"$MAX"'s| %-14s| %-13s| %-13s\n'
INDEX='Index'
CONTENT='Truncated content'
LAST='Last modified'
LINES='Lines number'
SIZE='Size (bytes)'
echo
printf "$FMT" "$INDEX" "$CONTENT" "$LAST" "$LINES" "$SIZE"
SIZE_COUNT=0
for (( I = 1; I <= $COUNT; I++ ))
do
CONTENT=`head -n 1 $DIR/$CAT/$I`
if [ ${#CONTENT} -gt $(( $MAX -3)) ]
then
CONTENT=${CONTENT:0:$(( $MAX -3))}'..'
fi
LAST=`ls -l $DIR/$CAT/$I | rev | cut -d ' ' -f 2,3,4 | rev`
LINES=`wc -l < $DIR/$CAT/$I | sed 's/ //g'`
SIZE=`ls -l $DIR/$CAT/$I | rev | cut -d ' ' -f 5 | rev`
printf "$FMT" "$I" "$CONTENT" "$LAST" "$LINES" "$SIZE"
SIZE_COUNT=$(( $SIZE_COUNT + $SIZE ))
done
echo
echo "Total size in bytes: $SIZE_COUNT"
echo
else
echo
echo "0 ideas in $CAT"
echo
fi
fi
elif [ "$1" == "-e" ] || [ "$1" == "-E" ] # Edit
then
if [ $# -eq 1 ]
then
noIndexError
elif [[ "$2" =~ ^[0-9]+$ ]]
then
vim "$DIR/$CAT/$2"
echo "Edited [$CAT : $2]"
else
if [[ "$3" =~ ^[0-9]+$ ]]
then
vim "$DIR/$2/$3"
echo "Edited [$2 : $3]"
else
noIndexError
fi
fi
elif [ "$1" == "-r" ] || [ "$1" == "-R" ] # Remove
then
if [ $# -gt 1 ]
then
if [ "$2" == "." ]
then
CATS_COUNT=0
IDEAS_COUNT=0
OLD="$IFS"
IFS='
'
for NAME in `ls -l "$DIR" | grep '^d' | rev | cut -d ' ' -f 1 | rev`
do
NUM=`ls "$DIR/$NAME" | wc -l`
IDEAS_COUNT=$(( $IDEAS_COUNT + $NUM ))
CATS_COUNT=$(( $CATS_COUNT +1 ))
done
IFS="$OLD"
read -p "Are you sure you want to remove all $CATS_COUNT categorie(s) with all their $IDEAS_COUNT idea(s)? (Y/n)" ANSWER
if [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ]
then
rm -rf "$DIR/"*
echo 'Removed all'
fi
exit
elif [[ "$2" =~ ^[0-9]+$ ]]
then
rmIdea $2
exit
fi
CAT="$2"
fi
if [ $# -gt 2 ]
then
rmIdea $3
else
initDIR
read -p "Are you sure you want to remove the $CAT category and thus all `ls "$DIR/$CAT" | wc -l | sed 's/ //g'` of its idea(s)? (Y/n)" ANSWER
if [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ]
then
rm -rf "$DIR/$CAT"
echo "Removed the $CAT category"
fi
fi
else # Record in default cat
recordIdea "$@"
fi
else # Record input in default cat
recordInput
fi