File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change 11#!/usr/bin/env python
22import sys
33import textwrap
4+ import re
45from typing import Tuple , List , Iterator
56
67MESSAGE = '\n ' .join ([
@@ -33,7 +34,11 @@ def speech_bubble_lines(speech) -> Iterator[str]:
3334
3435
3536def 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
Original file line number Diff line number Diff line change @@ -35,3 +35,15 @@ def test_three_lines():
3535
3636def 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"
You can’t perform that action at this time.
0 commit comments