diff --git a/gcalcli/argparsers.py b/gcalcli/argparsers.py index 3faedf9d..b198370a 100644 --- a/gcalcli/argparsers.py +++ b/gcalcli/argparsers.py @@ -330,18 +330,15 @@ def get_argument_parser(): calw.add_argument('weeks', type=int, default=1, nargs='?') free = sub.add_parser( - 'free', parents=[details_parser, output_parser, cal_query_parser], + 'free', parents=[details_parser, output_parser, start_end_parser], help='get free', description='Get free timeslots in week.') - free.add_argument('weeks', type=int, default=1, nargs='?') sub.add_parser( 'calm', parents=[details_parser, output_parser, cal_query_parser], help='get a month agenda in calendar format', description='Get a month agenda in calendar format.') - - quick = sub.add_parser( 'quick', parents=[details_parser, remind_parser], help='quick-add an event to a calendar', diff --git a/gcalcli/cli.py b/gcalcli/cli.py index 04c45b5a..bb325d16 100755 --- a/gcalcli/cli.py +++ b/gcalcli/cli.py @@ -173,9 +173,8 @@ def main(): ) elif parsed_args.command == 'free': - print(parsed_args) - gcal.FreeQuery(start=parsed_args.start) - + gcal.FreeQuery(start=parsed_args.start, + end=parsed_args.end) elif parsed_args.command == 'calm': gcal.CalQuery(parsed_args.command, start_text=parsed_args.start) diff --git a/gcalcli/gcal.py b/gcalcli/gcal.py index 2e7aaeb6..a161409e 100644 --- a/gcalcli/gcal.py +++ b/gcalcli/gcal.py @@ -1157,6 +1157,7 @@ def ListAllCalendars(self): def _display_free(self, start, end, search=None, year_date=False): event_list = self._search_for_events(start, end, search) + day = start.replace(hour=0, minute=0, second=0, microsecond=0) dayst = start.replace(hour=9, minute=0, second=0, microsecond=0) nextst = dayst @@ -1166,24 +1167,40 @@ def _display_free(self, start, end, search=None, self.printer.msg(f'\nAvailability {dayst.strftime("%b %d")} - {end.strftime("%b %d")} \n', 'yellow') self.printer.msg('---------------------------- \n\n', 'yellow') for event in event_list: - while event['s'] > dayend: self.printer.msg(prefix, 'green') - self.printer.msg(f" {nextst.strftime('%H:%M')} to {dayend.strftime('%H:%M')}\n", ) + self.printer.msg(f" {nextst.strftime('%H:%M')} to ") + self.printer.msg(f"{dayend.strftime('%H:%M')}\n") dayst = dayst + timedelta(days=1) dayend = dayend + timedelta(days=1) nextst = dayst print_prefix = True prefix = dayst.strftime('\n%a %b %d') - if event['s'] >= nextst: + if event['s'] > nextst: + self.printer.msg(prefix, 'green') - self.printer.msg(f" {nextst.strftime('%H:%M')} to {event['s'].strftime('%H:%M')}\n") + self.printer.msg(f" {nextst.strftime('%H:%M')} to ") + self.printer.msg(f"{event['s'].strftime('%H:%M')}\n") nextst = event['e'] prefix = ' ' - if event['e'] < dayend: - print(prefix + f" {nextst.strftime('%H:%M')} to {dayend.strftime('%H:%M')}") - + if (event['s'] <= nextst) and (event['e'] < dayend): + nextst = event['e'] + # last event: + if event['e'] < dayend: + self.printer.msg(prefix, 'green') + self.printer.msg(f" {nextst.strftime('%H:%M')} to ") + self.printer.msg(f"{event['s'].strftime('%H:%M')}\n") + + # any trailing days with no events: + dayst = dayst + timedelta(days=1) + dayend = dayend + timedelta(days=1) + while dayst < end: + prefix = dayst.strftime('\n%a %b %d') + self.printer.msg(prefix, 'green') + self.printer.msg(f" {dayst.strftime('%H:%M')} to {dayend.strftime('%H:%M')}\n", ) + dayst = dayst + timedelta(days=1) + dayend = dayend + timedelta(days=1) def _display_queried_events(self, start, end, search=None, year_date=False): @@ -1245,12 +1262,11 @@ def AgendaQuery(self, start=None, end=None): return self._display_queried_events(start, end) - def FreeQuery(self, start=None, count=1): + def FreeQuery(self, start=None, end=None, count=1): if not start: start = self.now.replace(hour=0, minute=0, second=0, microsecond=0) - - end = (start + timedelta(days=(count * 8))) - + if not end: + end = (start + timedelta(days=(count * 8))) return self._display_free(start, end) def CalQuery(self, cmd, start_text='', count=1):