From d7acf9dbb07a9341d50d1634a0c18d77add443c8 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 9 Mar 2022 22:43:44 +0100 Subject: [PATCH] Add todo dmenu script --- home/.local/bin/scripts/dmenu/todo | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 home/.local/bin/scripts/dmenu/todo diff --git a/home/.local/bin/scripts/dmenu/todo b/home/.local/bin/scripts/dmenu/todo new file mode 100755 index 0000000..4cf8c90 --- /dev/null +++ b/home/.local/bin/scripts/dmenu/todo @@ -0,0 +1,36 @@ +#!/bin/sh +# This is inspired by dmenu's todo script made by suckless +# +# Manage TODO tasks in dmenu by writing them, remove by selecting +# an existing entry +# +# Configurable variables +# --------------------------------------------------------------------- + +FILE="${XDG_DATA_HOME:-$HOME/.local/share}/todos" +PROMPT="Add/delete a task: " + +# Logic +# --------------------------------------------------------------------- +mkdir -p "$(dirname $FILE)" +touch "$FILE" + +height=$(wc -l "$FILE" | awk '{print $1}') + +# Run dmenu and keep restarting it until it returns an empty output +cmd=$(dmenu -l "$height" -p "$PROMPT" "$@" < "$FILE") +while [ -n "$cmd" ]; do + # If the output matched an existing TODO, remove it + if grep -q "^$cmd\$" "$FILE"; then + grep -v "^$cmd\$" "$FILE" > "$FILE.$$" + mv "$FILE.$$" "$FILE" + height=$(( height - 1 )) + # If the output didn't match an existing TODO, it's a new one, add it + else + echo "$cmd" >> "$FILE" + height=$(( height + 1 )) + fi + + # Keep restarting until empty output + cmd=$(dmenu -l "$height" -p "$PROMPT" "$@" < "$FILE") +done