-
Notifications
You must be signed in to change notification settings - Fork 5
/
shv
executable file
·169 lines (151 loc) · 3.17 KB
/
shv
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
#!/bin/sh -e
OS=$(uname)
AUTO_CD=${SHV_AUTO_CD:-"true"}
EXT=${SHV_EXT:-"md"}
DATE_FMT=${SHV_DATE_FMT:-"%Y-%m-%d"}
IGNORE=${SHV_IGNORE:-"./.obsidian/* ./.git/* ./*.pdf"}
JOURNAL_PATH=${SHV_JOURNAL_PATH:-"$SHV_PATH"}
NOTE_PATH=${SHV_PATH:-${XDG_DATA_HOME:-${HOME}/.local/share}/notes}
PICKER=${SHV_PICKER:-"fzf -m --ansi --print-query --bind=alt-enter:print-query"}
SUBCMD=${SHV_DEFAULT_CMD:-"select"}
NORMALIZE_CH=${SHV_NORMALIZE_CH:-"-"}
GREP_CMD=${SHV_GREP_CMD:-"$(type rg >/dev/null 2>/dev/null && echo 'rg -H' || echo 'grep -H')"}
###### UTILITY ######
usage() {
cat <<EOF
$0: illegal subcommand -- $1
usage: shv [FLAGS] [SUBCOMMAND] [ARGS]
more info:
man shv
EOF
exit 0
}
stderr() { printf '%s: %s.\n' "$0" "$1" >&2 && exit 1; }
strip() {
str="${1#"${1%%[![:space:]]*}"}"
str="${str%"${str##*[![:space:]]}"}"
printf "%s\n" "$str"
}
# $1: String
normalize() {
strip "$1" | sed "s/\n/#/g; s/\ /$NORMALIZE_CH/g; s/\n/#/g; s/#/\s/g"
}
# $1: filenames
# $2: directory
parse() {
args=$(normalize "$1")
for i in $args; do
[ -f "$i" ] && file="$file $i" || tmp="$2/$i.$EXT"
done
printf "%s\n" "${file:-$tmp}"
}
# $1: directory
list_files() {
for ign in $IGNORE; do
tmp="$tmp -not -path $ign"
done
eval "find $1 -type f $tmp"
}
# $1: n of days
get_date() {
if [ "$OS" = "Linux" ]; then
date -d "$1 days" +"$DATE_FMT"
else
date -v"$1"d +"$DATE_FMT"
fi
}
###### COMMANDS ######
# $1: n days
# $2: label
journal_note() {
date=$(get_date "$1")
[ -z "$2" ] && NORMALIZE_CH=""
printf "%s/%s%c%s.%s\n" "$JOURNAL_PATH" "$date" "$NORMALIZE_CH" "$2" "$EXT"
}
search_notes() {
sel=$(list_files "$NOTE_PATH" | xargs -I {} $GREP_CMD "" {} | $PICKER | sed "s/:.*//g")
[ -z "$sel" ] && exit 0
parse "$sel" "$NOTE_PATH"
}
# $1: directory to search
select_notes() {
sel=$(list_files "$1" | $PICKER | sed "s/\n/#/g")
[ -z "$sel" ] && exit 0
parse "$sel" "$1"
}
###### MAIN ######
while [ $# -gt 0 ]; do
case $1 in
--auto-cd)
AUTO_CD=true
shift
;;
--no-auto-cd)
AUTO_CD=false
shift
;;
-p | --note-path)
NOTE_PATH="$2"
shift
shift
;;
-j | --journal-path)
JOURNAL_PATH="$2"
shift
shift
;;
-i | --ignore)
IGNORE="$2"
shift
shift
;;
-c | --normalize-ch)
NORMALIZE_CH="$2"
shift
shift
;;
-P | --picker)
picker="$2"
shift
shift
;;
-g | --grep_cmd)
GREP_CMD="$2"
shift
shift
;;
-e | --ext)
EXT="$2"
shift
shift
;;
*)
break
;;
esac
done
case ${1:-$SUBCMD} in
j | journal)
note=$(select_notes "$JOURNAL_PATH")
;;
g | grep)
note=$(search_notes)
;;
s | select)
note=$(select_notes "$NOTE_PATH")
;;
t | today)
note=$(journal_note +0 "$2")
;;
tm | tomorrow)
note=$(journal_note +1 "$2")
;;
y | yesterday)
note=$(journal_note -1 "$2")
;;
*)
usage "$1"
;;
esac
"$AUTO_CD" && cd "$NOTE_PATH"
[ -n "$note" ] && eval "${EDITOR:-vi} $note"