summary refs log tree commit diff
path: root/bin/todo
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-01-07 17:34:57 +0100
committerRobert Günzler <r@gnzler.io>2025-01-07 17:34:57 +0100
commitbe5f474845be63fa891d44385264648ea1f09985 (patch)
tree6f5ac18fba2ba3d8eb9957e1402d92b8caa89f58 /bin/todo
parent39ac8f7d0a322e581f1757334b01702972996362 (diff)
bin: update todo script
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'bin/todo')
-rwxr-xr-xbin/todo126
1 files changed, 79 insertions, 47 deletions
diff --git a/bin/todo b/bin/todo
index d22f47a..5d49253 100755
--- a/bin/todo
+++ b/bin/todo
@@ -15,74 +15,103 @@ has_todo_file() {
 
 print_org() {
 	awk '
-function filename() {
-    n = split(FILENAME, a, "/")
-    return a[n]
-}
-function res() {
-  return "\033[0;10m"
-}
-function bold(s) {
+    function filename() {
+        n = split(FILENAME, a, "/")
+        return a[n]
+    }
+    function res() {
+        return "\033[0;10m"
+    }
+    function bold(s) {
     return "" s res()
 }
 function red(s) {
-    "tput setaf 1" | getline redc;
-    return redc s res()
+"tput setaf 1" | getline redc;
+return redc s res()
 }
 function green(s) {
-    "tput setaf 2" | getline greenc;
-    return greenc s res()
+"tput setaf 2" | getline greenc;
+return greenc s res()
 }
 function blue(s) {
-    "tput setaf 5" | getline bluec;
-    return bluec s res()
+"tput setaf 5" | getline bluec;
+return bluec s res()
 }
 function magenta(s) {
-    "tput setaf 191" | getline magc;
-    return magc s res()
-  }
+"tput setaf 191" | getline magc;
+return magc s res()
+}
 {
-    if ($1 ~ /^#+/) 
+    if ($1 ~ /^#+/)
         block=$0
 
-    if ($1 ~ /^.*(-|*)/) {
         // support TODO files
-        if (filename() == "TODO") {
-            $0 = gensub(/(^[\w]*(-|*))/, "\\1 TODO", "1", $0)
-        }
+        if ($1 ~ /^(-|*)/) {
+            // detect status strings
+            match($0, /\s(TODO|WIP|WAITING|DONE)\s/, arr)
+
+            if (filename() == "TODO" && arr[1] == "") {
+                $0 = gensub(/(^[\w]*(-|\*))/, "\\1 TODO", "1", $0)
+            }
 
-        match($0, /\s(TODO|WIP|WAITING|DONE|\?\?\?)\s/, arr)
-        if (arr[1] != "") {
             tasks[block][arr[1]][i++]=$0
         }
     }
-}
-END { 
-    for (b in tasks) {
-        print b
-        for (status in tasks[b]) {
-            for (t in tasks[b][status]) {
-                txt = tasks[b][status][t]
-                txt = gensub(/(TODO)/, red("\\1"), "1", txt)
-                txt = gensub(/(WIP)/, blue("\\1"), "1", txt)
-                txt = gensub(/(DONE)/, green("\\1"), "1", txt)
-                txt = gensub(/(\?\?\?)/, bold(magenta("\\1")), "1", txt)
-                print txt
-            }
+END {
+for (b in tasks) {
+    print b
+    for (status in tasks[b]) {
+        for (t in tasks[b][status]) {
+            txt = tasks[b][status][t]
+            txt = gensub(/(TODO)/, red("\\1"), "1", txt)
+            txt = gensub(/(WIP)/, blue("\\1"), "1", txt)
+            txt = gensub(/(DONE)/, green("\\1"), "1", txt)
+            txt = gensub(/(\?\?\?)/, bold(magenta("\\1")), "1", txt)
+            print txt
         }
-        printf "\n"
     }
+    printf "\n"
+}
 }' $1
 }
 
 print_shell_hook() {
 	case $1 in
+	bash)
+		cat <<-EOF
+			__todo_oldpwd="$(\builtin pwd -L)"
+			function __todo_precmd() {
+				\builtin local -r retval="$?"
+				\builtin local pwd_tmp
+				pwd_tmp="$(\builtin pwd -L)"
+				if [[ \${__todo_oldpwd} != "\${pwd_tmp}" ]]; then
+					__todo_oldpwd="\${pwd_tmp}"
+					\command ${0}
+				fi
+				return "\${retval}"
+			}
+			if [[ \${PROMPT_COMMAND:=} != *'__todo_precmd'* ]]; then
+				PROMPT_COMMAND="__todo_precmd;\${PROMPT_COMMAND#;}"
+			fi
+		EOF
+		;;
 	zsh)
-		echo "_orgdown_hook() {$0;}
-typeset -ag precmd_functions;
-if [[ -z \${precmd_functions[(r)_orgdown_hook]} ]]; then
-  precmd_functions+=_orgdown_hook;
-fi"
+		cat <<-EOF
+			__todo_oldpwd="$(\builtin pwd -L)"
+			function __todo_hook() {
+				\builtin local -r retval="$?"
+				\builtin local pwd_tmp
+				if [[ \${__todo_oldpwd} != "\${pwd_tmp}" ]]; then
+					__todo_oldpwd="\${pwd_tmp}"
+					\command ${0}
+				fi
+				return "\${retval}"
+			}
+			typeset -ag precmd_functions;
+			if [[ -z \${precmd_functions[(r)__todo_hook]} ]]; then
+				precmd_functions+=__todo_hook;
+			fi"
+		EOF
 		;;
 	cd)
 		echo "cd() { builtin cd \"\$@\" && $0; }"
@@ -90,6 +119,8 @@ fi"
 	-h | --help | *)
 		echo "usage: todo hook <integration>"
 		echo ""
+		echo "use like this: eval \$(todo hook <integration>)"
+		echo ""
 		echo "supported integrations:"
 		echo " - zsh: run todo as a precmd hook"
 		echo "   (this can get quite annoying when you're in a direcory that has a todo file)"
@@ -121,13 +152,14 @@ hook)
 	done < <(rg --files ${_todo_files[@]/#/-g })
 	;;
 *)
-	[ -f "$1" ] && {
+	if [ -f "$1" ]; then
 		print_org $1
 		exit 0
-	}
-	has_todo_file && {
+	fi
+	if has_todo_file; then
 		hr "todo"
 		print_org $_todo_file
-	}
+		hr ""
+	fi
 	;;
 esac