From 981ad8830949dc0e9b9691cbb5d65df9e599e2ed Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 15 Apr 2022 13:50:40 +0530 Subject: [PATCH] Allow nuking expandvars globally --- kitty/utils.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/kitty/utils.py b/kitty/utils.py index 15cbcfe44..4208475df 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -34,6 +34,24 @@ else: Options = object +class Flag: + + def __init__(self, initial_val: bool = True) -> None: + self.val = initial_val + + def __enter__(self) -> None: + self.val ^= True + + def __exit__(self, *a: object) -> None: + self.val ^= True + + def __bool__(self) -> bool: + return self.val + + +disallow_expand_vars = Flag(False) + + def expandvars(val: str, env: Mapping[str, str] = {}, fallback_to_os_env: bool = True) -> str: ''' Expand $VAR and ${VAR} Use $$ for a literal $ @@ -48,7 +66,7 @@ def expandvars(val: str, env: Mapping[str, str] = {}, fallback_to_os_env: bool = result = m.group() return result - if '$' not in val: + if disallow_expand_vars or '$' not in val: return val return re.sub(r'\$(?:(\w+)|\{([^}]+)\})', sub, val.replace('$$', '\0')).replace('\0', '$')