| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Utility functions for Zsh.''' |
| 5 |
|
| 6 |
from . import algo |
| 7 |
from . import shell |
| 8 |
from . import string_stream |
| 9 |
|
| 10 |
# pylint: disable=too-many-branches |
| 11 |
# pylint: disable=too-many-arguments |
| 12 |
# pylint: disable=too-many-positional-arguments |
| 13 |
|
| 14 |
|
| 15 |
def escape_colon(s): |
| 16 |
'''Escape colons in a string with backslash.''' |
| 17 |
|
| 18 |
return s.replace(':', '\\:') |
| 19 |
|
| 20 |
|
| 21 |
def escape_square_brackets(s): |
| 22 |
'''Escape square brackets with backslash.''' |
| 23 |
|
| 24 |
return s.replace('[', '\\[').replace(']', '\\]') |
| 25 |
|
| 26 |
|
| 27 |
def escape_colon_in_quoted_string(s): |
| 28 |
'''Escape a colon in a single/double quoted string.''' |
| 29 |
|
| 30 |
if ':' not in s: |
| 31 |
return s |
| 32 |
|
| 33 |
ss = string_stream.StringStream(s) |
| 34 |
result = '' |
| 35 |
|
| 36 |
while ss.have(): |
| 37 |
c = ss.get() |
| 38 |
if c == "'": |
| 39 |
sub = ss.parse_shell_single_quote(in_quotes=True) |
| 40 |
result += "'%s'" % sub.replace(':', r'\:') |
| 41 |
elif c == '"': |
| 42 |
sub = ss.parse_shell_double_quote(in_quotes=True) |
| 43 |
result += '"%s"' % sub.replace(':', r'\\:') |
| 44 |
elif c == ':': |
| 45 |
result += r'\\:' |
| 46 |
else: |
| 47 |
result += c |
| 48 |
|
| 49 |
return result |
| 50 |
|
| 51 |
|
| 52 |
def make_option_spec( |
| 53 |
option_strings, |
| 54 |
conflicting_options = None, |
| 55 |
description = None, |
| 56 |
complete = None, |
| 57 |
optional_arg = False, |
| 58 |
repeatable = False, |
| 59 |
hidden = False, |
| 60 |
final = False, |
| 61 |
metavar = None, |
| 62 |
action = None, |
| 63 |
long_opt_arg_sep = 'both'): |
| 64 |
|
| 65 |
''' |
| 66 |
Make a Zsh option spec. |
| 67 |
|
| 68 |
Returns something like this: |
| 69 |
(--option -o){--option=,-o+}[Option description]:Metavar:Action |
| 70 |
''' |
| 71 |
|
| 72 |
result = [] |
| 73 |
|
| 74 |
if conflicting_options is None: |
| 75 |
conflicting_options = [] |
| 76 |
|
| 77 |
# Hidden option =========================================================== |
| 78 |
if hidden: |
| 79 |
result.append("'!'") |
| 80 |
|
| 81 |
# Not options ============================================================= |
| 82 |
not_options = [] |
| 83 |
|
| 84 |
for o in sorted(conflicting_options): |
| 85 |
not_options.append(escape_colon(o)) |
| 86 |
|
| 87 |
if not repeatable: |
| 88 |
for o in sorted(option_strings): |
| 89 |
not_options.append(escape_colon(o)) |
| 90 |
|
| 91 |
if final: |
| 92 |
not_options = ['- *'] |
| 93 |
|
| 94 |
if not_options: |
| 95 |
result.append(shell.quote('(%s)' % ' '.join(algo.uniq(not_options)))) |
| 96 |
|
| 97 |
# Repeatable option ======================================================= |
| 98 |
if repeatable: |
| 99 |
result.append("'*'") |
| 100 |
|
| 101 |
# Option strings ========================================================== |
| 102 |
if complete and optional_arg is True: |
| 103 |
opts = [o+'-' if len(o) == 2 else o+'=-' for o in option_strings] |
| 104 |
elif complete: |
| 105 |
if long_opt_arg_sep == 'both': |
| 106 |
opts = [o+'+' if len(o) == 2 else o+'=' for o in option_strings] |
| 107 |
elif long_opt_arg_sep == 'equals': |
| 108 |
opts = [o+'+' if len(o) == 2 else o+'=-' for o in option_strings] |
| 109 |
elif long_opt_arg_sep == 'space': |
| 110 |
opts = [o+'+' if len(o) == 2 else o for o in option_strings] |
| 111 |
else: |
| 112 |
opts = option_strings |
| 113 |
|
| 114 |
if len(opts) == 1: |
| 115 |
result.append(opts[0]) |
| 116 |
else: |
| 117 |
result.append('{%s}' % ','.join(opts)) |
| 118 |
|
| 119 |
# Description ============================================================= |
| 120 |
if description is not None: |
| 121 |
result.append(shell.quote('[%s]' % escape_colon(escape_square_brackets(description)))) |
| 122 |
|
| 123 |
# Complete ================================================================ |
| 124 |
if complete: |
| 125 |
if metavar is None: |
| 126 |
metavar = ' ' |
| 127 |
|
| 128 |
action = escape_colon_in_quoted_string(action) |
| 129 |
result.append(':%s:%s' % (shell.quote(escape_colon(metavar)), action)) |
| 130 |
|
| 131 |
return ''.join(result) |
| 132 |
|
| 133 |
|
| 134 |
def make_positional_spec( |
| 135 |
number, |
| 136 |
repeatable, |
| 137 |
description, |
| 138 |
action): |
| 139 |
|
| 140 |
''' |
| 141 |
Make a Zsh positional spec. |
| 142 |
|
| 143 |
Returns something like this: |
| 144 |
Number:Description:Action |
| 145 |
''' |
| 146 |
|
| 147 |
if repeatable is True: |
| 148 |
num_spec = "'*'" |
| 149 |
else: |
| 150 |
num_spec = str(number) |
| 151 |
|
| 152 |
desc_spec = shell.quote(escape_colon(description)) |
| 153 |
|
| 154 |
if action == '_normal': |
| 155 |
return f'{num_spec}::{desc_spec}:{action}' |
| 156 |
|
| 157 |
action_escaped = escape_colon_in_quoted_string(action) |
| 158 |
return f'{num_spec}:{desc_spec}:{action_escaped}' |
| 159 |
|
| 160 |
|
| 161 |
def make_file_extension_pattern(extensions, fuzzy): |
| 162 |
'''Make a case-insensitive glob pattern matching `extensions`. |
| 163 |
|
| 164 |
Takes a list of extensions (e.g. ['txt', 'jpg']) and returns a Zsh-style |
| 165 |
pattern that matches all of them, ignoring case. |
| 166 |
|
| 167 |
Example output: |
| 168 |
'*.(#i)(txt|jpg)' |
| 169 |
''' |
| 170 |
|
| 171 |
if len(extensions) == 1: |
| 172 |
pattern = '*.(#i)%s' % extensions[0] |
| 173 |
else: |
| 174 |
pattern = '*.(#i)(%s)' % '|'.join(extensions) |
| 175 |
|
| 176 |
if fuzzy: |
| 177 |
pattern += '*' |
| 178 |
|
| 179 |
return pattern |
| 180 |
|
| 181 |
|
| 182 |
def test(): |
| 183 |
'''Tests.''' |
| 184 |
|
| 185 |
cases = [ |
| 186 |
(0, r'foo', r'foo'), |
| 187 |
(1, r':', r'\\:'), |
| 188 |
(2, r'":"', r'"\\:"'), |
| 189 |
(3, r"':'", r"'\:'"), |
| 190 |
] |
| 191 |
|
| 192 |
for num, instring, expected in cases: |
| 193 |
result = escape_colon_in_quoted_string(instring) |
| 194 |
if result != expected: |
| 195 |
print(f"Test {num} failed:") |
| 196 |
print(f"Having: {result}") |
| 197 |
print(f"Expected: {expected}") |
| 198 |
raise AssertionError("Test failed") |