From c4f5dbab6e377fe7ee6040c3d5038157b58db621 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Sat, 1 May 2021 15:58:35 +0200 Subject: actions: Allow specifying a unit suffix for --around Up until now it was only possible to give --around a range specified in hours. Since most discussions on IRC tend not to last for multiple hours, and one may only be interested in a few minutes of logs, this commit adds the ability to specify a unit suffix that takes either 'm' for minutes or 'h' for hours. If it is missing, 'h' is assumed. --- quarg/actions.py | 18 +++++++++++++++--- quarg/main.py | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/quarg/actions.py b/quarg/actions.py index 990327e..9361ac2 100644 --- a/quarg/actions.py +++ b/quarg/actions.py @@ -1,4 +1,5 @@ import argparse +import re from abc import ABCMeta, abstractmethod import dateutil.relativedelta @@ -49,14 +50,25 @@ class ParseAround(argparse.Action): if not rangespec: errx('Missing range for --around') + match = re.match(r'^(?P\d+)(?P[hm])?$', rangespec) + + if not match: + errx(f'Invalid range for --around: \'{rangespec}\'') + + unit = match.group('unit') + try: - hour_range = int(rangespec) + time_range = int(match.group('range')) except ValueError as err: errx(f'Error when parsing range for --around: {err}') else: - datespec, hour_range = (aroundspec, 12) + datespec, time_range, unit = (aroundspec, 12, 'h') date = parse_isodate(datespec) - offset = dateutil.relativedelta.relativedelta(hours=hour_range) + if unit == 'm': + offset = dateutil.relativedelta.relativedelta(minutes=time_range) + else: + offset = dateutil.relativedelta.relativedelta(hours=time_range) + setattr(namespace, self.dest, (date - offset, date + offset)) diff --git a/quarg/main.py b/quarg/main.py index 083166c..82474b8 100644 --- a/quarg/main.py +++ b/quarg/main.py @@ -30,7 +30,7 @@ matchers.add_argument('-f', action=actions.ParseMessageFlag, dest='msgflag', hel date_matchers = cli.add_argument_group('matching message timestamps') date_matchers.add_argument('--after', action=actions.ParseDate, metavar='DATE', help='sent after this date') date_matchers.add_argument('--before', action=actions.ParseDate, metavar='DATE', help='sent before this date') -date_matchers.add_argument('--around', action=actions.ParseAround, metavar='DATE[/RANGE]', help='sent RANGE hours before or after this date') +date_matchers.add_argument('--around', action=actions.ParseAround, metavar='DATE[/RANGE]', help='sent RANGE hours/minutes before or after this date') joined_group = matchers.add_mutually_exclusive_group() joined_group.add_argument('--joined', default=None, action='store_true', dest='joined', help='match buffers which are currently joined') -- cgit v1.2.3-2-gb3c3