Coverage for src/types/strings.py: 100.00%

65 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 21:20 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from typing import List 

5 

6# Components 

7from ..prints.colors import Colors 

8 

9# Strings class 

10class Strings: 

11 

12 # Center 

13 @staticmethod 

14 def center(string: str, length: int) -> str: 

15 

16 # Extract text 

17 text: str = Colors.strip(string) 

18 

19 # Center string 

20 if len(text) < length: 

21 paddings = length - len(text) 

22 left = paddings // 2 

23 right = -(-paddings // 2) 

24 return ' ' * left + string + ' ' * right 

25 

26 # Default string 

27 return string 

28 

29 # Quote 

30 @staticmethod 

31 def quote(string: str) -> str: # pragma: no cover 

32 

33 # Single quotes 

34 if '\'' not in string: 

35 return '\'' + string + '\'' 

36 

37 # Double quotes 

38 if '\"' not in string: 

39 return '\"' + string + '\"' 

40 

41 # Adaptive quotes 

42 return '\'' + string.replace('\'', '\\\'') + '\'' 

43 

44 # Wrap 

45 @staticmethod 

46 def wrap(string: str, length: int) -> List[str]: 

47 

48 # Variables 

49 color: str = '' 

50 index: int = 0 

51 line_data: str = '' 

52 line_length: int = 0 

53 lines: List[str] = [] 

54 space: str = '' 

55 word: str = '' 

56 

57 # Length limitations 

58 length = max(length, 1) 

59 

60 # Append line 

61 def append_line() -> None: 

62 nonlocal line_data, line_length, lines 

63 if line_length > 0: 

64 lines += [line_data] 

65 line_data = '' 

66 line_length = 0 

67 

68 # Store word 

69 def store_word() -> None: 

70 nonlocal color, length, line_data, line_length, space, word 

71 if len(word) > 0: 

72 

73 # Word overflows 

74 if line_length + len(space) + len(word) > length: 

75 word_full = word 

76 if len(word_full) > length: 

77 while len(word_full) > 0: 

78 word = word_full[0:length] 

79 store_word() 

80 word_full = word_full[length:] 

81 append_line() 

82 

83 # Word spacing 

84 if line_length > 0 and space: 

85 line_data += space 

86 line_length += len(space) 

87 space = '' 

88 

89 # Word appendation 

90 line_data += color + word 

91 line_length += len(word) 

92 word = '' 

93 

94 # Line wrapping 

95 if line_length >= length: 

96 append_line() 

97 

98 # Add char 

99 def add_char(char: str) -> None: 

100 nonlocal index, word 

101 index += 1 

102 word += char 

103 

104 # Iterate through chars 

105 while index < len(string): 

106 

107 # Space separator 

108 if string[index] == ' ': 

109 

110 # Store last word 

111 store_word() 

112 

113 # Reset word data 

114 space += ' ' 

115 index += 1 

116 continue 

117 

118 # Color marker 

119 for item in Colors.ALL: 

120 if item and string[index:].startswith(item): 

121 

122 # Store last word 

123 store_word() 

124 

125 # Store new color 

126 color = item 

127 index += len(color) 

128 break 

129 

130 # Text content 

131 else: 

132 

133 # Append character 

134 add_char(string[index]) 

135 

136 # Store last word 

137 store_word() 

138 

139 # Append last line 

140 append_line() 

141 

142 # Result 

143 return lines