ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
lib/util/display.c
Go to the documentation of this file.
1
6#include "../../include/ascii-chat/util/display.h"
7#include "../../include/ascii-chat/util/utf8.h"
8#include "../../include/ascii-chat/video/ansi.h"
9#include "../../include/ascii-chat/common.h"
10#include <stdlib.h>
11#include <string.h>
12
13int display_width(const char *text) {
14 if (!text) {
15 return 0;
16 }
17
18 // Strip ANSI escape codes
19 char *stripped = ansi_strip_escapes(text, strlen(text));
20
21 // Use stripped version if available, otherwise use original text
22 const char *to_measure = stripped ? stripped : text;
23
24 // Calculate display width using UTF-8 aware calculation
25 int width = utf8_display_width(to_measure);
26 SAFE_FREE(stripped);
27
28 // Return 0 for empty or invalid input, otherwise return calculated width
29 return width < 0 ? 0 : width;
30}
31
32int display_center_horizontal(const char *text, int terminal_width) {
33 if (!text || terminal_width <= 0) {
34 return 0;
35 }
36
37 int text_width = display_width(text);
38
39 // If text is empty or wider than terminal, don't add padding
40 if (text_width == 0 || text_width >= terminal_width) {
41 return 0;
42 }
43
44 // Calculate left padding to center
45 return (terminal_width - text_width) / 2;
46}
47
48int display_center_vertical(int content_height, int terminal_height) {
49 if (content_height <= 0 || terminal_height <= 0) {
50 return 0;
51 }
52
53 if (content_height >= terminal_height) {
54 return 0;
55 }
56
57 return (terminal_height - content_height) / 2;
58}
char * ansi_strip_escapes(const char *input, size_t input_len)
Definition ansi.c:13
int display_width(const char *text)
int display_center_horizontal(const char *text, int terminal_width)
int display_center_vertical(int content_height, int terminal_height)
int utf8_display_width(const char *str)
Definition utf8.c:46