Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/rich/_wrap.py: 18%

38 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1import re 

2from typing import Iterable, List, Tuple 

3 

4from ._loop import loop_last 

5from .cells import cell_len, chop_cells 

6 

7re_word = re.compile(r"\s*\S+\s*") 

8 

9 

10def words(text: str) -> Iterable[Tuple[int, int, str]]: 

11 position = 0 

12 word_match = re_word.match(text, position) 

13 while word_match is not None: 

14 start, end = word_match.span() 

15 word = word_match.group(0) 

16 yield start, end, word 

17 word_match = re_word.match(text, end) 

18 

19 

20def divide_line(text: str, width: int, fold: bool = True) -> List[int]: 

21 divides: List[int] = [] 

22 append = divides.append 

23 line_position = 0 

24 _cell_len = cell_len 

25 for start, _end, word in words(text): 

26 word_length = _cell_len(word.rstrip()) 

27 if line_position + word_length > width: 

28 if word_length > width: 

29 if fold: 

30 chopped_words = chop_cells(word, max_size=width, position=0) 

31 for last, line in loop_last(chopped_words): 

32 if start: 

33 append(start) 

34 

35 if last: 

36 line_position = _cell_len(line) 

37 else: 

38 start += len(line) 

39 else: 

40 if start: 

41 append(start) 

42 line_position = _cell_len(word) 

43 elif line_position and start: 

44 append(start) 

45 line_position = _cell_len(word) 

46 else: 

47 line_position += _cell_len(word) 

48 return divides 

49 

50 

51if __name__ == "__main__": # pragma: no cover 

52 from .console import Console 

53 

54 console = Console(width=10) 

55 console.print("12345 abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ 12345") 

56 print(chop_cells("abcdefghijklmnopqrstuvwxyz", 10, position=2))