From 5d8f1194957da3523a877a8288fc523388f3ce3c Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Thu, 13 Jun 2024 11:59:46 +0200 Subject: [PATCH] removing seconds from timestamp; wider column for name when printing ls/ ps --- lib/TimeSlot.py | 8 +++++--- lib/ui.py | 6 +++--- main.py | 29 +++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/TimeSlot.py b/lib/TimeSlot.py index 0241dca..d084f36 100644 --- a/lib/TimeSlot.py +++ b/lib/TimeSlot.py @@ -24,7 +24,8 @@ class TimeSlot(): @start.setter def start(self, d: datetime): - self._start = d.timestamp() + _d = d.replace(second=0, microsecond=0) + self._start = _d.timestamp() @property def end(self): @@ -34,9 +35,10 @@ class TimeSlot(): @end.setter def end(self, d: datetime): - if d < self.start: + _d = d.replace(second=0, microsecond=0) + if _d < self.start: raise ValueError("End date must be after the start date.") - self._end = d.timestamp() + self._end = _d.timestamp() def end_now(self): self.end = datetime.now() diff --git a/lib/ui.py b/lib/ui.py index 906606b..e95561f 100644 --- a/lib/ui.py +++ b/lib/ui.py @@ -2,14 +2,14 @@ from .TimeSlot import TimeSlot def convert_to_table(time_slot_list: list[TimeSlot]): - widths = [20, 26, 26] + widths = [30, 26, 26] r_str = f"+{'-'*(widths[0]+2)}+{'-'*(widths[1]+2)}+{'-'*(widths[2]+2)}+\n" - r_str += f"| {'Name':<20} | {'Startzeitpunkt':<26} | {'Endzeitpunkt':<26} |\n" + r_str += f"| {'Name':<30} | {'Startzeitpunkt':<26} | {'Endzeitpunkt':<26} |\n" r_str += f"+{'-'*(widths[0]+2)}+{'-'*(widths[1]+2)}+{'-'*(widths[2]+2)}+\n" for time_slot in time_slot_list: - r_str += f"| {time_slot.name:<20} | {str(time_slot.start):<26} | {str(time_slot.end):<26} |\n" + r_str += f"| {time_slot.name:<30} | {str(time_slot.start):<26} | {str(time_slot.end):<26} |\n" r_str += f"+{'-'*(widths[0]+2)}+{'-'*(widths[1]+2)}+{'-'*(widths[2]+2)}+" diff --git a/main.py b/main.py index c334a5b..b9f95df 100755 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import argparse from lib.DataStore import DataStore from lib.TimeSlot import TimeSlot from lib.ui import convert_to_table +from datetime import datetime def create_parser(): @@ -12,14 +13,15 @@ def create_parser(): subparsers = parser.add_subparsers(dest="command", help="Sub-command help") # Create the parser for the "ls" command - parser_ls = subparsers.add_parser("ls", help="List all events.") + _ = subparsers.add_parser("ls", help="List all events.") # Create the parser for the "ps" command - parser_ps = subparsers.add_parser("ps", help="Show the currently running event.") + _ = subparsers.add_parser("ps", help="Show the currently running event.") # Create the parser for the "add" command parser_add = subparsers.add_parser("add", help="Adding a new event.") - parser_add.add_argument("-b", "--begin", help="Begin datetime") + parser_add.add_argument("name", help="Name of the event") + parser_add.add_argument("-s", "--start", help="Start datetime (default now)") parser_add.add_argument("-e", "--end", help="End datetime") # Create the parser for the "start" command @@ -27,7 +29,7 @@ def create_parser(): parser_start.add_argument("name", help="Name of the event") # Create the parser for the "end" command - parser_end = subparsers.add_parser("end", help="Ending the current event.") + _ = subparsers.add_parser("end", help="Ending the current event.") return parser @@ -62,3 +64,22 @@ if __name__ == "__main__": ds.write_update() print(f"Ended event {running[0].name}.") + elif args.command == "ps": + time_slots = ds.get_all_time_slots() + running = list(filter(lambda x: x.end is None, time_slots)) + print(convert_to_table(running)) + + elif args.command == "add": + time_slots = ds.get_all_time_slots() + running = list(filter(lambda x: x.end is None, time_slots)) + if len(running) > 0 and args.end is None: + print("An event is already running.") + else: + ts = TimeSlot(args.name) + if args.start is not None: + ts.start = datetime.strptime(args.start, '%d.%m.%y %H:%M') + if args.end is not None: + ts.end = datetime.strptime(args.end, '%d.%m.%y %H:%M') + ds.add_time_slot(ts) + ds.write_update() + print("The event was added.")