Skip to content

Commit c605b4e

Browse files
Preserves urls in multi-line snakesay.
Co-authored-by: Piotr Kaznowski <piotr@kazno.dev>
1 parent 0eb2083 commit c605b4e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

snakesay/snakesay.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
import sys
33
import textwrap
4+
import re
45
from typing import Tuple, List, Iterator
56

67
MESSAGE = '\n'.join([
@@ -33,7 +34,11 @@ def speech_bubble_lines(speech) -> Iterator[str]:
3334

3435

3536
def rewrap(speech: str) -> Tuple[List[str], int]:
36-
lines = textwrap.wrap(speech)
37+
url_pattern = r'https?://[^\s]+'
38+
if re.search(url_pattern, speech):
39+
lines = textwrap.wrap(speech, break_long_words=False, break_on_hyphens=False)
40+
else:
41+
lines = textwrap.wrap(speech)
3742
width = max(len(l) for l in lines) if lines else 0
3843
return [line.ljust(width) for line in lines], width
3944

tests/test_snakesay.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ def test_three_lines():
3535

3636
def test_multiple_arguments():
3737
assert '< hi there >' in snakesay('hi', 'there')
38+
39+
40+
def test_long_url_not_split():
41+
long_url = 'https://www.example.com/very/long/path/that/exceeds/normal/line/length/and/should/not/be/split/across/multiple/lines'
42+
43+
message = snakesay(long_url)
44+
45+
assert long_url in message
46+
47+
lines = message.split('\n')
48+
url_containing_lines = [line for line in lines if 'https://' in line]
49+
assert len(url_containing_lines) == 1, "URL should appear on exactly one line"

0 commit comments

Comments
 (0)