Skip to content

Commit 96f393f

Browse files
committed
Refactor code and fix bugs
- Fix not working recursive search if current directory is MDT_DIR. - Fix subtasks not displaying on some systems if -u option isn't passed. - Add compatibility to allow the use of 1/0 instead of true/false for -u option. - Change short option names to long for better readability. - Use traditional if-else statement for $_selected_items to improve readability. - Group functions: move die() function in the beginning. - Fix typos.
1 parent 65f530e commit 96f393f

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

mdt

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
# Configuration ################################
55

6-
dir="${MDT_DIR:-$PWD}"
6+
dir="${MDT_DIR}"
77
inbox="${MDT_INBOX}"
88
add_multiple_tasks="${MDT_ADD_MULTIPLE_TASKS:-0}"
9-
unite_tasks="${MDT_UNITE_TASKS:-false}"
9+
unite_tasks="${MDT_UNITE_TASKS:-0}"
1010
color="${MDT_MAIN_COLOR:-5}"
1111
prompt="${MDT_PROMPT:-◆}"
1212
cursor="${MDT_CURSOR:-➔}"
@@ -18,7 +18,7 @@ checkbox_prefix="${MDT_CHECKBOX_PREFIX:--}"
1818
################################################
1919

2020
me="${0##*/}"
21-
mdt_version="1.3.0"
21+
mdt_version="1.4.0"
2222

2323
print_help()
2424
{
@@ -60,7 +60,7 @@ parse_commandline()
6060
-i|--inbox) inbox="$2" && shift ;;
6161
--inbox=*) inbox="${_key##--inbox=}" ;;
6262
-m|--add-multiple) add_multiple_tasks=1 ;;
63-
-u|--unite-tasks) unite_tasks=true ;;
63+
-u|--unite-tasks) unite_tasks=1 ;;
6464
--color) color="$2" && shift ;;
6565
--color=*) color="${_key##--color=}" ;;
6666
--prompt) prompt="$2" && shift ;;
@@ -81,6 +81,11 @@ parse_commandline()
8181
done
8282
}
8383

84+
die() {
85+
printf 'Error: %s.\n' "$1" >&2
86+
exit 1
87+
}
88+
8489
gum_choose() {
8590
gum choose --item.width="${item_width}" \
8691
--selected.foreground="${color}" \
@@ -117,13 +122,13 @@ get_file_with_checkboxes() {
117122
_checkbox_pattern="^[[:space:]]*(-|\*|\+) \[${_checkbox_type}\]"
118123

119124
# Do not search recursively if $dir isn't specified
120-
if [ "${isPWD}" ]; then
125+
if [ "${isDirNotSpecified}" ]; then
121126
_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}")"
123128
else
124129
_files="$(find * -name '*.md' -not -path '*/.*' \
125130
-exec ls -1 -t -- {} + | tr "\n" "\0" \
126-
| xargs -0 grep -El "${_checkbox_pattern}")"
131+
| xargs -0 grep -E --files-with-matches "${_checkbox_pattern}")"
127132
fi
128133

129134
[ -z "${_files}" ] && die "No todo files found"
@@ -152,9 +157,9 @@ get_file_with_checkboxes() {
152157
get_header_with_tasks() {
153158
_headers="$(awk \
154159
'/^#/ { header = $0 } /^[-*+] \[.]/ { print header; header = "" }' \
155-
"${_file}" | grep -v '^$')"
160+
"${_file}" | grep --invert-match '^$')"
156161

157-
if [ "$(printf "%s\n" "${_headers}" | wc -l)" -gt 1 ]; then
162+
if [ "$(printf "%s\n" "${_headers}" | wc -l)" -gt 1 ]; then
158163
_header="$(printf "%s\n" "${_headers}" | gum_choose)"
159164
[ -z "${_header}" ] && exit 1
160165
else
@@ -168,24 +173,27 @@ get_header_with_tasks() {
168173
list_open_todos() {
169174
[ -f "$1" ] && _file="$1" || exit
170175

171-
if ! grep -Eqs "(-|\*|\+) \[ \]" "${_file}"; then
176+
if ! grep -E --quiet --no-messages "(-|\*|\+) \[ \]" "${_file}"; then
172177
printf "%b\n" "Yey,\033[38;5;${color}m nothing\033[m left to do!" && exit
173178
fi
174179

175-
if [ "${unite_tasks}" = true ]; then
180+
if [ "${unite_tasks}" = true ] || [ "${unite_tasks}" = 1 ]; then
176181
_items="$(grep -E "(-|\*|\+) \[ \] " "${_file}")"
177182
else
178183
_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}")"
180185
fi
181186

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
189197

190198
[ -z "${_selected_items}" ] && exit
191199

@@ -222,10 +230,10 @@ add_todo() {
222230
p && /^[-*+] \[.]/ && !found {line=NR; found=1} \
223231
END {print line}' "${_file}")"
224232

225-
# Add a checkbox before $_item it isn't explicitly specified
233+
# Add a checkbox before $_item if it isn't explicitly specified
226234
# Example checkboxes for inspiration:
227235
# 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 "^\[.\?\] ")"
229237
[ -z "${_checkbox}" ] && _item="[ ] ${_item}"
230238

231239
if [ -z "${_line}" ]; then
@@ -244,12 +252,7 @@ edit_todo() {
244252
eval "${file_editor}" "\"${_file}\""
245253
}
246254

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.
253256
# If it doesn't exist, ask to create.
254257
set_inbox() {
255258
_default_inbox="${dir}/TODO.md"
@@ -266,8 +269,9 @@ main() {
266269
command -v gum > /dev/null || die "gum is required but not installed, exiting"
267270
parse_commandline "$@"
268271

269-
if [ "${dir}" = "${PWD}" ]; then
270-
isPWD=1
272+
if [ -z "${dir}" ]; then
273+
dir="${PWD}"
274+
isDirNotSpecified=1
271275
else
272276
cd "${dir}" || die "Directory does not exist"
273277
fi

0 commit comments

Comments
 (0)