Skip to content

Commit

Permalink
feat: add resume command
Browse files Browse the repository at this point in the history
Support resuming the last tracked activity.

When `hamster resume` is called, the last tracked activity is
started, i.e., the same activity is started again with the current
start_time.

If `hamster resume --continuous` (or short with `-c`)
is called, the last tracked activity continues to be tracked, i.e.,
its end_time is removed.

Signed-off-by: Olaf Lessenich <[email protected]>
  • Loading branch information
xai committed Apr 28, 2023
1 parent 7013263 commit 2f30b7e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/hamster-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ def start(self, *args):
return id_


def resume(self, *args):
'''Resume the last activity.'''
facts = self.storage.get_todays_facts()
if facts and facts[-1].end_time:
continuous = False
if args and (args[0] == "--continuous" or args[0] == "-c"):
continuous = True
self.storage.resume_tracking(continuous=continuous)
else:
print((_("No activity to resume")))


def stop(self, *args):
'''Stop tracking the current activity.'''
self.storage.stop_tracking()
Expand Down
8 changes: 8 additions & 0 deletions src/hamster-service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions src/hamster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
13 changes: 13 additions & 0 deletions src/hamster/storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ 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:
if continuous:
self.__touch_fact(facts[-1], None)
else:
start_time = dt.datetime.now()
self.add_fact(facts[-1].copy(start_time=start_time,
end_time=None))
self.facts_changed()
else:
logger.warning("No activity to resume")

def stop_or_restart_tracking(self):
"""Stops or restarts tracking the last activity"""
Expand Down

0 comments on commit 2f30b7e

Please sign in to comment.