From 5b91852a6f718223b1e4b0e637d4b6500bf8c5f0 Mon Sep 17 00:00:00 2001 From: Olaf Lessenich Date: Tue, 25 Apr 2023 21:30:07 +0200 Subject: [PATCH] feat: add resume command Support resuming the last tracked activity. When `hamster resume` is called, the last tracked activity is restarted. If `hamster resume --continuous` (or short with `-c`) is called, the last tracked activity is restarted and the start time is set to the end time of the previous activity. Signed-off-by: Olaf Lessenich --- src/hamster-cli.py | 8 ++++++++ src/hamster-service.py | 8 ++++++++ src/hamster/client.py | 8 ++++++++ src/hamster/storage/storage.py | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/src/hamster-cli.py b/src/hamster-cli.py index 9c6786787..f65d46068 100755 --- a/src/hamster-cli.py +++ b/src/hamster-cli.py @@ -261,6 +261,14 @@ def start(self, *args): return id_ + def resume(self, *args): + '''Resume the last activity.''' + continuous = False + if args and (args[0] == "--continuous" or args[0] == "-c"): + continuous = True + self.storage.resume_tracking(continuous=continuous) + + def stop(self, *args): '''Stop tracking the current activity.''' self.storage.stop_tracking() diff --git a/src/hamster-service.py b/src/hamster-service.py index 29e7c1e06..eb2677cac 100755 --- a/src/hamster-service.py +++ b/src/hamster-service.py @@ -281,6 +281,14 @@ def StopTracking(self, end_time): end_time = dt.datetime.utcfromtimestamp(end_time) return self.stop_tracking(end_time) + @dbus.service.method("org.gnome.Hamster") + def ResumeTracking(self, continuous = False): + """Resumes tracking the last activity. + Parameters: + b continuous: Use the previous end time as start time to fill the untracked gap. + Default is False. + """ + return self.resume_tracking(continuous) @dbus.service.method("org.gnome.Hamster") def StopOrRestartTracking(self): diff --git a/src/hamster/client.py b/src/hamster/client.py index 8a0c297c3..d65d3fe3b 100644 --- a/src/hamster/client.py +++ b/src/hamster/client.py @@ -221,6 +221,14 @@ def add_fact(self, fact, temporary_activity = False): return new_id + def resume_tracking(self, continuous = False): + """Resume tracking last activity. + Parameters: + b continuous: Use the previous end time as start time to fill the untracked gap. + Default is False. + """ + return self.conn.ResumeTracking(continuous) + def stop_tracking(self, end_time = None): """Stop tracking current activity. end_time can be passed in if the activity should have other end time than the current moment""" diff --git a/src/hamster/storage/storage.py b/src/hamster/storage/storage.py index 82e551917..3feca76f6 100644 --- a/src/hamster/storage/storage.py +++ b/src/hamster/storage/storage.py @@ -145,6 +145,14 @@ def stop_tracking(self, end_time): self.__touch_fact(facts[-1], end_time) self.facts_changed() + def resume_tracking(self, continuous=False): + '''Resume tracking the last activity''' + facts = self.__get_todays_facts() + if facts and facts[-1].end_time: + start_time = facts[-1].end_time if continuous else dt.datetime.now() + self.add_fact(facts[-1].copy(start_time=start_time, + end_time=None)) + self.facts_changed() def stop_or_restart_tracking(self): """Stops or restarts tracking the last activity"""