ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
lib/util/display.c
Go to the documentation of this file.
1
9#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}
59
60int display_height(const char *text, int terminal_width) {
61 if (!text || terminal_width <= 0) {
62 return 0;
63 }
64
65 int total_lines = 0;
66 const char *p = text;
67
68 // Count lines by processing the text and counting newlines + final segment
69 while (*p) {
70 // Find the end of this line (next newline or end of string)
71 const char *line_end = p;
72 while (*line_end && *line_end != '\n') {
73 line_end++;
74 }
75
76 // Calculate display width of this line
77 size_t line_len = line_end - p;
78 if (line_len > 0) {
79 // Strip ANSI codes and calculate display width
80 char *stripped = ansi_strip_escapes(p, line_len);
81 if (stripped) {
82 int line_width = utf8_display_width(stripped);
83 SAFE_FREE(stripped);
84
85 if (line_width > 0) {
86 // Calculate how many display lines this wraps to
87 int display_lines = (line_width + terminal_width - 1) / terminal_width; // Ceiling division
88 total_lines += display_lines;
89 } else {
90 total_lines += 1;
91 }
92 } else {
93 // If stripping fails, count as 1 line
94 total_lines += 1;
95 }
96 } else {
97 // Empty line still counts as 1 line
98 total_lines += 1;
99 }
100
101 // Move to next line
102 if (*line_end == '\n') {
103 p = line_end + 1;
104 } else {
105 break;
106 }
107 }
108
109 return total_lines;
110}
ANSI escape sequence utilities and fast color code generation.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
char * ansi_strip_escapes(const char *input, size_t input_len)
Strip all ANSI escape sequences from a string.
Definition ansi.c:23
#define SAFE_FREE(ptr)
Definition common.h:376
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
int display_center_horizontal(const char *text, int terminal_width)
Calculate left padding needed to center text horizontally.
int display_center_vertical(int content_height, int terminal_height)
Calculate top padding needed to vertically center content.
int utf8_display_width(const char *str)
Calculate terminal display width of a UTF-8 string.
Definition utf8.c:46
int display_height(const char *text, int terminal_width)
Calculate how many display lines text will consume when wrapped.
🖥️ Display Width and Terminal Centering Utilities
🔤 UTF-8 Encoding and Decoding Utilities