ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
invitation.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_INVITATION_LINE_BUFFER_SIZE 80
4
10#include <stdio.h>
11#include <string.h>
12
13static rgb_pixel_t get_rainbow_color_rgb(double position) {
15 rgb_pixel_t color = {0};
17 .color = {.position = position, .out = &color}};
18 animation_apply(&sample, &target);
19 return color;
20}
21
22static void centered_line(frame_buffer_t *buf, const char *text, int bytes,
23 int cols, int frame, bool colors) {
24 int width = utf8_display_width_n(text, (size_t)bytes);
25 int padding = (cols - width) / 2;
26 frame_buffer_printf(buf, "%*s", padding > 0 ? padding : 0, "");
27 for (int offset = 0; offset < bytes;) {
28 int character_bytes =
29 utf8_next_char_bytes(text + offset, (size_t)(bytes - offset));
30 if (character_bytes <= 0)
31 character_bytes = 1;
32 bool is_space = character_bytes == 1 && text[offset] == ' ';
33 if (colors && !is_space) {
34 rgb_pixel_t color = get_rainbow_color_rgb((offset + frame / 5.0) / 30.0);
35 frame_buffer_printf(buf, "\033[38;2;%u;%u;%um%.*s\033[0m", color.r,
36 color.g, color.b, character_bytes, text + offset);
37 } else {
38 frame_buffer_printf(buf, "%.*s", character_bytes, text + offset);
39 }
40 offset += character_bytes;
41 }
42 frame_buffer_printf(buf, "\033[K\r\n");
43}
44
45void invitation_render_logo(frame_buffer_t *buf, int cols, int frame,
46 bool colors, bool compact) {
47 const char *ascii_logo[4] = {
48 " __ _ ___ ___(_|_) ___| |__ __ _| |_ ",
49 " / _` / __|/ __| | |_____ / __| '_ \\ / _` | __| ",
50 "| (_| \\__ \\ (__| | |_____| (__| | | | (_| | |_ ",
51 " \\__,_|___/\\___|_|_| \\___|_| |_|\\__,_|\\__| "};
52 if (compact) {
53 const char *title = "ascii-chat";
54 int len = (int)strlen(title);
55 centered_line(buf, title, len < cols ? len : cols, cols, frame, colors);
56 return;
57 }
58 // Pad every row to the same width so the ASCII glyphs stay aligned.
59 int width = 0;
60 for (int i = 0; i < 4; ++i) {
61 int len = (int)strlen(ascii_logo[i]);
62 if (len > width)
63 width = len;
64 }
65 for (int i = 0; i < 4; ++i) {
67 snprintf(line, sizeof(line), "%-*s", width, ascii_logo[i]);
68 centered_line(buf, line, width < cols ? width : cols, cols, frame, colors);
69 }
70}
71
72void invitation_render_rainbow_clouds(frame_buffer_t *buf, int cols, int frame,
73 bool colors, bool unicode) {
74 const char *clouds = unicode ? "╭─╮ ╭─╮ ╭─╮" : "(__) (__) (__)";
75 int bytes = (int)strlen(clouds);
76 centered_line(buf, clouds, bytes, cols, frame + 9, colors);
77}
78
79void invitation_render(frame_buffer_t *buf, terminal_size_t size,
80 const char *session, bool joining, int frame,
81 bool colors) {
82 if (!buf || size.cols < 2 || size.rows < 2)
83 return;
84 int cols = size.cols - 1;
85 int rows = size.rows - 1;
86 bool ready = session && session[0];
87 const char *label = ready ? (joining ? "Connecting to session:"
88 : "Share this string to connect:")
89 : "Creating session...";
90 char command[SESSION_STRING_BUFFER_SIZE + 32];
91 snprintf(command, sizeof(command), "Run: ascii-chat %s",
92 ready ? session : "");
93 const char *lines[3] = {label, ready ? session : "",
94 ready && !joining ? command : ""};
95 int text_rows = 0;
96 for (int i = 0; i < 3; ++i) {
97 if (lines[i][0])
98 text_rows += ((int)strlen(lines[i]) + cols - 1) / cols;
99 }
100 bool compact = cols < 52 || rows < text_rows + 7;
101 int logo_rows = compact ? 1 : 4;
102 bool show_logo = rows >= text_rows + logo_rows + 1;
103 int height = text_rows + (show_logo ? logo_rows + 1 : 0);
104 int top = rows > height ? (rows - height) / 2 : 0;
105 int used = 0;
106 for (; used < top; ++used)
107 frame_buffer_printf(buf, "\n");
108 if (show_logo) {
109 invitation_render_logo(buf, cols, frame, colors, compact);
110 frame_buffer_printf(buf, "\n");
111 used += logo_rows + 1;
112 }
113 // On exceptionally short terminals prioritize the string and its command.
114 int first = height > rows && ready ? 1 : 0;
115 for (int i = first; i < 3 && used < rows; ++i) {
116 const char *text = lines[i];
117 size_t remaining = strlen(text);
118 while (remaining && used < rows) {
119 int length = remaining > (size_t)cols ? cols : (int)remaining;
120 centered_line(buf, text, length, cols, frame, false);
121 text += length;
122 remaining -= length;
123 ++used;
124 }
125 }
126 // Erase stale logs/rows after a layout change without clearing between
127 // frames.
128 frame_buffer_printf(buf, "\033[J");
129}
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
int utf8_display_width_n(const char *str, size_t max_bytes)
Calculate terminal display width of a UTF-8 string up to byte limit.
Definition utf8.c:106
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact)
Definition invitation.c:45
void invitation_render(frame_buffer_t *buf, terminal_size_t size, const char *session, bool joining, int frame, bool colors)
Definition invitation.c:79
#define TUI_INVITATION_LINE_BUFFER_SIZE
Definition invitation.c:3
void invitation_render_rainbow_clouds(frame_buffer_t *buf, int cols, int frame, bool colors, bool unicode)
Definition invitation.c:72
🔤 String Manipulation and Shell Escaping Utilities
#define SESSION_STRING_BUFFER_SIZE
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
🔤 UTF-8 Encoding and Decoding Utilities
asciichat_error_t animation_apply(const animation_sample_t *s, animation_target_t *t)
@ ANIMATION_SPLASH_RAINBOW
@ ANIMATION_TARGET_COLOR