3
3
4
4
# Configuration ################################
5
5
6
- dir=" ${MDT_DIR:- $PWD } "
6
+ dir=" ${MDT_DIR} "
7
7
inbox=" ${MDT_INBOX} "
8
8
add_multiple_tasks=" ${MDT_ADD_MULTIPLE_TASKS:- 0} "
9
- unite_tasks=" ${MDT_UNITE_TASKS:- false } "
9
+ unite_tasks=" ${MDT_UNITE_TASKS:- 0 } "
10
10
color=" ${MDT_MAIN_COLOR:- 5} "
11
11
prompt=" ${MDT_PROMPT:- ◆} "
12
12
cursor=" ${MDT_CURSOR:- ➔} "
@@ -18,7 +18,7 @@ checkbox_prefix="${MDT_CHECKBOX_PREFIX:--}"
18
18
# ###############################################
19
19
20
20
me=" ${0##*/ } "
21
- mdt_version=" 1.3 .0"
21
+ mdt_version=" 1.4 .0"
22
22
23
23
print_help ()
24
24
{
@@ -60,7 +60,7 @@ parse_commandline()
60
60
-i|--inbox) inbox=" $2 " && shift ;;
61
61
--inbox=* ) inbox=" ${_key## --inbox=} " ;;
62
62
-m|--add-multiple) add_multiple_tasks=1 ;;
63
- -u|--unite-tasks) unite_tasks=true ;;
63
+ -u|--unite-tasks) unite_tasks=1 ;;
64
64
--color) color=" $2 " && shift ;;
65
65
--color=* ) color=" ${_key## --color=} " ;;
66
66
--prompt) prompt=" $2 " && shift ;;
@@ -81,6 +81,11 @@ parse_commandline()
81
81
done
82
82
}
83
83
84
+ die () {
85
+ printf ' Error: %s.\n' " $1 " >&2
86
+ exit 1
87
+ }
88
+
84
89
gum_choose () {
85
90
gum choose --item.width=" ${item_width} " \
86
91
--selected.foreground=" ${color} " \
@@ -117,13 +122,13 @@ get_file_with_checkboxes() {
117
122
_checkbox_pattern=" ^[[:space:]]*(-|\*|\+) \[${_checkbox_type} \]"
118
123
119
124
# Do not search recursively if $dir isn't specified
120
- if [ " ${isPWD } " ]; then
125
+ if [ " ${isDirNotSpecified } " ]; then
121
126
_files=" $( ls -1 -t -- * .md | tr " \n" " \0" \
122
- | xargs -0 grep -El " ${_checkbox_pattern} " ) "
127
+ | xargs -0 grep -E --files-with-matches " ${_checkbox_pattern} " ) "
123
128
else
124
129
_files=" $( find * -name ' *.md' -not -path ' */.*' \
125
130
-exec ls -1 -t -- {} + | tr " \n" " \0" \
126
- | xargs -0 grep -El " ${_checkbox_pattern} " ) "
131
+ | xargs -0 grep -E --files-with-matches " ${_checkbox_pattern} " ) "
127
132
fi
128
133
129
134
[ -z " ${_files} " ] && die " No todo files found"
@@ -152,9 +157,9 @@ get_file_with_checkboxes() {
152
157
get_header_with_tasks () {
153
158
_headers=" $( awk \
154
159
' /^#/ { header = $0 } /^[-*+] \[.]/ { print header; header = "" }' \
155
- " ${_file} " | grep -v ' ^$' ) "
160
+ " ${_file} " | grep --invert-match ' ^$' ) "
156
161
157
- if [ " $( printf " %s\n" " ${_headers} " | wc -l) " -gt 1 ]; then
162
+ if [ " $( printf " %s\n" " ${_headers} " | wc -l) " -gt 1 ]; then
158
163
_header=" $( printf " %s\n" " ${_headers} " | gum_choose) "
159
164
[ -z " ${_header} " ] && exit 1
160
165
else
@@ -168,24 +173,27 @@ get_header_with_tasks() {
168
173
list_open_todos () {
169
174
[ -f " $1 " ] && _file=" $1 " || exit
170
175
171
- if ! grep -Eqs " (-|\*|\+) \[ \]" " ${_file} " ; then
176
+ if ! grep -E --quiet --no-messages " (-|\*|\+) \[ \]" " ${_file} " ; then
172
177
printf " %b\n" " Yey,\033[38;5;${color} m nothing\033[m left to do!" && exit
173
178
fi
174
179
175
- if [ " ${unite_tasks} " = true ]; then
180
+ if [ " ${unite_tasks} " = true ] || [ " ${unite_tasks} " = 1 ] ; then
176
181
_items=" $( grep -E " (-|\*|\+) \[ \] " " ${_file} " ) "
177
182
else
178
183
_items=" $( awk -v header=" $( get_header_with_tasks) " \
179
- ' $0 ~ header {p=1; next} /^#/ {p=0} p && /^\s* [-*+] \[ ]/ {print}' " ${_file} " ) "
184
+ ' $0 ~ header {p=1; next} /^#/ {p=0} p && /[-*+] \[ ] / {print}' " ${_file} " ) "
180
185
fi
181
186
182
- [ -n " ${_items} " ] && _selected_items=" $( printf " %s\n" " ${_items} " \
183
- | sed ' s/^ \{0,1\}[-*+] \[ \] //g' \
184
- | sed ' s/^[[:space:]]*[-*+] \[ \] /└─ /g' \
185
- | gum_choose --no-limit \
186
- --cursor-prefix " [ ] " \
187
- --selected-prefix " [✓] " \
188
- --unselected-prefix " [ ] " ) "
187
+ if [ -n " ${_items} " ]; then
188
+ # Add "└─ " at the beginning for subtasks
189
+ _selected_items=" $( printf " %s\n" " ${_items} " \
190
+ | sed ' s/^ \{0,1\}[-*+] \[ \] //g' \
191
+ | sed ' s/^[[:space:]]*[-*+] \[ \] /└─ /g' \
192
+ | gum_choose --no-limit \
193
+ --cursor-prefix " [ ] " \
194
+ --selected-prefix " [✓] " \
195
+ --unselected-prefix " [ ] " ) "
196
+ fi
189
197
190
198
[ -z " ${_selected_items} " ] && exit
191
199
@@ -222,10 +230,10 @@ add_todo() {
222
230
p && /^[-*+] \[.]/ && !found {line=NR; found=1} \
223
231
END {print line}' " ${_file} " ) "
224
232
225
- # Add a checkbox before $_item it isn't explicitly specified
233
+ # Add a checkbox before $_item if it isn't explicitly specified
226
234
# Example checkboxes for inspiration:
227
235
# https://minimal.guide/Block+types/Checklists#Checkbox+icons
228
- _checkbox=" $( printf " %s\n" " ${_item} " | grep -o " ^\[.\?\] " ) "
236
+ _checkbox=" $( printf " %s\n" " ${_item} " | grep --only-matching " ^\[.\?\] " ) "
229
237
[ -z " ${_checkbox} " ] && _item=" [ ] ${_item} "
230
238
231
239
if [ -z " ${_line} " ]; then
@@ -244,12 +252,7 @@ edit_todo() {
244
252
eval " ${file_editor} " " \" ${_file} \" "
245
253
}
246
254
247
- die () {
248
- printf ' Error: %s.\n' " $1 " >&2
249
- exit 1
250
- }
251
-
252
- # If inbox isn't specified, use default.
255
+ # If inbox isn't specified, use default.
253
256
# If it doesn't exist, ask to create.
254
257
set_inbox () {
255
258
_default_inbox=" ${dir} /TODO.md"
@@ -266,8 +269,9 @@ main() {
266
269
command -v gum > /dev/null || die " gum is required but not installed, exiting"
267
270
parse_commandline " $@ "
268
271
269
- if [ " ${dir} " = " ${PWD} " ]; then
270
- isPWD=1
272
+ if [ -z " ${dir} " ]; then
273
+ dir=" ${PWD} "
274
+ isDirNotSpecified=1
271
275
else
272
276
cd " ${dir} " || die " Directory does not exist"
273
277
fi
0 commit comments