(input string)
| 1130 | } |
| 1131 | |
| 1132 | func wrapTextWithAnsi(input string) string { |
| 1133 | scanner := bufio.NewScanner(strings.NewReader(input)) // Create a scanner to read the input string line by line. |
| 1134 | var wrappedBuilder strings.Builder // Builder for the resulting wrapped text. |
| 1135 | currentAnsiCode := "" // Variable to hold the current ANSI escape sequence. |
| 1136 | lastAnsiCode := "" // Variable to hold the last ANSI escape sequence. |
| 1137 | |
| 1138 | // Iterate over each line in the input string. |
| 1139 | for scanner.Scan() { |
| 1140 | line := scanner.Text() // Get the current line. |
| 1141 | |
| 1142 | // If there is a current ANSI code, append it to the builder. |
| 1143 | if currentAnsiCode != "" { |
| 1144 | wrappedBuilder.WriteString(currentAnsiCode) |
| 1145 | } |
| 1146 | |
| 1147 | // Find all ANSI escape sequences in the current line. |
| 1148 | startAnsiCodes := ansiRegex.FindAllString(line, -1) |
| 1149 | if len(startAnsiCodes) > 0 { |
| 1150 | // Update the last ANSI escape sequence to the last one found in the line. |
| 1151 | lastAnsiCode = startAnsiCodes[len(startAnsiCodes)-1] |
| 1152 | } |
| 1153 | |
| 1154 | // Append the current line to the builder. |
| 1155 | wrappedBuilder.WriteString(line) |
| 1156 | |
| 1157 | // Check if the current ANSI code needs to be reset or updated. |
| 1158 | if (currentAnsiCode != "" && !strings.HasSuffix(line, ansiResetCode)) || len(startAnsiCodes) > 0 { |
| 1159 | // If the current line does not end with a reset code or if there are ANSI codes, append a reset code. |
| 1160 | wrappedBuilder.WriteString(ansiResetCode) |
| 1161 | // Update the current ANSI code to the last one found in the line. |
| 1162 | currentAnsiCode = lastAnsiCode |
| 1163 | } else { |
| 1164 | // If no ANSI codes need to be maintained, reset the current ANSI code. |
| 1165 | currentAnsiCode = "" |
| 1166 | } |
| 1167 | |
| 1168 | // Append a newline character to the builder. |
| 1169 | wrappedBuilder.WriteString("\n") |
| 1170 | } |
| 1171 | |
| 1172 | // Return the processed string with properly wrapped ANSI escape sequences. |
| 1173 | return wrappedBuilder.String() |
| 1174 | } |
| 1175 | |
| 1176 | // truncateStrings applies truncation logic to expected and actual strings |
| 1177 | // If both exceed 10000 bytes, truncate both to 10000 bytes |
no test coverage detected