Skip to content

Commit

Permalink
Make shlex_split always return a token
Browse files Browse the repository at this point in the history
Matches behavior of split() so is therefore more intuitive
  • Loading branch information
kovidgoyal committed Sep 2, 2024
1 parent d9b1c8c commit 913ce58
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 8 additions & 0 deletions kitty/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,14 +1215,22 @@ def key_val_matcher(items: Iterable[tuple[str, str]], key_pat: 're.Pattern[str]'

def shlex_split(text: str, allow_ansi_quoted_strings: bool = False) -> Iterator[str]:
s = Shlex(text, allow_ansi_quoted_strings)
yielded = False
while (q := s.next_word())[0] > -1:
yield q[1]
yielded = True
if not yielded:
yield ''


def shlex_split_with_positions(text: str, allow_ansi_quoted_strings: bool = False) -> Iterator[tuple[int, str]]:
s = Shlex(text, allow_ansi_quoted_strings)
yielded = False
while (q := s.next_word())[0] > -1:
yield q
yielded = True
if not yielded:
yield 0, ''


def timed_debug_print(*a: Any, sep: str = ' ', end: str = '\n') -> None:
Expand Down
3 changes: 1 addition & 2 deletions kitty/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ def compile_match_query(exp: str, is_simple: bool = True) -> MatchPatternType:
def decode_cmdline(x: str) -> str:
ctype, sep, val = x.partition('=')
if ctype == 'cmdline':
with suppress(StopIteration):
return next(shlex_split(val, True))
return next(shlex_split(val, True))
elif ctype == 'cmdline_url':
from urllib.parse import unquote
return unquote(val)
Expand Down
2 changes: 1 addition & 1 deletion kitty_tests/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def test_shlex_split(self):
r'x "ab"y \m': ((0, 'x'), (2, 'aby'), (8, 'm')),
r'''x'y"\z'1''': ((0, 'xy"\\z1'),),
r'\abc\ d': ((0, 'abc d'),),
'': (), ' ': (), ' \tabc\n\t\r ': ((2, 'abc'),),
'': ((0, ''),), ' ': ((0, ''),), ' \tabc\n\t\r ': ((2, 'abc'),),
"$'ab'": ((0, '$ab'),),
}.items():
actual = tuple(shlex_split_with_positions(q))
Expand Down

0 comments on commit 913ce58

Please sign in to comment.