diff options
author | Marco Zatta <marco@zatta.me> | 2020-12-07 19:12:19 +0100 |
---|---|---|
committer | Marco Zatta <marco@zatta.me> | 2020-12-07 19:12:19 +0100 |
commit | 96b997802e942937e81d2b8a32d08f00d3f4bc4e (patch) | |
tree | b5d72c24987c54febec6a42831f1027476d215fa /qutebrowser/utils/utils.py | |
parent | 2e65f731b1b615b5cd60417c00b6993c2295e9f8 (diff) | |
download | qutebrowser-96b997802e942937e81d2b8a32d08f00d3f4bc4e.tar.gz qutebrowser-96b997802e942937e81d2b8a32d08f00d3f4bc4e.zip |
Time units added to :later command
The :later command is modified to accept either an integer or time
duration with properly formatted units.
The expected format is XhYmZs, where X, Y and Z are integers
corresponding to hours, minutes or seconds respectively.
The user needs to specify at least one of the units for the command to
work.
In case of integer input, unit is assumed to be seconds.
Diffstat (limited to 'qutebrowser/utils/utils.py')
-rw-r--r-- | qutebrowser/utils/utils.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 31ff5bf50..9fc8e1abc 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -773,3 +773,20 @@ def libgl_workaround() -> None: libgl = ctypes.util.find_library("GL") if libgl is not None: # pragma: no branch ctypes.CDLL(libgl, mode=ctypes.RTLD_GLOBAL) + + +def parse_duration(duration: str) -> int: + """Parse duration in format XhYmZs into milliseconds duration.""" + has_only_valid_chars = re.match("^([0-9]+[shm]?){1,3}$", duration) + if not has_only_valid_chars: + return -1 + if re.match("^[0-9]+$", duration): + seconds = int(duration) + else: + match = re.search("([0-9]+)s", duration) + seconds = match.group(1) if match else 0 + match = re.search("([0-9]+)m", duration) + minutes = match.group(1) if match else 0 + match = re.search("([0-9]+)h", duration) + hours = match.group(1) if match else 0 + return (int(seconds) + int(minutes) * 60 + int(hours) * 3600) * 1000 |