From 7f4c7f9bda73851ec556ffef313588f6c773b218 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sun, 1 Oct 2023 20:17:53 +0200 Subject: fix(hm/conf/taskwarrior/hooks): Check in every hook the started tasks --- hm/soispha/conf/taskwarrior/hooks/default.nix | 7 +++++- .../hooks/scripts/on-modify_track-timewarrior.py | 25 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/hm/soispha/conf/taskwarrior/hooks/default.nix b/hm/soispha/conf/taskwarrior/hooks/default.nix index 8fc6d9ff..66af5b3b 100644 --- a/hm/soispha/conf/taskwarrior/hooks/default.nix +++ b/hm/soispha/conf/taskwarrior/hooks/default.nix @@ -20,7 +20,11 @@ name = "track_timewarrior.taskwarrior-hook"; propagatedBuildInputs = [ pkgs.timewarrior - pkgs.python3 + pkgs.taskwarrior + (pkgs.python3.withPackages (pythonPackages: + with pythonPackages; [ + taskw + ])) ]; dontUnpack = true; installPhase = "install -Dm755 ${./scripts/on-modify_track-timewarrior.py} $out/bin/bin"; @@ -28,6 +32,7 @@ track_total_active_time = pkgs.stdenv.mkDerivation { name = "track_total_active_time.taskwarrior-hook"; propagatedBuildInputs = [ + pkgs.taskwarrior (pkgs.python3.withPackages (pythonPackages: with pythonPackages; [ taskw 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. -- cgit 1.4.1