1+ import time
12from cmd import Cmd
23from objectbox import *
3- import time
4+
45
56@Entity ()
67class Task :
78 id = Id ()
89 text = String ()
9-
1010 date_created = Date (py_type = int )
1111 date_finished = Date (py_type = int )
1212
1313
14-
15- # objectbox expects date timestamp in milliseconds since UNIX epoch
14+ # Objectbox expects date timestamp in milliseconds since UNIX epoch
1615def now_ms () -> int :
17- return time .time_ns () / 1000000
16+ return int ( time .time_ns () / 1000000 )
1817
1918
2019def format_date (timestamp_ms : int ) -> str :
@@ -23,34 +22,63 @@ def format_date(timestamp_ms: int) -> str:
2322
2423class TasklistCmd (Cmd ):
2524 prompt = "> "
26- _store = Store (directory = "tasklist-db" )
27- _box = _store .box (Task )
2825
29- def do_ls (self , _ ):
30- """list tasks"""
26+ def __init__ (self ):
27+ super ().__init__ ()
28+ self ._store = Store (directory = "tasklist-db" )
29+ self ._task_box = self ._store .box (Task )
30+ self ._query = self ._task_box .query ().build ()
3131
32- tasks = self ._box .get_all ()
32+ def add_task (self , text : str ):
33+ task = Task (text = text , date_created = now_ms ())
34+ self ._task_box .put (task )
35+
36+ def remove_task (self , task_id : int ) -> bool :
37+ is_removed = self ._task_box .remove (task_id )
38+ return is_removed
39+
40+ def find_tasks (self ):
41+ query = self ._task_box .query ().build ()
42+ return query .find ()
43+
44+ # *** Command line ***
45+
46+ def do_ls (self , _ ):
47+ """ Lists all the tasks created. """
48+ tasks = self .find_tasks ()
3349
3450 print ("%3s %-29s %-29s %s" % ("ID" , "Created" , "Finished" , "Text" ))
3551 for task in tasks :
3652 print ("%3d %-29s %-29s %s" % (
37- task .id , format_date (task .date_created ), format_date (task .date_finished ), task .text ))
53+ task .id , format_date (task .date_created ), format_date (task .date_finished ), task .text ))
3854
3955 def do_new (self , text : str ):
40- """create a new task with the given text (all arguments concatenated)"""
41- task = Task ()
42- task .text = text
43- task .date_created = now_ms ()
44- self ._box .put (task )
45-
46- def do_done (self , id : str ):
47- """mark task with the given ID as done"""
48- task = self ._box .get (int (id ))
56+ """ Creates a new task with the given text (all arguments concatenated). """
57+ self .add_task (text )
58+
59+ def do_done (self , task_id : str ):
60+ """ Marks the task with the given ID as done. """
61+ if not task_id .isdigit () or int (task_id ) <= 0 :
62+ print (f"Invalid task ID: \" { task_id } \" " )
63+ return
64+ task = self ._task_box .get (int (task_id ))
65+ if task is None :
66+ print (f"Task { task_id } not found" )
67+ return
4968 task .date_finished = now_ms ()
50- self ._box .put (task )
69+ self ._task_box .put (task )
70+
71+ def do_rm (self , task_id : str ):
72+ """ Removes a task given its ID. """
73+ if not task_id .isdigit () or int (task_id ) <= 0 :
74+ print (f"Invalid task ID: \" { task_id } \" " )
75+ return
76+ is_removed = self .remove_task (int (task_id ))
77+ if not is_removed :
78+ print (f"Task { task_id } not found" )
5179
5280 def do_exit (self , _ ):
53- """close the program"""
81+ """ Closes the program. """
5482 raise SystemExit ()
5583
5684
0 commit comments