about summary refs log tree commit diff stats
path: root/hm/soispha/conf/taskwarrior/hooks/scripts
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2023-10-01 20:17:53 +0200
committerSoispha <soispha@vhack.eu>2023-10-01 20:34:15 +0200
commit7f4c7f9bda73851ec556ffef313588f6c773b218 (patch)
tree68bbc9396519b13a0521cf4b0b78dfffab5ff8c2 /hm/soispha/conf/taskwarrior/hooks/scripts
parentchore(revert): "fix(hm/conf/taskwarrior): Add 'total_active_time' to next rep... (diff)
downloadnixos-config-7f4c7f9bda73851ec556ffef313588f6c773b218.zip
fix(hm/conf/taskwarrior/hooks): Check in every hook the started tasks
Diffstat (limited to 'hm/soispha/conf/taskwarrior/hooks/scripts')
-rwxr-xr-xhm/soispha/conf/taskwarrior/hooks/scripts/on-modify_track-timewarrior.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/hm/soispha/conf/taskwarrior/hooks/scripts/on-modify_track-timewarrior.py b/hm/soispha/conf/taskwarrior/hooks/scripts/on-modify_track-timewarrior.py
index 63065754..03b4ce42 100755
--- a/hm/soispha/conf/taskwarrior/hooks/scripts/on-modify_track-timewarrior.py
+++ b/hm/soispha/conf/taskwarrior/hooks/scripts/on-modify_track-timewarrior.py
@@ -32,15 +32,27 @@ This hook is a fork from the `official on-modify.timewarrior hook`_.
    https://github.com/GothenburgBitFactory/timewarrior/blob/dev/ext/on-modify.timewarrior
 """
 
-from sys import stdin
-from os import system
+import subprocess
+import sys
 from json import loads, dumps
+from os import system
+from sys import stdin
+from taskw import TaskWarrior
 
 # Make no changes to the task, simply observe.
 old = loads(stdin.readline())
 new = loads(stdin.readline())
 print(dumps(new))
 
+
+w = TaskWarrior(config_filename=sys.argv[4].replace("rc:", ""))
+config = w.load_config(config_filename=sys.argv[4].replace("rc:", ""))
+if "max_active_tasks" in config:
+    MAX_ACTIVE = int(config["max_active_tasks"])
+else:
+    MAX_ACTIVE = 1
+
+
 # Extract attributes for use as tags.
 tags = [new["description"]]
 
@@ -57,6 +69,15 @@ combined = " ".join(["'%s'" % tag for tag in tags]).encode("utf-8").strip()
 
 # Task has been started.
 if "start" in new and not "start" in old:
+    # Prevent this task from starting if "task +ACTIVE count" is greater than "MAX_ACTIVE".
+    p = subprocess.Popen(["task", "+ACTIVE", "status:pending", "count", "rc.verbose:off"], stdout=subprocess.PIPE)
+    out, err = p.communicate()
+    count = int(out.rstrip())
+    if count >= MAX_ACTIVE:
+        print("Only %d task(s) can be active at a time. "
+              "See 'max_active_tasks' in .taskrc." % MAX_ACTIVE)
+        sys.exit(1)
+
     system("timew start " + combined.decode() + " :yes")
 
 # Task has been stopped.