CleanAndCopy removes control characters from output
(dst io.Writer, src io.Reader)
| 191 | |
| 192 | // CleanAndCopy removes control characters from output |
| 193 | func CleanAndCopy(dst io.Writer, src io.Reader) error { |
| 194 | scanner := bufio.NewScanner(src) |
| 195 | // This regex matches ANSI escape codes that are used for terminal text formatting such as color changes. |
| 196 | // \x1b is the ESC character, which starts the escape sequence. |
| 197 | // [^m]* matches any character that is not 'm', multiple times. 'm' is the final character in the sequence. |
| 198 | // This effectively matches any escape sequence starting with ESC and ending with 'm'. |
| 199 | re := regexp.MustCompile(`\x1b[^m]*m`) |
| 200 | for scanner.Scan() { |
| 201 | cleanString := re.ReplaceAllString(scanner.Text(), "") |
| 202 | _, err := io.WriteString(dst, cleanString+"\n") |
| 203 | if err != nil { |
| 204 | return err |
| 205 | } |
| 206 | } |
| 207 | if err := scanner.Err(); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | return nil |
| 211 | } |
no outgoing calls
no test coverage detected