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
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-21 21:20 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from typing import List
6# Components
7from ..prints.colors import Colors
9# Strings class
10class Strings:
12 # Center
13 @staticmethod
14 def center(string: str, length: int) -> str:
16 # Extract text
17 text: str = Colors.strip(string)
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
26 # Default string
27 return string
29 # Quote
30 @staticmethod
31 def quote(string: str) -> str: # pragma: no cover
33 # Single quotes
34 if '\'' not in string:
35 return '\'' + string + '\''
37 # Double quotes
38 if '\"' not in string:
39 return '\"' + string + '\"'
41 # Adaptive quotes
42 return '\'' + string.replace('\'', '\\\'') + '\''
44 # Wrap
45 @staticmethod
46 def wrap(string: str, length: int) -> List[str]:
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 = ''
57 # Length limitations
58 length = max(length, 1)
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
68 # Store word
69 def store_word() -> None:
70 nonlocal color, length, line_data, line_length, space, word
71 if len(word) > 0:
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()
83 # Word spacing
84 if line_length > 0 and space:
85 line_data += space
86 line_length += len(space)
87 space = ''
89 # Word appendation
90 line_data += color + word
91 line_length += len(word)
92 word = ''
94 # Line wrapping
95 if line_length >= length:
96 append_line()
98 # Add char
99 def add_char(char: str) -> None:
100 nonlocal index, word
101 index += 1
102 word += char
104 # Iterate through chars
105 while index < len(string):
107 # Space separator
108 if string[index] == ' ':
110 # Store last word
111 store_word()
113 # Reset word data
114 space += ' '
115 index += 1
116 continue
118 # Color marker
119 for item in Colors.ALL:
120 if item and string[index:].startswith(item):
122 # Store last word
123 store_word()
125 # Store new color
126 color = item
127 index += len(color)
128 break
130 # Text content
131 else:
133 # Append character
134 add_char(string[index])
136 # Store last word
137 store_word()
139 # Append last line
140 append_line()
142 # Result
143 return lines