stripAnsiCodes function
Strip ANSI escape codes from output.
Implementation
String stripAnsiCodes(String input) {
// Matches: ESC[ ... m (SGR), ESC[ ... H/J/K (cursor/erase),
// ESC] ... ST (OSC), and other common escape sequences.
return input
.replaceAll(RegExp(r'\x1B\[[0-9;]*[A-Za-z]'), '') // CSI sequences
.replaceAll(
RegExp(r'\x1B\][^\x07\x1B]*(\x07|\x1B\\)'),
'',
) // OSC sequences
.replaceAll(RegExp(r'\x1B[()][AB012]'), '') // Character set selection
.replaceAll(RegExp(r'\x1B[>=<]'), '') // Keypad mode
.replaceAll(RegExp(r'\x1B\[[\?]?[0-9;]*[hlsr]'), '') // Mode set/reset
.replaceAll(RegExp(r'\x1B[78DMEHc]'), '') // Single-char escapes
.replaceAll(RegExp(r'\x1B\[\d*[ABCDEFGH]'), ''); // Remaining cursor moves
}