Skip to content

Commit 43f03c1

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 the `resume` command is called with the option `--no-gap`, the activity is started again with its previous end_time. If `hamster continue` 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 43f03c1

5 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/hamster-cli.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,24 @@ 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+
def continue_(self, *args):
274+
'''Continue the last activity.'''
275+
facts = self.storage.get_todays_facts()
276+
if facts and facts[-1].end_time:
277+
self.storage.continue_tracking()
278+
else:
279+
print((_("No activity to continue")))
280+
281+
264282
def stop(self, *args):
265283
'''Stop tracking the current activity.'''
266284
self.storage.stop_tracking()
@@ -419,6 +437,10 @@ def version(self):
419437
Actions:
420438
* add [activity [start-time [end-time]]]: Add an activity
421439
* stop: Stop tracking current activity.
440+
* resume: The last tracked activity is tracked again from now.
441+
If --no-gap is specified, the activity is restarted without a gap
442+
since its last end time
443+
* continue: The last tracked activity is continued from its last end time.
422444
* list [start-date [end-date]]: List activities
423445
* search [terms] [start-date [end-date]]: List activities matching a search
424446
term
@@ -488,13 +510,17 @@ def version(self):
488510
else:
489511
action = args.action
490512

491-
if action in ("about", "add", "edit", "overview", "preferences"):
513+
if action in ("about", "add", "edit", "overview", "preferences", "continue"):
492514
if action == "add" and args.action_args:
493515
assert not unknown_args, "unknown options: {}".format(unknown_args)
494516
# directly add fact from arguments
495517
id_ = hamster_client.start(*args.action_args)
496518
assert id_ > 0, "failed to add fact"
497519
sys.exit(0)
520+
elif action == "continue":
521+
assert not unknown_args, "unknown options: {}".format(unknown_args)
522+
hamster_client.continue_()
523+
sys.exit(0)
498524
else:
499525
app.register()
500526
if action == "edit":

src/hamster-service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ 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)
292+
293+
@dbus.service.method("org.gnome.Hamster")
294+
def ContinueTracking(self):
295+
"""Continue tracking the last activity"""
296+
return self.continue_tracking()
284297

285298
@dbus.service.method("org.gnome.Hamster")
286299
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ 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+
232+
def continue_tracking(self):
233+
"""Continue tracking last activity."""
234+
return self.conn.ContinueTracking()
235+
224236
def stop_tracking(self, end_time = None):
225237
"""Stop tracking current activity. end_time can be passed in if the
226238
activity should have other end time than the current moment"""

src/hamster/storage/storage.py

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

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

149168
def stop_or_restart_tracking(self):
150169
"""Stops or restarts tracking the last activity"""

0 commit comments

Comments
 (0)