ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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
9#include <stdio.h>
10#include <string.h>
11
12static rgb_pixel_t get_rainbow_color_rgb(double position) {
14 rgb_pixel_t color = {0};
15 animation_target_t target = {.type = ANIMATION_TARGET_COLOR, .color = {.position = position, .out = &color}};
16 animation_apply(&sample, &target);
17 return color;
18}
19
20static void centered_line(frame_buffer_t *buf, const char *text, int len, int cols, int frame, bool colors) {
21 int padding = (cols - len) / 2;
22 frame_buffer_printf(buf, "%*s", padding > 0 ? padding : 0, "");
23 for (int i = 0; i < len; ++i) {
24 if (colors && text[i] != ' ') {
25 rgb_pixel_t color = get_rainbow_color_rgb((i + frame / 5.0) / 30.0);
26 frame_buffer_printf(buf, "\033[38;2;%u;%u;%um%c\033[0m", color.r, color.g, color.b, text[i]);
27 } else {
28 frame_buffer_printf(buf, "%c", text[i]);
29 }
30 }
31 frame_buffer_printf(buf, "\033[K\r\n");
32}
33
34void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact) {
35 const char *ascii_logo[4] = {
36 " __ _ ___ ___(_|_) ___| |__ __ _| |_ ", " / _` / __|/ __| | |_____ / __| '_ \\ / _` | __| ",
37 "| (_| \\__ \\ (__| | |_____| (__| | | | (_| | |_ ", " \\__,_|___/\\___|_|_| \\___|_| |_|\\__,_|\\__| "};
38 if (compact) {
39 const char *title = "ascii-chat";
40 int len = (int)strlen(title);
41 centered_line(buf, title, len < cols ? len : cols, cols, frame, colors);
42 return;
43 }
44 // Pad every row to the same width so the ASCII glyphs stay aligned.
45 int width = 0;
46 for (int i = 0; i < 4; ++i) {
47 int len = (int)strlen(ascii_logo[i]);
48 if (len > width)
49 width = len;
50 }
51 for (int i = 0; i < 4; ++i) {
53 snprintf(line, sizeof(line), "%-*s", width, ascii_logo[i]);
54 centered_line(buf, line, width < cols ? width : cols, cols, frame, colors);
55 }
56}
57
58void invitation_render(frame_buffer_t *buf, terminal_size_t size, const char *session, bool joining, int frame,
59 bool colors) {
60 if (!buf || size.cols < 2 || size.rows < 2)
61 return;
62 int cols = size.cols - 1;
63 int rows = size.rows - 1;
64 bool ready = session && session[0];
65 const char *label =
66 ready ? (joining ? "Connecting to session:" : "Share this string to connect:") : "Creating session...";
67 char command[SESSION_STRING_BUFFER_SIZE + 32];
68 snprintf(command, sizeof(command), "Run: ascii-chat %s", ready ? session : "");
69 const char *lines[3] = {label, ready ? session : "", ready && !joining ? command : ""};
70 int text_rows = 0;
71 for (int i = 0; i < 3; ++i) {
72 if (lines[i][0])
73 text_rows += ((int)strlen(lines[i]) + cols - 1) / cols;
74 }
75 bool compact = cols < 52 || rows < text_rows + 7;
76 int logo_rows = compact ? 1 : 4;
77 bool show_logo = rows >= text_rows + logo_rows + 1;
78 int height = text_rows + (show_logo ? logo_rows + 1 : 0);
79 int top = rows > height ? (rows - height) / 2 : 0;
80 int used = 0;
81 for (; used < top; ++used)
82 frame_buffer_printf(buf, "\n");
83 if (show_logo) {
84 invitation_render_logo(buf, cols, frame, colors, compact);
85 frame_buffer_printf(buf, "\n");
86 used += logo_rows + 1;
87 }
88 // On exceptionally short terminals prioritize the string and its command.
89 int first = height > rows && ready ? 1 : 0;
90 for (int i = first; i < 3 && used < rows; ++i) {
91 const char *text = lines[i];
92 size_t remaining = strlen(text);
93 while (remaining && used < rows) {
94 int length = remaining > (size_t)cols ? cols : (int)remaining;
95 centered_line(buf, text, length, cols, frame, false);
96 text += length;
97 remaining -= length;
98 ++used;
99 }
100 }
101 // Erase stale logs/rows after a layout change without clearing between frames.
102 frame_buffer_printf(buf, "\033[J");
103}
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:22
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
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.
void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact)
Definition invitation.c:34
void invitation_render(frame_buffer_t *buf, terminal_size_t size, const char *session, bool joining, int frame, bool colors)
Definition invitation.c:58
#define TUI_INVITATION_LINE_BUFFER_SIZE
Definition invitation.c:3
🔤 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).
asciichat_error_t animation_apply(const animation_sample_t *s, animation_target_t *t)
@ ANIMATION_SPLASH_RAINBOW
@ ANIMATION_TARGET_COLOR