ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
ansi.c
Go to the documentation of this file.
1
7#include "ansi.h"
8#include "output_buffer.h"
9#include "common.h"
10
11#include <string.h>
12
13char *ansi_strip_escapes(const char *input, size_t input_len) {
14 if (!input || input_len == 0) {
15 return NULL;
16 }
17
18 // Output will be at most input_len (stripping only removes chars)
19 outbuf_t ob = {0};
20 ob_reserve(&ob, input_len);
21
22 size_t i = 0;
23 while (i < input_len) {
24 // Check for ESC character (start of ANSI sequence)
25 if (input[i] == '\x1b' && i + 1 < input_len && input[i + 1] == '[') {
26 // Skip ESC[
27 i += 2;
28
29 // Skip parameter bytes (digits, semicolons, and intermediate bytes)
30 while (i < input_len) {
31 char c = input[i];
32 // Parameter bytes: 0x30-0x3F (digits, semicolon, etc.)
33 // Intermediate bytes: 0x20-0x2F (space, !, ", etc.)
34 if ((c >= 0x30 && c <= 0x3F) || (c >= 0x20 && c <= 0x2F)) {
35 i++;
36 } else {
37 break;
38 }
39 }
40
41 // Skip final byte (0x40-0x7E: @, A-Z, [, \, ], ^, _, `, a-z, {, |, }, ~)
42 if (i < input_len) {
43 char final = input[i];
44 if (final >= 0x40 && final <= 0x7E) {
45 i++;
46 }
47 }
48 } else {
49 // Regular character - copy to output
50 ob_putc(&ob, input[i]);
51 i++;
52 }
53 }
54
55 ob_term(&ob);
56 return ob.buf;
57}
char * ansi_strip_escapes(const char *input, size_t input_len)
Strip all ANSI escape sequences from a string.
Definition ansi.c:13
ANSI escape sequence utilities.
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 with ANSI Sequence Support.
Dynamic output buffer (auto-expanding)
char * buf
Buffer pointer (allocated, owned by caller, must be freed)