Skip to content

Commit

Permalink
Fix goto CLI handler
Browse files Browse the repository at this point in the history
see #47
  • Loading branch information
clobrano committed Jul 29, 2024
1 parent f57a051 commit b4a440a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ clean: clean_src

clean_src:
-rm -r src/__pycache__


replace:
make clean && make build && pip install .
8 changes: 2 additions & 6 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ def main():
is_ok, msg = handlers.stop_task_handler(" ".join(args["<time>"]))

elif args["goto"]:
name = " ".join(args["<newtask>"])
tid, is_ok = guess_task_id_from_string(name)
if not is_ok:
msg = f"could not get task ID from: {name}"

is_ok, msg = handlers.goto_task_handler(tid)
description = " ".join(args["<newtask>"])
is_ok, msg = handlers.goto_task_handler(description)

if args["see"]:
if args["<query>"]:
Expand Down
28 changes: 17 additions & 11 deletions src/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def autocomplete_handler():
return autocomplete()


def start_task_handler(description: str, start_str: str) -> (bool, str):
def start_task_handler(description: str, start_str: str="") -> (bool, str):
"""handles a request to start a task"""
if not description:
return False, "task description is mandatory"
Expand Down Expand Up @@ -70,15 +70,21 @@ def stop_task_handler(stop_time: str) -> (bool, str):
return True, msg


def goto_task_handler(tid: int) -> (bool, str):
def goto_task_handler(description: str) -> (bool, str):
"""handles a request to switch to another task given the ID"""
tasks = get_tasks(lambda x: x.tid == tid)
if not tasks:
return False, f"could not find task with ID {tid}"
task = tasks[0]
if not description:
return False, "task description is mandatory"

if Task.get_running():
now = datetime.now().strftime("%H:%M")
is_ok, msg = stop_task_handler(now)
if not is_ok:
return False, msg

tid, got_id = guess_task_id_from_string(description)
if got_id:
work_on(task_id=tid)
return True, ""
else:
return Task(description).start(), ""

now = datetime.now().strftime("%H:%M")
is_ok, msg = stop_task_handler(now)
if not is_ok:
return False, msg
return start_task_handler(task.name, now)

0 comments on commit b4a440a

Please sign in to comment.