ANSI escape sequence utilities.
More...
Go to the source code of this file.
|
| char * | ansi_strip_escapes (const char *input, size_t input_len) |
| | Strip all ANSI escape sequences from a string.
|
| |
ANSI escape sequence utilities.
Definition in file ansi.c.
◆ ansi_strip_escapes()
| char * ansi_strip_escapes |
( |
const char * |
input, |
|
|
size_t |
input_len |
|
) |
| |
Strip all ANSI escape sequences from a string.
Removes all ANSI CSI sequences (ESC [ ... final_byte) from the input, leaving only printable text. Useful for creating plain text output from colorized ASCII art.
- Parameters
-
| input | Input string containing ANSI escape sequences |
| input_len | Length of input string |
- Returns
- Newly allocated string with escapes removed (caller must free), or NULL on error
Definition at line 13 of file ansi.c.
13 {
14 if (!input || input_len == 0) {
15 return NULL;
16 }
17
18
21
22 size_t i = 0;
23 while (i < input_len) {
24
25 if (input[i] == '\x1b' && i + 1 < input_len && input[i + 1] == '[') {
26
27 i += 2;
28
29
30 while (i < input_len) {
31 char c = input[i];
32
33
34 if ((c >= 0x30 && c <= 0x3F) || (c >= 0x20 && c <= 0x2F)) {
35 i++;
36 } else {
37 break;
38 }
39 }
40
41
42 if (i < input_len) {
43 char final = input[i];
44 if (final >= 0x40 && final <= 0x7E) {
45 i++;
46 }
47 }
48 } else {
49
51 i++;
52 }
53 }
54
57}
void ob_term(outbuf_t *ob)
Append null terminator to buffer.
void ob_putc(outbuf_t *ob, char c)
Append a character to buffer.
void ob_reserve(outbuf_t *ob, size_t need)
Reserve buffer space for upcoming writes.
Dynamic output buffer (auto-expanding)
char * buf
Buffer pointer (allocated, owned by caller, must be freed)
References outbuf_t::buf, ob_putc(), ob_reserve(), and ob_term().