Skip to content

Commit

Permalink
Add option to add multiple tasks at once
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
basilioss committed Apr 25, 2023
1 parent afe9509 commit 8e9d375
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions mdt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

dir="${MDT_DIR:-$PWD}"
inbox="${MDT_INBOX}"
add_multiple_tasks="${MDT_ADD_MULTIPLE_TASKS:-0}"
unite_tasks="${MDT_UNITE_TASKS:-false}"
color="${MDT_MAIN_COLOR:-5}"
prompt="${MDT_PROMPT:-◆}"
Expand Down Expand Up @@ -32,6 +33,7 @@ Usage
Options
-d, --dir Path to the tasks directory
-i, --inbox Path to the inbox file
-m, --add-multiple Add multiple tasks
-u, --unite-tasks List all tasks in the file
--color Main color
--prompt Input prompt character
Expand All @@ -56,6 +58,7 @@ parse_commandline()
--dir=*) dir="${_key##--dir=}" ;;
-i|--inbox) inbox="$2" && shift ;;
--inbox=*) inbox="${_key##--inbox=}" ;;
-m|--add-multiple) add_multiple_tasks=1 ;;
-u|--unite-tasks) unite_tasks=true ;;
--color) color="$2" && shift ;;
--color=*) color="${_key##--color=}" ;;
Expand Down Expand Up @@ -84,6 +87,12 @@ gum_choose() {
--cursor="${cursor} " "$@"
}

gum_input() {
gum input --prompt="${prompt} " \
--prompt.foreground="${color}" \
--width="${input_width}" "$@"
}

create_file_if_missing() {
_file="$1"
_basename="$(basename "${_file}" .md)"
Expand Down Expand Up @@ -151,6 +160,7 @@ get_header_with_tasks() {
_header="${_headers}"
fi

[ -z "${_header}" ] && grep "^#.*" "${_file}" | head -1
printf "%s" "${_header}"
}

Expand Down Expand Up @@ -189,35 +199,42 @@ list_open_todos() {

add_todo() {
[ -f "$1" ] && _file="$1" || exit

_header="$(get_header_with_tasks)" || exit

# Get line number of the first task under the header
_line="$(awk -v header="${_header}" \
'$0 ~ header {p=1; next} /^#/ {p=0} \
p && /^[-*+] \[.]/ && !found {line=NR; found=1} \
END {print line}' "${_file}")"

_todo="$(gum input --prompt="${prompt} " \
--prompt.foreground="${color}" \
--width="${input_width}" \
--placeholder "Got something to do?")"

[ -z "${_todo}" ] && exit

# Add a checkbox before $_todo if it isn't explicitly specified
# Example checkboxes for inspiration:
# https://minimal.guide/Block+types/Checklists#Checkbox+icons
_checkbox="$(printf "%s\n" "${_todo}" | grep -o "^\[.\?\] ")"
[ -z "${_checkbox}" ] && _todo="[ ] ${_todo}"

if [ -z "${_line}" ]; then
printf "%s\n" "${checkbox_prefix} ${_todo}" >> "${_file}"
if [ "${add_multiple_tasks}" = true ] || [ "${add_multiple_tasks}" = 1 ]; then
while true; do
_input="$(gum_input --placeholder "Press Esc or Enter to exit")"
[ -z "${_input}" ] && break || _items="${_items}\n${_input}"
done
else
awk -v line="${_line}" -v todo="${checkbox_prefix} ${_todo}" \
'NR==line{print todo}1' \
"${_file}" > "${_file}.bak" && mv "${_file}.bak" "${_file}"
_items="$(gum_input --placeholder "Got something to do?")"
fi

[ -z "${_items}" ] && exit

printf "%b\n" "${_items}" | while read -r _item; do
[ -z "${_item}" ] && continue

# Get line number of the first task under the header
_line="$(awk -v header="${_header}" \
'$0 ~ header {p=1; next} /^#/ {p=0} \
p && /^[-*+] \[.]/ && !found {line=NR; found=1} \
END {print line}' "${_file}")"

# Add a checkbox before $_item it isn't explicitly specified
# Example checkboxes for inspiration:
# https://minimal.guide/Block+types/Checklists#Checkbox+icons
_checkbox="$(printf "%s\n" "${_item}" | grep -o "^\[.\?\] ")"
[ -z "${_checkbox}" ] && _item="[ ] ${_item}"

if [ -z "${_line}" ]; then
printf "%s\n" "${checkbox_prefix} ${_item}" >> "${_file}"
else
awk -v line="${_line}" -v todo="${checkbox_prefix} ${_item}" \
'NR==line{print todo}1' \
"${_file}" > "${_file}.bak" && mv "${_file}.bak" "${_file}"
fi
done
}

edit_todo() {
Expand Down

0 comments on commit 8e9d375

Please sign in to comment.