| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Conditions for Fish.''' |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from . import cli |
| 9 |
from . import when |
| 10 |
from . import shell |
| 11 |
from .errors import InternalError |
| 12 |
from .type_utils import is_list_type |
| 13 |
|
| 14 |
|
| 15 |
def escape_in_double(string): |
| 16 |
'''Escape special characters in a double quoted string.''' |
| 17 |
|
| 18 |
return re.sub(r'(["$`\\])', r'\\\1', string) |
| 19 |
|
| 20 |
|
| 21 |
def make_condition_command(prefix, args): |
| 22 |
'''Make a condition command. |
| 23 |
|
| 24 |
In our completion scripts, we use variables like $has_option, |
| 25 |
$positional_contains, $num_of_positionals etc. |
| 26 |
|
| 27 |
We generate code like this: |
| 28 |
|
| 29 |
complete -n "$has_option -f --foo" |
| 30 |
|
| 31 |
This function generates such a condition command. |
| 32 |
|
| 33 |
We have to ensure proper escaping inside the double quotes. |
| 34 |
''' |
| 35 |
|
| 36 |
args_quoted = [shell.quote(arg) for arg in args] |
| 37 |
args_quoted = [escape_in_double(arg) for arg in args_quoted] |
| 38 |
return '%s %s' % (prefix, ' '.join(args_quoted)) |
| 39 |
|
| 40 |
|
| 41 |
class Condition: |
| 42 |
'''Base class for conditions.''' |
| 43 |
|
| 44 |
def get_code(self, ctxt): |
| 45 |
'''Returns the condition code.''' |
| 46 |
raise NotImplementedError |
| 47 |
|
| 48 |
|
| 49 |
class Not(Condition): |
| 50 |
'''Logical not.''' |
| 51 |
|
| 52 |
def __init__(self, conditional): |
| 53 |
self.conditional = conditional |
| 54 |
|
| 55 |
def get_code(self, ctxt): |
| 56 |
return 'not %s' % self.conditional.get_code(ctxt) |
| 57 |
|
| 58 |
|
| 59 |
class HasOption(Condition): |
| 60 |
'''Checks if an option is present on command line.''' |
| 61 |
|
| 62 |
def __init__(self, option_strings): |
| 63 |
if not is_list_type(option_strings): |
| 64 |
raise InternalError('option_strings: Invalid type') |
| 65 |
|
| 66 |
self.option_strings = option_strings |
| 67 |
|
| 68 |
def get_code(self, ctxt): |
| 69 |
ctxt.helpers.use_function('has_option') |
| 70 |
return make_condition_command('$has_option', self.option_strings) |
| 71 |
|
| 72 |
|
| 73 |
class HasHiddenOption(Condition): |
| 74 |
'''Checks if an incomplete option is present on command line.''' |
| 75 |
|
| 76 |
def __init__(self, option_strings): |
| 77 |
if not is_list_type(option_strings): |
| 78 |
raise InternalError('option_strings: Invalid type') |
| 79 |
|
| 80 |
self.option_strings = option_strings |
| 81 |
|
| 82 |
def get_code(self, ctxt): |
| 83 |
ctxt.helpers.use_function('has_option') |
| 84 |
ctxt.helpers.use_function('has_option', 'with_incomplete') |
| 85 |
options = self.option_strings |
| 86 |
return make_condition_command('$has_option', ['WITH_INCOMPLETE'] + options) |
| 87 |
|
| 88 |
|
| 89 |
class OptionIs(Condition): |
| 90 |
'''Checks if an option has a specific value.''' |
| 91 |
|
| 92 |
def __init__(self, option_strings, values, ignore_case, any_options): |
| 93 |
if not is_list_type(option_strings): |
| 94 |
raise InternalError('option_strings: Invalid type') |
| 95 |
|
| 96 |
if not is_list_type(values): |
| 97 |
raise InternalError('values: Invalid type') |
| 98 |
|
| 99 |
self.option_strings = option_strings |
| 100 |
self.values = values |
| 101 |
self.ignore_case = ignore_case |
| 102 |
self.any_options = any_options |
| 103 |
|
| 104 |
def get_code(self, ctxt): |
| 105 |
args = ['--', *self.option_strings, '--', *self.values] |
| 106 |
ctxt.helpers.use_function('option_is') |
| 107 |
|
| 108 |
if self.any_options: |
| 109 |
ctxt.helpers.use_function('option_is', 'any') |
| 110 |
args.insert(0, '-a') |
| 111 |
|
| 112 |
if self.ignore_case: |
| 113 |
ctxt.helpers.use_function('option_is', 'nocase') |
| 114 |
args.insert(0, '-i') |
| 115 |
|
| 116 |
return make_condition_command('$option_is', args) |
| 117 |
|
| 118 |
|
| 119 |
class OptionMatch(Condition): |
| 120 |
'''Checks if an option's value matches a regex''' |
| 121 |
|
| 122 |
def __init__(self, option_strings, regex, ignore_case, any_options): |
| 123 |
if not is_list_type(option_strings): |
| 124 |
raise InternalError('option_strings: Invalid type') |
| 125 |
|
| 126 |
if not isinstance(regex, str): |
| 127 |
raise InternalError('regex: Invalid type') |
| 128 |
|
| 129 |
self.option_strings = option_strings |
| 130 |
self.regex = regex |
| 131 |
self.ignore_case = ignore_case |
| 132 |
self.any_options = any_options |
| 133 |
|
| 134 |
def get_code(self, ctxt): |
| 135 |
ctxt.helpers.use_function('option_match') |
| 136 |
args = ['--', *self.option_strings, '--', self.regex] |
| 137 |
|
| 138 |
if self.any_options: |
| 139 |
ctxt.helpers.use_function('option_match', 'any') |
| 140 |
args.insert(0, '-a') |
| 141 |
|
| 142 |
if self.ignore_case: |
| 143 |
ctxt.helpers.use_function('option_match', 'nocase') |
| 144 |
args.insert(0, '-i') |
| 145 |
|
| 146 |
return make_condition_command('$option_match', args) |
| 147 |
|
| 148 |
|
| 149 |
class PositionalNum(Condition): |
| 150 |
'''Check the number of positionals.''' |
| 151 |
|
| 152 |
TEST_OPERATORS = { |
| 153 |
'==': '-eq', |
| 154 |
'!=': '-ne', |
| 155 |
'<=': '-le', |
| 156 |
'>=': '-ge', |
| 157 |
'<': '-lt', |
| 158 |
'>': '-gt', |
| 159 |
} |
| 160 |
|
| 161 |
def __init__(self, operator, number): |
| 162 |
if operator not in ('==', '!=', '<', '<=', '>', '>='): |
| 163 |
raise InternalError(f'operator: Invalid value: {operator}') |
| 164 |
|
| 165 |
if not isinstance(number, int): |
| 166 |
raise InternalError(f'number: Invalid type: {number}') |
| 167 |
|
| 168 |
self.operator = operator |
| 169 |
self.number = number |
| 170 |
|
| 171 |
def get_code(self, ctxt): |
| 172 |
ctxt.helpers.use_function('num_of_positionals') |
| 173 |
op = self.TEST_OPERATORS[self.operator] |
| 174 |
return make_condition_command('$num_of_positionals', [op, str(self.number)]) |
| 175 |
|
| 176 |
|
| 177 |
class PositionalContains(Condition): |
| 178 |
'''Checks if a positional contains a specific value.''' |
| 179 |
|
| 180 |
def __init__(self, number, values, ignore_case=False): |
| 181 |
if not isinstance(number, int): |
| 182 |
raise InternalError(f'number: Invalid type: {number}') |
| 183 |
|
| 184 |
if not is_list_type(values): |
| 185 |
raise InternalError(f'values: Invalid type: {values}') |
| 186 |
|
| 187 |
self.number = number |
| 188 |
self.values = values |
| 189 |
self.ignore_case = ignore_case |
| 190 |
|
| 191 |
def get_code(self, ctxt): |
| 192 |
ctxt.helpers.use_function('positional_contains') |
| 193 |
args = [str(self.number), *self.values] |
| 194 |
|
| 195 |
if self.ignore_case: |
| 196 |
ctxt.helpers.use_function('positional_contains', 'nocase') |
| 197 |
args.insert(0, '-i') |
| 198 |
|
| 199 |
return make_condition_command('$positional_contains', args) |
| 200 |
|
| 201 |
|
| 202 |
class PositionalMatch(Condition): |
| 203 |
'''Checks if a positional matches a regular expression.''' |
| 204 |
|
| 205 |
def __init__(self, number, regex, ignore_case=False): |
| 206 |
if not isinstance(number, int): |
| 207 |
raise InternalError(f'number: Invalid type: {number}') |
| 208 |
|
| 209 |
if not isinstance(regex, str): |
| 210 |
raise InternalError(f'regex: Invalid type: {regex}') |
| 211 |
|
| 212 |
self.number = number |
| 213 |
self.regex = regex |
| 214 |
self.ignore_case = ignore_case |
| 215 |
|
| 216 |
def get_code(self, ctxt): |
| 217 |
ctxt.helpers.use_function('positional_match') |
| 218 |
args = [str(self.number), self.regex] |
| 219 |
|
| 220 |
if self.ignore_case: |
| 221 |
ctxt.helpers.use_function('positional_match', 'nocase') |
| 222 |
args.insert(0, '-i') |
| 223 |
|
| 224 |
return make_condition_command('$positional_match', args) |
| 225 |
|
| 226 |
|
| 227 |
def replace_commands(tokens): |
| 228 |
'''Replace when objects by own condition objects.''' |
| 229 |
|
| 230 |
r = [] |
| 231 |
|
| 232 |
for obj in tokens: |
| 233 |
if isinstance(obj, when.OptionIs): |
| 234 |
r.append(OptionIs(obj.options, obj.values, obj.ignore_case, obj.any)) |
| 235 |
|
| 236 |
elif isinstance(obj, when.OptionMatch): |
| 237 |
r.append(OptionMatch(obj.options, obj.regex, obj.ignore_case, obj.any)) |
| 238 |
|
| 239 |
elif isinstance(obj, when.HasOption): |
| 240 |
r.append(HasOption(obj.options)) |
| 241 |
|
| 242 |
elif isinstance(obj, when.PositionalCount): |
| 243 |
r.append(PositionalNum(obj.operator, obj.number)) |
| 244 |
|
| 245 |
elif isinstance(obj, when.PositionalContains): |
| 246 |
r.append(PositionalContains(obj.number, obj.values, obj.ignore_case)) |
| 247 |
|
| 248 |
elif isinstance(obj, when.PositionalMatch): |
| 249 |
r.append(PositionalMatch(obj.number, obj.regex, obj.ignore_case)) |
| 250 |
|
| 251 |
elif isinstance(obj, str): |
| 252 |
r.append(obj) |
| 253 |
|
| 254 |
else: |
| 255 |
raise AssertionError("Not reached") |
| 256 |
|
| 257 |
return r |
| 258 |
|
| 259 |
|
| 260 |
class Conditions(Condition): |
| 261 |
'''Holds a list of conditions.''' |
| 262 |
|
| 263 |
def __init__(self): |
| 264 |
self.conditions = [] |
| 265 |
self.when = None |
| 266 |
|
| 267 |
def _optimized_conditions(self): |
| 268 |
positional_contains = [] |
| 269 |
positional_num = [] |
| 270 |
has_option = [] |
| 271 |
other = [] |
| 272 |
|
| 273 |
for condition in self.conditions: |
| 274 |
if isinstance(condition, PositionalContains): |
| 275 |
positional_contains.append(condition) |
| 276 |
elif isinstance(condition, PositionalNum): |
| 277 |
positional_num.append(condition) |
| 278 |
elif isinstance(condition, (HasHiddenOption, HasOption)): |
| 279 |
has_option.append(condition) |
| 280 |
else: |
| 281 |
other.append(condition) |
| 282 |
|
| 283 |
# positional_num is fastest, followed by positional_contains |
| 284 |
r = [] |
| 285 |
r.extend(positional_num) |
| 286 |
r.extend(positional_contains) |
| 287 |
r.extend(has_option) |
| 288 |
r.extend(other) |
| 289 |
return r |
| 290 |
|
| 291 |
def get_conditions(self, ctxt): |
| 292 |
conditions = [] |
| 293 |
|
| 294 |
for condition in self._optimized_conditions(): |
| 295 |
conditions.append('"%s"' % condition.get_code(ctxt)) |
| 296 |
|
| 297 |
if self.when: |
| 298 |
r = [] |
| 299 |
for obj in self.when: |
| 300 |
if obj == '(': |
| 301 |
r.append('begin') |
| 302 |
elif obj == ')': |
| 303 |
r.append(';end') |
| 304 |
elif obj in ('!', '&&', '||'): |
| 305 |
r.append(obj) |
| 306 |
else: |
| 307 |
r.append(obj.get_code(ctxt)) |
| 308 |
conditions.append('"%s"' % ' '.join(r)) |
| 309 |
|
| 310 |
return conditions |
| 311 |
|
| 312 |
def add(self, condition): |
| 313 |
'''Add a condition.''' |
| 314 |
|
| 315 |
self.conditions.append(condition) |
| 316 |
|
| 317 |
def extend(self, conditions): |
| 318 |
'''Add many conditions.''' |
| 319 |
|
| 320 |
self.conditions.extend(conditions) |
| 321 |
|
| 322 |
def add_when(self, tokens): |
| 323 |
'''Add when objects.''' |
| 324 |
|
| 325 |
if not tokens: |
| 326 |
return |
| 327 |
|
| 328 |
self.when = replace_commands(tokens) |