-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiary
More file actions
executable file
·86 lines (77 loc) · 1.79 KB
/
diary
File metadata and controls
executable file
·86 lines (77 loc) · 1.79 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
#!/usr/bin/env bash
# global tnutil variables
tags_dir=${TNU_TAG_DIR:-$HOME/tags}
store_dir=${TNU_STORE_DIR:-$HOME/store}
date=$(date -I)
today=$store_dir/$date
err() {
printf 'an error occured: %s' "$1"
exit 1
}
parseargs() {
while [ $# -gt 0 ]
do
arg=$1
case $arg in
# handle options
-s|--store-dir)
store_dir=$2
test -d "$store_dir" ||
err "$arg must specify a valid directory"
shift
;;
-t|--tags-dir)
tags_dir=$2
test -d "$tags_dir" ||
err "$arg must specify a valid directory"
shift
;;
# treat all other flags as basic switches and set a new variable
# with the same name as the switch to the value of "true"
# do this for both short and long options
-?)
opt="${arg:1}";
printf -v "$opt" '%s' "true" 2> /dev/null ||
err "Invalid switch: $arg" # printf will likely only fail if
# $opt is an invalid variable name
;;
--*)
opt="${arg:2}"
printf -v "$opt" '%s' "true" 2> /dev/null ||
err "Invalid switch: $arg"
;;
@*) tags+=("${arg:1}") ;;
*) err "Invalid arg: $arg" ;;
esac
shift
done
}
# Script-specific variables
diary_file="$today/today.md"
tags=("diary")
# Flags
v=
parseargs "$@"
[ "$v" ] && printf 'args passed: %s\n' "$*"
[ -d "$today" ] || mkdir -p "$today"
if [ -f "$diary_file" ]; then
$VISUAL "$diary_file"
else
touch "$diary_file"
[ -d "$tags_dir/diary" ] || mkdir -p "$tags_dir/diary"
$VISUAL "$diary_file"
fi
for tag in "${tags[@]}"
do
[ -d "$tags_dir/$tag" ] || (
mkdir -p "$tags_dir/$tag" &&
[ "$v" ] &&
printf 'tag "%s" does not exist\n Creating tag directory: %s\n' "$tag" "$tags_dir/$tag"
)
link="$tags_dir/$tag/$date.md"
[ -h "$link" ] || (
ln -sr "$diary_file" "$link" &&
[ "$v" ] &&
printf 'Creating symlink for tag: %s\n' "$tag"
)
done