From bdf77e48d53fd1a6ba366ac8f8ea9489da24a784 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 28 Jan 2025 19:48:51 +0100 Subject: ... --- overlays/worktime/worktime/__main__.py | 35 ++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'overlays/worktime') diff --git a/overlays/worktime/worktime/__main__.py b/overlays/worktime/worktime/__main__.py index 4c623acd..9b556850 100755 --- a/overlays/worktime/worktime/__main__.py +++ b/overlays/worktime/worktime/__main__.py @@ -23,7 +23,7 @@ import argparse from copy import deepcopy import sys -from sys import stderr +from sys import stderr, stdout from tabulate import tabulate @@ -38,6 +38,7 @@ from collections import defaultdict import jsonpickle from hashlib import blake2s +import json class TogglAPISection(Enum): TOGGL = '/api/v9' @@ -518,7 +519,7 @@ def format_days(worktime, days, date_format=None): return ', '.join(map(lambda group: ','.join(map(format_group, group)), groups)) -def worktime(pull_forward_cutoff, **args): +def worktime(pull_forward_cutoff, waybar, **args): worktime = Worktime(**args) def format_worktime(worktime): @@ -562,6 +563,7 @@ def worktime(pull_forward_cutoff, **args): else: return f"({difference_string})" + out_class = "running" if worktime.running_entry else "stopped" if worktime.time_pulled_forward >= pull_forward_cutoff: worktime_no_pulled_forward = deepcopy(worktime) worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward @@ -570,11 +572,16 @@ def worktime(pull_forward_cutoff, **args): difference_string = format_worktime(worktime) difference_string_no_pulled_forward = format_worktime(worktime_no_pulled_forward) - print(f"{difference_string_no_pulled_forward}…{difference_string}") + out_text = f"{difference_string_no_pulled_forward}…{difference_string}" else: - print(format_worktime(worktime)) + out_text = format_worktime(worktime) -def time_worked(now, **args): + if waybar: + json.dump({"text": out_text, "class": out_class}, stdout) + else: + print(out_text) + +def time_worked(now, waybar, **args): then = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) if now.time() == time(): now = now + timedelta(days = 1) @@ -584,6 +591,8 @@ def time_worked(now, **args): worked = now.time_worked - then.time_worked + out_text = None + out_class = "stopped" if args['do_round']: total_minutes_difference = 5 * ceil(worked / timedelta(minutes = 5)) (hours_difference, minutes_difference) = divmod(abs(total_minutes_difference), 60) @@ -606,11 +615,19 @@ def time_worked(now, **args): clockout_time = clockout_time.replace(second = 0, microsecond = 0) if now.running_entry and clockout_time and clockout_difference >= 0: - print(f"{difference_string}/{clockout_time:%H:%M}") + out_class = "running" + out_text = f"{difference_string}/{clockout_time:%H:%M}" else: - print(difference_string) + if now.running_entry: + out_class = "over" + out_text = difference_string + else: + out_text = str(worked) + + if waybar: + json.dump({"text": out_text, "class": out_class}, stdout) else: - print(worked) + print(out_text) def diff(now, **args): now = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) @@ -841,9 +858,11 @@ def main(): subparsers = parser.add_subparsers(help = 'Subcommands') worktime_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked']) worktime_parser.add_argument('--pull-forward-cutoff', dest = 'pull_forward_cutoff', metavar = 'MINUTES', type = duration_minutes, default = timedelta(minutes = 15)) + worktime_parser.add_argument('--waybar', action='store_true') worktime_parser.set_defaults(cmd = worktime) time_worked_parser = subparsers.add_parser('today') time_worked_parser.add_argument('--no-round', dest = 'do_round', action = 'store_false') + time_worked_parser.add_argument('--waybar', action='store_true') time_worked_parser.set_defaults(cmd = time_worked) diff_parser = subparsers.add_parser('diff') diff_parser.set_defaults(cmd = diff) -- cgit v1.2.3