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

67 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-10-18 15:02 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from random import choices 

5from string import ascii_letters, digits 

6from typing import List 

7 

8# Components 

9from ..prints.colors import Colors 

10 

11# Strings class 

12class Strings: 

13 

14 # Center 

15 @staticmethod 

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

17 

18 # Extract text 

19 text: str = Colors.strip(string) 

20 

21 # Center string 

22 if len(text) < length: 

23 paddings = length - len(text) 

24 left = paddings // 2 

25 right = -(-paddings // 2) 

26 return ' ' * left + string + ' ' * right 

27 

28 # Default string 

29 return string 

30 

31 # Quote 

32 @staticmethod 

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

34 

35 # Single quotes 

36 if '\'' not in string: 

37 return '\'' + string + '\'' 

38 

39 # Double quotes 

40 if '\"' not in string: 

41 return '\"' + string + '\"' 

42 

43 # Adaptive quotes 

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

45 

46 # Random 

47 @staticmethod 

48 def random(length: int) -> str: # pragma: no cover 

49 

50 # Generate random string 

51 return ''.join(choices( 

52 ascii_letters + digits, 

53 k=length, 

54 )) 

55 

56 # Wrap 

57 @staticmethod 

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

59 

60 # Variables 

61 color: str = '' 

62 index: int = 0 

63 line_data: str = '' 

64 line_length: int = 0 

65 lines: List[str] = [] 

66 space: str = '' 

67 word: str = '' 

68 

69 # Length limitations 

70 length = max(length, 1) 

71 

72 # Append line 

73 def append_line() -> None: 

74 nonlocal line_data, line_length, lines 

75 if line_length > 0: 

76 lines += [line_data] 

77 line_data = '' 

78 line_length = 0 

79 

80 # Store word 

81 def store_word() -> None: 

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

83 if len(word) > 0: 

84 

85 # Word overflows 

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

87 word_full = word 

88 if len(word_full) > length: 

89 while len(word_full) > 0: 

90 word = word_full[0:length] 

91 store_word() 

92 word_full = word_full[length:] 

93 append_line() 

94 

95 # Word spacing 

96 if line_length > 0 and space: 

97 line_data += space 

98 line_length += len(space) 

99 space = '' 

100 

101 # Word appendation 

102 line_data += color + word 

103 line_length += len(word) 

104 word = '' 

105 

106 # Line wrapping 

107 if line_length >= length: 

108 append_line() 

109 

110 # Add char 

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

112 nonlocal index, word 

113 index += 1 

114 word += char 

115 

116 # Iterate through chars 

117 while index < len(string): 

118 

119 # Space separator 

120 if string[index] == ' ': 

121 

122 # Store last word 

123 store_word() 

124 

125 # Reset word data 

126 space += ' ' 

127 index += 1 

128 continue 

129 

130 # Color marker 

131 for item in Colors.ALL: 

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

133 

134 # Store last word 

135 store_word() 

136 

137 # Store new color 

138 color = item 

139 index += len(color) 

140 break 

141 

142 # Text content 

143 else: 

144 

145 # Append character 

146 add_char(string[index]) 

147 

148 # Store last word 

149 store_word() 

150 

151 # Append last line 

152 append_line() 

153 

154 # Result 

155 return lines