Skip to content

Commit 0180b08

Browse files
committed
feat: add commands resume and continue
Support resuming/continuing 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 continue` (or, for convience, hamster resume `--no-gap`) is called, the last tracked activity continues to be tracked, i.e., its end_time is removed and there is no gap in the time tracking. Signed-off-by: Olaf Lessenich <olessenich@eclipsesource.com>
1 parent 9eb80bc commit 0180b08

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/hamster-cli.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ def start(self, *args):
261261
return id_
262262

263263

264+
def resume(self, *args, no_gap=False):
265+
'''Resume the last activity.'''
266+
facts = self.storage.get_todays_facts()
267+
if facts and facts[-1].end_time:
268+
no_gap = no_gap or "--no-gap" in args
269+
self.storage.resume_tracking(no_gap)
270+
else:
271+
print((_("No activity to resume")))
272+
273+
264274
def stop(self, *args):
265275
'''Stop tracking the current activity.'''
266276
self.storage.stop_tracking()
@@ -419,6 +429,8 @@ def version(self):
419429
Actions:
420430
* add [activity [start-time [end-time]]]: Add an activity
421431
* stop: Stop tracking current activity.
432+
* resume: The last tracked activity is tracked again from now
433+
* continue: The last tracked activity is continued without a gap since its last end time
422434
* list [start-date [end-date]]: List activities
423435
* search [terms] [start-date [end-date]]: List activities matching a search
424436
term
@@ -488,13 +500,17 @@ def version(self):
488500
else:
489501
action = args.action
490502

491-
if action in ("about", "add", "edit", "overview", "preferences"):
503+
if action in ("about", "add", "edit", "overview", "preferences", "continue"):
492504
if action == "add" and args.action_args:
493505
assert not unknown_args, "unknown options: {}".format(unknown_args)
494506
# directly add fact from arguments
495507
id_ = hamster_client.start(*args.action_args)
496508
assert id_ > 0, "failed to add fact"
497509
sys.exit(0)
510+
elif action == "continue":
511+
assert not unknown_args, "unknown options: {}".format(unknown_args)
512+
hamster_client.resume(*args.action_args, no_gap=True)
513+
sys.exit(0)
498514
else:
499515
app.register()
500516
if action == "edit":

src/hamster-service.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ def StopTracking(self, end_time):
281281
end_time = dt.datetime.utcfromtimestamp(end_time)
282282
return self.stop_tracking(end_time)
283283

284+
@dbus.service.method("org.gnome.Hamster")
285+
def ResumeTracking(self, no_gap = False):
286+
"""Resumes tracking the last activity.
287+
Parameters:
288+
b no_gap: Use the previous end time as start time to fill the untracked gap.
289+
Default is False.
290+
"""
291+
return self.resume_tracking(no_gap)
284292

285293
@dbus.service.method("org.gnome.Hamster")
286294
def StopOrRestartTracking(self):

src/hamster.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _hamster()
1717
#
1818
# The basic options we'll complete.
1919
#
20-
opts="activities categories current export list search start stop "
20+
opts="activities categories current export list search start stop resume continue "
2121

2222

2323
#

src/hamster/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ def add_fact(self, fact, temporary_activity = False):
221221

222222
return new_id
223223

224+
def resume_tracking(self, no_gap = False):
225+
"""Resume tracking last activity.
226+
Parameters:
227+
b no_gap: Use the previous end time as start time to fill the untracked gap.
228+
Default is False.
229+
"""
230+
return self.conn.ResumeTracking(no_gap)
231+
224232
def stop_tracking(self, end_time = None):
225233
"""Stop tracking current activity. end_time can be passed in if the
226234
activity should have other end time than the current moment"""

src/hamster/storage/storage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ def stop_tracking(self, end_time):
145145
self.__touch_fact(facts[-1], end_time)
146146
self.facts_changed()
147147

148+
def resume_tracking(self, no_gap=False):
149+
"""Resume tracking the last activity"""
150+
facts = self.__get_todays_facts()
151+
if facts and facts[-1].end_time:
152+
if no_gap:
153+
self.__touch_fact(facts[-1], None)
154+
else:
155+
start_time = dt.datetime.now()
156+
self.add_fact(facts[-1].copy(start_time=start_time,
157+
end_time=None))
158+
else:
159+
logger.warning("No activity to resume")
148160

149161
def stop_or_restart_tracking(self):
150162
"""Stops or restarts tracking the last activity"""

0 commit comments

Comments
 (0)