aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/ti/timewarrior
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-04 11:55:51 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-04 11:55:51 +0200
commitae373d7927bcb0148f6714f4438008433882ffbc (patch)
tree4c067cdcf0afb928782d334e348027d2832f6d17 /modules/by-name/ti/timewarrior
parentfeat(modules/tskm): Init module (diff)
downloadnixos-config-ae373d7927bcb0148f6714f4438008433882ffbc.zip
refactor(modules/timewarrior): Migrate to by-name
Diffstat (limited to 'modules/by-name/ti/timewarrior')
-rw-r--r--modules/by-name/ti/timewarrior/module.nix96
-rw-r--r--modules/by-name/ti/timewarrior/nord.theme25
-rwxr-xr-xmodules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-timewarrior.py94
-rwxr-xr-xmodules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-total-active-time.py148
4 files changed, 363 insertions, 0 deletions
diff --git a/modules/by-name/ti/timewarrior/module.nix b/modules/by-name/ti/timewarrior/module.nix
new file mode 100644
index 00000000..e4087e69
--- /dev/null
+++ b/modules/by-name/ti/timewarrior/module.nix
@@ -0,0 +1,96 @@
+{
+ config,
+ lib,
+ pkgs,
+ ...
+}: let
+ cfg = config.soispha.programs.timewarrior;
+
+ track_timewarrior = pkgs.stdenv.mkDerivation {
+ name = "track_timewarrior.taskwarrior-hook";
+ nativeBuildInputs = [
+ pkgs.makeWrapper
+ ];
+ buildInputs = [
+ pkgs.timewarrior
+ pkgs.taskwarrior
+ # TODO: Use a `taskw` package, that actually supports newer python variants <2024-07-13>
+ (pkgs.python311.withPackages (pythonPackages:
+ with pythonPackages; [
+ taskw
+ ]))
+ ];
+ dontUnpack = true;
+ installPhase = ''
+ install -Dm755 ${./taskwarirror_hooks/on-modify_track-timewarrior.py} $out/bin/bin
+ wrapProgram $out/bin/bin \
+ --prefix PATH : ${lib.makeBinPath [pkgs.taskwarrior pkgs.timewarrior]}
+ '';
+
+ meta.mainProgram = "bin";
+ };
+ track_total_active_time = pkgs.stdenv.mkDerivation {
+ name = "track_total_active_time.taskwarrior-hook";
+ nativeBuildInputs = [
+ pkgs.makeWrapper
+ ];
+ buildInputs = [
+ pkgs.taskwarrior
+ # TODO: Use a `taskw` package, that actually supports newer python variants <2024-07-13>
+ (pkgs.python311.withPackages (pythonPackages:
+ with pythonPackages; [
+ taskw
+ ]))
+ ];
+ dontUnpack = true;
+ installPhase = ''
+ install -Dm755 ${./taskwarirror_hooks/on-modify_track-total-active-time.py} $out/bin/bin
+ wrapProgram $out/bin/bin \
+ --prefix PATH : ${lib.makeBinPath [pkgs.taskwarrior]}
+ '';
+
+ meta.mainProgram = "bin";
+ };
+in {
+ options.soispha.programs.timewarrior = {
+ enable = lib.mkEnableOption "timewarrior";
+ };
+ config = lib.mkIf cfg.enable {
+ home-manager.users.soispha = {
+ home.packages = [
+ pkgs.timewarrior
+ ];
+
+ soispha.programs.taskwarrior = {
+ hooks = {
+ track-timewarrior = {
+ mode = "on-modify";
+ executable = track_timewarrior;
+ };
+ track-total-active-time = {
+ mode = "on-modify";
+ executable = track_total_active_time;
+ };
+ };
+ };
+
+ xdg.configFile."timewarrior/timewarrior.cfg".text = ''
+ # source: https://github.com/arcticicestudio/igloo
+ #+----+
+ #+ UI +
+ #+----+
+ import ${./nord.theme}
+ color = true
+
+ #+---------+
+ #+ Reports +
+ #+---------+
+ define reports:
+ day:
+ lines = 10
+ month = true
+ week = true
+ '';
+ };
+ };
+}
diff --git a/modules/by-name/ti/timewarrior/nord.theme b/modules/by-name/ti/timewarrior/nord.theme
new file mode 100644
index 00000000..da3c387a
--- /dev/null
+++ b/modules/by-name/ti/timewarrior/nord.theme
@@ -0,0 +1,25 @@
+# Copyright (C) 2016-present Arctic Ice Studio <development@arcticicestudio.com>
+# Copyright (C) 2016-present Sven Greb <development@svengreb.de>
+
+# Project: igloo
+# Repository: https://github.com/arcticicestudio/igloo
+# License: MIT
+# References:
+# https://taskwarrior.org/docs/timewarrior/themes.html
+# timew(1)
+
+define theme:
+ description = "An arctic, north-bluish clean and elegant Timewarrior theme."
+ colors:
+ exclusion = "bold black"
+ today = "cyan"
+ holiday = "bold blue on white"
+ label = "bold white on black"
+ ids = "bold black on cyan"
+ debug = "magenta"
+
+ # Rotating color palette for tags.
+ palette:
+ color01 = "bold black on cyan"
+ color02 = "bold black on bright cyan"
+ color03 = "bold black on blue"
diff --git a/modules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-timewarrior.py b/modules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-timewarrior.py
new file mode 100755
index 00000000..b482af6a
--- /dev/null
+++ b/modules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-timewarrior.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (C) 2016-present Arctic Ice Studio <development@arcticicestudio.com>
+# Copyright (C) 2016-present Sven Greb <development@svengreb.de>
+
+# Project: igloo
+# Repository: https://github.com/arcticicestudio/igloo
+# License: MIT
+# References:
+# https://taskwarrior.org/docs
+# https://taskwarrior.org/docs/timewarrior
+# timew(1)
+# task(1)
+
+"""A Taskwarrior hook to track the time of a active task with Taskwarrior.
+
+This hook will extract all of the following for use as Timewarrior tags:
+
+* UUID
+* Project
+* Tags
+* Description
+* UDAs
+
+Note:
+ This hook requires Python 3 and is only compatible with Taskwarrior version greater or equal to 2.4!
+
+This hook is a fork from the `official on-modify.timewarrior hook`_.
+
+.. _`official on-modify.timewarrior hook`:
+ https://github.com/GothenburgBitFactory/timewarrior/blob/dev/ext/on-modify.timewarrior
+"""
+
+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"]]
+
+if "project" in new:
+ project = new["project"]
+ tags.append(project)
+ if "." in project:
+ tags.extend([tag for tag in project.split(".")])
+
+if "tags" in new:
+ tags.extend(new["tags"])
+
+combined = " ".join(["'%s'" % tag for tag in tags]).encode("utf-8").strip()
+
+# Task has been started.
+if "start" in new and "start" not 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.
+elif "start" not in new and "start" in old:
+ system("timew stop " + combined.decode() + " :yes")
+
+# Any task that is active, with a non-pending status should not be tracked.
+elif "start" in new and new["status"] != "pending":
+ system("timew stop " + combined.decode() + " :yes")
diff --git a/modules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-total-active-time.py b/modules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-total-active-time.py
new file mode 100755
index 00000000..d5b380d0
--- /dev/null
+++ b/modules/by-name/ti/timewarrior/taskwarirror_hooks/on-modify_track-total-active-time.py
@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (C) 2016-present Arctic Ice Studio <development@arcticicestudio.com>
+# Copyright (C) 2016-present Sven Greb <development@svengreb.de>
+
+# Project: igloo
+# Repository: https://github.com/arcticicestudio/igloo
+# License: MIT
+# References:
+# https://taskwarrior.org/docs
+# task(1)
+
+"""A Taskwarrior hook to track the total active time of a task.
+
+The tracked time is stored in a UDA task duration attribute named ``totalactivetime`` of type ``duration`` holding the total number of seconds the task was
+active. The tracked time can then be included in any report by adding the ``totalactivetime`` column.
+
+By default, this plugin allows to have one task active at a time. This can be changed by setting ``max_active_tasks`` in ``.taskrc`` to a value greater than
+``1``.
+
+Note:
+ This hook requires Python 3 and the `taskw`_ package to be installed which provides the python bindings for Taskwarrior!
+ Also note that this hook is only compatible with Taskwarrior version greater or equal to 2.4!
+
+This hook is a fork from `kostajh/taskwarrior-time-tracking-hook`_
+
+.. _taskw:
+ https://pypi.python.org/pypi/taskw
+.. _kostajh/taskwarrior-time-tracking-hook:
+ https://github.com/kostajh/taskwarrior-time-tracking-hook
+"""
+
+import datetime
+import json
+import re
+import sys
+import subprocess
+from taskw import TaskWarrior
+from typing import TypeVar
+
+TIME_FORMAT = "%Y%m%dT%H%M%SZ"
+UDA_KEY = "total_active_time"
+
+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
+
+"""Compiled regular expression for the duration as ISO-8601 formatted string."""
+ISO8601DURATION = re.compile("P((\d*)Y)?((\d*)M)?((\d*)D)?T((\d*)H)?((\d*)M)?((\d*)S)?")
+
+"""The duration type either as integer (in seconds), as ISO-8601 formatted string ("PT1H10M31S") or the seconds suffixed with "seconds"."""
+DurationType = TypeVar("DurationType", str, int)
+
+
+def duration_str_to_time_delta(duration_str: DurationType) -> datetime.timedelta:
+ """Converts duration string into a timedelta object.
+
+ :param duration_str: The duration
+ :return: The duration as timedelta object
+ """
+ if duration_str.startswith("P"):
+ match = ISO8601DURATION.match(duration_str)
+ if match:
+ year = match.group(2)
+ month = match.group(4)
+ day = match.group(6)
+ hour = match.group(8)
+ minute = match.group(10)
+ second = match.group(12)
+ value = 0
+ if second:
+ value += int(second)
+ if minute:
+ value += int(minute) * 60
+ if hour:
+ value += int(hour) * 3600
+ if day:
+ value += int(day) * 3600 * 24
+ if month:
+ # Assume a month is 30 days for now.
+ value += int(month) * 3600 * 24 * 30
+ if year:
+ # Assume a year is 365 days for now.
+ value += int(year) * 3600 * 24 * 365
+ else:
+ value = int(duration_str)
+ elif duration_str.endswith("seconds"):
+ value = int(duration_str.rstrip("seconds"))
+ else:
+ value = int(duration_str)
+ return datetime.timedelta(seconds=value)
+
+
+def main():
+ original = json.loads(sys.stdin.readline())
+ modified = json.loads(sys.stdin.readline())
+
+ # An active task has just been started.
+ if "start" in modified and "start" not in original:
+ # 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)
+
+ # An active task has just been stopped.
+ if "start" in original and "start" not in modified:
+ # Calculate the elapsed time.
+ start = datetime.datetime.strptime(original["start"], TIME_FORMAT)
+ end = datetime.datetime.utcnow()
+
+ if UDA_KEY not in modified:
+ modified[UDA_KEY] = 0
+
+ this_duration = end - start
+ total_duration = this_duration + duration_str_to_time_delta(
+ str(modified[UDA_KEY])
+ )
+ print(
+ "Total Time Tracked: %s (%s in this instance)"
+ % (total_duration, this_duration)
+ )
+ modified[UDA_KEY] = (
+ str(int(total_duration.days * (60 * 60 * 24) + total_duration.seconds))
+ + "seconds"
+ )
+
+ return json.dumps(modified, separators=(",", ":"))
+
+
+def cmdline():
+ sys.stdout.write(main())
+
+
+if __name__ == "__main__":
+ cmdline()