forked from markwu/todo-cli-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view
executable file
·221 lines (196 loc) · 7.03 KB
/
view
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
#!/bin/bash
# Stop Verbose lines, thanks to Mark Harrison
TODOTXT_VERBOSE=0
# Get action
action=$1
shift
# Get option
option=$1;
shift
# Get rest of them
term="$@"
function usage() {
echo " $(basename $0) [OPTION] [TERM]"
echo ""
echo " Show todo items containing TERM, grouped by OPTION, and displayed in"
echo " priority order. If no TERM provided, displays entire todo.txt."
echo ""
echo " Usage"
echo " \$ todo.sh $(basename $0) [OPTION] [TERM]"
echo ""
echo " Options"
echo " project Show todo items group by project"
echo " context Show todo items group by context"
echo " date Show todo items group by date"
echo " nodate Show todo items group by date without date"
echo " past Show todo items group by date from today to past"
echo " future Show todo items group by date from today to future"
echo " today Show todo items group by date only today"
echo " yesterday Show todo items group by date from today to yesterday"
echo " tomorrow Show todo items group by date from today to tomorrow"
echo " ?length Show todo items group by date from today to ?length"
echo " ? could be signed(+-) or unsigned numbers."
echo " Length could be (days|weeks|months|years)"
echo ""
echo " Examples"
echo " \$ todo.sh $(basename $0) project # Show todo items grouped by project"
echo " \$ todo.sh $(basename $0) project @context # Show todo items grouped by project and filtered by @context"
echo " \$ todo.sh $(basename $0) context # Show todo items grouped by context"
echo " \$ todo.sh $(basename $0) date +project # Show todo items grouped by date and filtered by +project"
echo " \$ todo.sh $(basename $0) -3days # Show todo items grouped by date from today to 3days before today"
echo " \$ todo.sh $(basename $0) 4weeks # Show todo items grouped by date from today to 4weeks after today"
echo ""
exit
}
function project_view() {
# Show projects in alphabetical order and todo items in priority order
echo "===== Projects ====="
echo ""
# Find all projects and sort
PROJECTS=$(grep -o '[^ ]*+[^ ]\+' "$TODO_FILE" | grep '^+' | sort -u | sed 's/^+//g')
# For each project show header and the list of todo items
for project in $PROJECTS; do
# Use core _list function, does numbering and colouring for us
PROJECT_LIST=$(_list "$TODO_FILE" "+$project\b" "$term" | sed 's/\(^+\|\ *+\)[a-zA-Z0-9\{._\-\}]*\ */ /g')
if [[ -n "${PROJECT_LIST}" ]]; then
echo "--- $project ---"
echo "${PROJECT_LIST}"
echo ""
fi
done
# Show todo items not associated to a project
PROJECT_LIST=$(_list "$TODO_FILE" "$term" | grep -v "+\w")
if [[ -n "${PROJECT_LIST}" ]]; then
echo "--- Items without project ---"
echo "${PROJECT_LIST}"
fi
}
function context_view() {
# Show contexts in alphabetical order and todo items in priority order
echo "===== Contexts ====="
echo ""
# Find all contexts and sort
CONTEXTS=$(grep -o '[^ ]*@[^ ]\+' "$TODO_FILE" | grep '^@' | sort -u | sed 's/@//g')
# For each context show header and the list of todo items
for context in $CONTEXTS ; do
# Use core _list function, does numbering and colouring for us
CONTEXT_LIST=$(_list "$TODO_FILE" "@$context\b" "$term" | sed 's/(^@|\ *@)[^[:cntrl:] ]\ */ /g')
if [[ -n "${CONTEXT_LIST}" ]]; then
echo "--- $context ---"
echo "${CONTEXT_LIST}"
echo ""
fi
done
# Show todo items not associated to a context
CONTEXT_LIST=$(_list "$TODO_FILE" "$term" | grep -v "@[^ ]*\ *")
if [[ -n "${CONTEXT_LIST}" ]]; then
echo "--- Items without context ---"
echo "${CONTEXT_LIST}"
fi
}
function date_view() {
# Show dates in alphabetical order and todo items in priority order
echo "===== Dates ====="
echo ""
# Find all dates and sort
DATES=$(grep -o '[^ ]*t:[^ ]\+' "$TODO_FILE" | grep '^t:' | sort -u | sed 's/^t://g')
# Get option
option=$1
# Get today
today=$(date -d $(date +%Y-%m-%d) +%s)
# For each date show header and the list of todo items
for date in $DATES ; do
# Check it is a valid date or not
date_check $option $today $date
show="$?"
if [[ $show -eq 0 ]]; then
# If it is not a valid date, just do nothing
continue
fi
# Use core _list function, does numbering and colouring for us
DATE_LIST=$(_list "$TODO_FILE" "t:$date" "$term" | sed 's/\(^t:\|\ *t:\)[0-9-]*\ */ /g')
if [ -n "${DATE_LIST}" ]; then
echo "--- $date ---"
echo "${DATE_LIST}"
echo ""
fi
done
# Show todo items not associated to a date
re="^(date|nodate)$"
if [[ "$option" =~ $re ]]; then
DATE_LIST=$(_list "$TODO_FILE" "$term" | grep -v "t:[0-9-]*")
if [ -n "${DATE_LIST}" ]; then
echo "--- Items without date ---"
echo "${DATE_LIST}"
fi
fi
}
function date_check() {
# Assign variables
threshold=$1
today=$2
_date=$(date -d "$3" +%s)
# Check if the date is valid or not
# return:
# 1: If the date is valid
# 0: If the date is invalid
case $threshold in
'future')
if [[ "$_date" -ge "$today" ]]; then
return 1
fi
;;
'past')
if [[ "$_date" -le "$today" ]]; then
return 1
fi
;;
'nodate')
return 0
;;
'date')
return 1
;;
*)
if [[ "$threshold" -eq "$today" ]]; then
if [[ "$_date" -eq "$today" ]]; then
return 1
fi
elif [[ "$threshold" -gt "$today" ]]; then
if [[ ( "$_date" -ge "$today" ) && ( "$_date" -le "$threshold" ) ]]; then
return 1
fi
elif [[ "$threshold" -lt "$today" ]]; then
if [[ ( "$_date" -le "$today" ) && ( "$_date" -ge "$threshold" ) ]]; then
return 1
fi
fi
;;
esac
return 0
}
# Validate the input options
re="^(help|project|context|date|nodate|future|past|today|tomorrow|yesterday|([+-][0-9]+|[0-9]+)(days|weeks|months|years))$"
if [[ "$option" =~ $re ]]; then
case $option in
'help')
usage
;;
'project')
project_view
;;
'context')
context_view
;;
*)
re="^(date|nodate|future|past)$"
if [[ ! ( "$option" =~ $re ) ]]; then
option=$(date -d $(date -d $option +%Y-%m-%d) +%s)
fi
date_view $option
;;
esac
else
echo "Error: Unrecognized option \"$option\"."
echo "Try \"todo.sh view help\" to get more information."
fi