File: zsh_utils.py

package info (click to toggle)
crazy-complete 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 13,342; sh: 995; makefile: 68
file content (198 lines) | stat: -rw-r--r-- 5,374 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>

'''Utility functions for Zsh.'''

from . import algo
from . import shell
from . import string_stream

# pylint: disable=too-many-branches
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments


def escape_colon(s):
    '''Escape colons in a string with backslash.'''

    return s.replace(':', '\\:')


def escape_square_brackets(s):
    '''Escape square brackets with backslash.'''

    return s.replace('[', '\\[').replace(']', '\\]')


def escape_colon_in_quoted_string(s):
    '''Escape a colon in a single/double quoted string.'''

    if ':' not in s:
        return s

    ss = string_stream.StringStream(s)
    result = ''

    while ss.have():
        c = ss.get()
        if c == "'":
            sub = ss.parse_shell_single_quote(in_quotes=True)
            result += "'%s'" % sub.replace(':', r'\:')
        elif c == '"':
            sub = ss.parse_shell_double_quote(in_quotes=True)
            result += '"%s"' % sub.replace(':', r'\\:')
        elif c == ':':
            result += r'\\:'
        else:
            result += c

    return result


def make_option_spec(
        option_strings,
        conflicting_options = None,
        description         = None,
        complete            = None,
        optional_arg        = False,
        repeatable          = False,
        hidden              = False,
        final               = False,
        metavar             = None,
        action              = None,
        long_opt_arg_sep    = 'both'):

    '''
    Make a Zsh option spec.

    Returns something like this:
        (--option -o){--option=,-o+}[Option description]:Metavar:Action
    '''

    result = []

    if conflicting_options is None:
        conflicting_options = []

    # Hidden option ===========================================================
    if hidden:
        result.append("'!'")

    # Not options =============================================================
    not_options = []

    for o in sorted(conflicting_options):
        not_options.append(escape_colon(o))

    if not repeatable:
        for o in sorted(option_strings):
            not_options.append(escape_colon(o))

    if final:
        not_options = ['- *']

    if not_options:
        result.append(shell.quote('(%s)' % ' '.join(algo.uniq(not_options))))

    # Repeatable option =======================================================
    if repeatable:
        result.append("'*'")

    # Option strings ==========================================================
    if complete and optional_arg is True:
        opts = [o+'-' if len(o) == 2 else o+'=-' for o in option_strings]
    elif complete:
        if long_opt_arg_sep == 'both':
            opts = [o+'+' if len(o) == 2 else o+'=' for o in option_strings]
        elif long_opt_arg_sep == 'equals':
            opts = [o+'+' if len(o) == 2 else o+'=-' for o in option_strings]
        elif long_opt_arg_sep == 'space':
            opts = [o+'+' if len(o) == 2 else o for o in option_strings]
    else:
        opts = option_strings

    if len(opts) == 1:
        result.append(opts[0])
    else:
        result.append('{%s}' % ','.join(opts))

    # Description =============================================================
    if description is not None:
        result.append(shell.quote('[%s]' % escape_colon(escape_square_brackets(description))))

    # Complete ================================================================
    if complete:
        if metavar is None:
            metavar = ' '

        action = escape_colon_in_quoted_string(action)
        result.append(':%s:%s' % (shell.quote(escape_colon(metavar)), action))

    return ''.join(result)


def make_positional_spec(
        number,
        repeatable,
        description,
        action):

    '''
    Make a Zsh positional spec.

    Returns something like this:
        Number:Description:Action
    '''

    if repeatable is True:
        num_spec = "'*'"
    else:
        num_spec = str(number)

    desc_spec = shell.quote(escape_colon(description))

    if action == '_normal':
        return f'{num_spec}::{desc_spec}:{action}'

    action_escaped = escape_colon_in_quoted_string(action)
    return f'{num_spec}:{desc_spec}:{action_escaped}'


def make_file_extension_pattern(extensions, fuzzy):
    '''Make a case-insensitive glob pattern matching `extensions`.

    Takes a list of extensions (e.g. ['txt', 'jpg']) and returns a Zsh-style
    pattern that matches all of them, ignoring case.

    Example output:
        '*.(#i)(txt|jpg)'
    '''

    if len(extensions) == 1:
        pattern = '*.(#i)%s' % extensions[0]
    else:
        pattern = '*.(#i)(%s)' % '|'.join(extensions)

    if fuzzy:
        pattern += '*'

    return pattern


def test():
    '''Tests.'''

    cases = [
        (0,  r'foo',         r'foo'),
        (1,  r':',           r'\\:'),
        (2,  r'":"',         r'"\\:"'),
        (3,  r"':'",         r"'\:'"),
    ]

    for num, instring, expected in cases:
        result = escape_colon_in_quoted_string(instring)
        if result != expected:
            print(f"Test {num} failed:")
            print(f"Having:   {result}")
            print(f"Expected: {expected}")
            raise AssertionError("Test failed")