diff options
Diffstat (limited to 'overlays')
| -rwxr-xr-x | overlays/worktime/worktime/__main__.py | 35 |
1 files changed, 27 insertions, 8 deletions
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 | |||
| 23 | from copy import deepcopy | 23 | from copy import deepcopy |
| 24 | 24 | ||
| 25 | import sys | 25 | import sys |
| 26 | from sys import stderr | 26 | from sys import stderr, stdout |
| 27 | 27 | ||
| 28 | from tabulate import tabulate | 28 | from tabulate import tabulate |
| 29 | 29 | ||
| @@ -38,6 +38,7 @@ from collections import defaultdict | |||
| 38 | 38 | ||
| 39 | import jsonpickle | 39 | import jsonpickle |
| 40 | from hashlib import blake2s | 40 | from hashlib import blake2s |
| 41 | import json | ||
| 41 | 42 | ||
| 42 | class TogglAPISection(Enum): | 43 | class TogglAPISection(Enum): |
| 43 | TOGGL = '/api/v9' | 44 | TOGGL = '/api/v9' |
| @@ -518,7 +519,7 @@ def format_days(worktime, days, date_format=None): | |||
| 518 | return ', '.join(map(lambda group: ','.join(map(format_group, group)), groups)) | 519 | return ', '.join(map(lambda group: ','.join(map(format_group, group)), groups)) |
| 519 | 520 | ||
| 520 | 521 | ||
| 521 | def worktime(pull_forward_cutoff, **args): | 522 | def worktime(pull_forward_cutoff, waybar, **args): |
| 522 | worktime = Worktime(**args) | 523 | worktime = Worktime(**args) |
| 523 | 524 | ||
| 524 | def format_worktime(worktime): | 525 | def format_worktime(worktime): |
| @@ -562,6 +563,7 @@ def worktime(pull_forward_cutoff, **args): | |||
| 562 | else: | 563 | else: |
| 563 | return f"({difference_string})" | 564 | return f"({difference_string})" |
| 564 | 565 | ||
| 566 | out_class = "running" if worktime.running_entry else "stopped" | ||
| 565 | if worktime.time_pulled_forward >= pull_forward_cutoff: | 567 | if worktime.time_pulled_forward >= pull_forward_cutoff: |
| 566 | worktime_no_pulled_forward = deepcopy(worktime) | 568 | worktime_no_pulled_forward = deepcopy(worktime) |
| 567 | worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward | 569 | worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward |
| @@ -570,11 +572,16 @@ def worktime(pull_forward_cutoff, **args): | |||
| 570 | difference_string = format_worktime(worktime) | 572 | difference_string = format_worktime(worktime) |
| 571 | difference_string_no_pulled_forward = format_worktime(worktime_no_pulled_forward) | 573 | difference_string_no_pulled_forward = format_worktime(worktime_no_pulled_forward) |
| 572 | 574 | ||
| 573 | print(f"{difference_string_no_pulled_forward}…{difference_string}") | 575 | out_text = f"{difference_string_no_pulled_forward}…{difference_string}" |
| 574 | else: | 576 | else: |
| 575 | print(format_worktime(worktime)) | 577 | out_text = format_worktime(worktime) |
| 576 | 578 | ||
| 577 | def time_worked(now, **args): | 579 | if waybar: |
| 580 | json.dump({"text": out_text, "class": out_class}, stdout) | ||
| 581 | else: | ||
| 582 | print(out_text) | ||
| 583 | |||
| 584 | def time_worked(now, waybar, **args): | ||
| 578 | then = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) | 585 | then = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) |
| 579 | if now.time() == time(): | 586 | if now.time() == time(): |
| 580 | now = now + timedelta(days = 1) | 587 | now = now + timedelta(days = 1) |
| @@ -584,6 +591,8 @@ def time_worked(now, **args): | |||
| 584 | 591 | ||
| 585 | worked = now.time_worked - then.time_worked | 592 | worked = now.time_worked - then.time_worked |
| 586 | 593 | ||
| 594 | out_text = None | ||
| 595 | out_class = "stopped" | ||
| 587 | if args['do_round']: | 596 | if args['do_round']: |
| 588 | total_minutes_difference = 5 * ceil(worked / timedelta(minutes = 5)) | 597 | total_minutes_difference = 5 * ceil(worked / timedelta(minutes = 5)) |
| 589 | (hours_difference, minutes_difference) = divmod(abs(total_minutes_difference), 60) | 598 | (hours_difference, minutes_difference) = divmod(abs(total_minutes_difference), 60) |
| @@ -606,11 +615,19 @@ def time_worked(now, **args): | |||
| 606 | clockout_time = clockout_time.replace(second = 0, microsecond = 0) | 615 | clockout_time = clockout_time.replace(second = 0, microsecond = 0) |
| 607 | 616 | ||
| 608 | if now.running_entry and clockout_time and clockout_difference >= 0: | 617 | if now.running_entry and clockout_time and clockout_difference >= 0: |
| 609 | print(f"{difference_string}/{clockout_time:%H:%M}") | 618 | out_class = "running" |
| 619 | out_text = f"{difference_string}/{clockout_time:%H:%M}" | ||
| 610 | else: | 620 | else: |
| 611 | print(difference_string) | 621 | if now.running_entry: |
| 622 | out_class = "over" | ||
| 623 | out_text = difference_string | ||
| 624 | else: | ||
| 625 | out_text = str(worked) | ||
| 626 | |||
| 627 | if waybar: | ||
| 628 | json.dump({"text": out_text, "class": out_class}, stdout) | ||
| 612 | else: | 629 | else: |
| 613 | print(worked) | 630 | print(out_text) |
| 614 | 631 | ||
| 615 | def diff(now, **args): | 632 | def diff(now, **args): |
| 616 | now = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) | 633 | now = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) |
| @@ -841,9 +858,11 @@ def main(): | |||
| 841 | subparsers = parser.add_subparsers(help = 'Subcommands') | 858 | subparsers = parser.add_subparsers(help = 'Subcommands') |
| 842 | worktime_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked']) | 859 | worktime_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked']) |
| 843 | worktime_parser.add_argument('--pull-forward-cutoff', dest = 'pull_forward_cutoff', metavar = 'MINUTES', type = duration_minutes, default = timedelta(minutes = 15)) | 860 | worktime_parser.add_argument('--pull-forward-cutoff', dest = 'pull_forward_cutoff', metavar = 'MINUTES', type = duration_minutes, default = timedelta(minutes = 15)) |
| 861 | worktime_parser.add_argument('--waybar', action='store_true') | ||
| 844 | worktime_parser.set_defaults(cmd = worktime) | 862 | worktime_parser.set_defaults(cmd = worktime) |
| 845 | time_worked_parser = subparsers.add_parser('today') | 863 | time_worked_parser = subparsers.add_parser('today') |
| 846 | time_worked_parser.add_argument('--no-round', dest = 'do_round', action = 'store_false') | 864 | time_worked_parser.add_argument('--no-round', dest = 'do_round', action = 'store_false') |
| 865 | time_worked_parser.add_argument('--waybar', action='store_true') | ||
| 847 | time_worked_parser.set_defaults(cmd = time_worked) | 866 | time_worked_parser.set_defaults(cmd = time_worked) |
| 848 | diff_parser = subparsers.add_parser('diff') | 867 | diff_parser = subparsers.add_parser('diff') |
| 849 | diff_parser.set_defaults(cmd = diff) | 868 | diff_parser.set_defaults(cmd = diff) |
