ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
options/strings.c
Go to the documentation of this file.
1
14#include <ascii-chat/options/options.h> // For asciichat_mode_t and MODE_ enums
18#include <ascii-chat/platform/terminal.h> // For term_color_level_t
19#include <ascii-chat/video/ascii/palette.h> // For palette_type_t
20#include <ascii-chat/common.h>
21#include <string.h>
22#include <stdio.h>
23
24// ============================================================================
25// Mode String Conversions
26// ============================================================================
27
31typedef struct {
33 const char *name;
34 const char *description;
36
44static const mode_string_mapping_t mode_registry[] = {
45 {MODE_SERVER, "server", "Multi-client video chat server"},
46 {MODE_CLIENT, "client", "Connect to ascii-chat server"},
47 {MODE_MIRROR, "mirror", "Local webcam preview (no networking)"},
48 {MODE_DISCOVERY_SERVICE, "discovery-service", "Session discovery server (ACDS)"},
49 {MODE_DISCOVERY_SERVICE, "acds", "Alias for discovery-service"},
50 {MODE_INVALID, NULL, NULL} // Terminator
51};
52
54 // Special case: MODE_DISCOVERY is the default mode, show as "default" to users
55 if (mode == MODE_DISCOVERY) {
56 return "default";
57 }
58
59 for (size_t i = 0; mode_registry[i].name != NULL; i++) {
60 if (mode_registry[i].mode == mode) {
61 return mode_registry[i].name;
62 }
63 }
64 return "unknown";
65}
66
68 if (!str) {
69 return MODE_INVALID;
70 }
71
72 for (size_t i = 0; mode_registry[i].name != NULL; i++) {
73 if (strcmp(mode_registry[i].name, str) == 0) {
74 return mode_registry[i].mode;
75 }
76 }
77
78 return MODE_INVALID;
79}
80
81bool asciichat_is_valid_mode_string(const char *str) {
83}
84
85size_t asciichat_get_all_mode_strings(const char ***out_names) {
86 static const char *names[32]; // Max 32 modes
87 size_t count = 0;
88
89 for (size_t i = 0; mode_registry[i].name != NULL && count < 32; i++) {
90 // Skip duplicates (like "acds" alias)
91 bool duplicate = false;
92 for (size_t j = 0; j < count; j++) {
93 if (strcmp(names[j], mode_registry[i].name) == 0) {
94 duplicate = true;
95 break;
96 }
97 }
98 if (!duplicate) {
99 names[count++] = mode_registry[i].name;
100 }
101 }
102
103 if (out_names) {
104 *out_names = names;
105 }
106 return count;
107}
108
109const char *asciichat_suggest_mode(const char *input) {
110 if (!input) {
111 return NULL;
112 }
113
114 const char **names = NULL;
115 size_t count = asciichat_get_all_mode_strings(&names);
116
117 // Create NULL-terminated array for levenshtein_find_similar
118 static const char *null_terminated[33]; // Max 32 modes + NULL
119 for (size_t i = 0; i < count && i < 32; i++) {
120 null_terminated[i] = names[i];
121 }
122 null_terminated[count] = NULL;
123
124 return levenshtein_find_similar(input, null_terminated);
125}
126
127// ============================================================================
128// Color Mode String Conversions
129// ============================================================================
130
132 switch (mode) {
133 case TERM_COLOR_AUTO:
134 return "auto";
135 case TERM_COLOR_NONE:
136 return "none";
137 case TERM_COLOR_16:
138 return "16";
139 case TERM_COLOR_256:
140 return "256";
142 return "truecolor";
143 default:
144 return "unknown";
145 }
146}
147
148// ============================================================================
149// Render Mode String Conversions
150// ============================================================================
151
153 switch (mode) {
155 return "foreground";
157 return "background";
159 return "half-block";
160 default:
161 return "unknown";
162 }
163}
164
165// ============================================================================
166// Palette Type String Conversions
167// ============================================================================
168
170 switch (type) {
171 case PALETTE_STANDARD:
172 return "standard";
173 case PALETTE_BLOCKS:
174 return "blocks";
175 case PALETTE_DIGITAL:
176 return "digital";
177 case PALETTE_MINIMAL:
178 return "minimal";
179 case PALETTE_COOL:
180 return "cool";
181 case PALETTE_CUSTOM:
182 return "custom";
183 default:
184 return "unknown";
185 }
186}
187
188// ============================================================================
189// Generic Enum Value Fuzzy Matching
190// ============================================================================
191
192const char *asciichat_suggest_enum_value(const char *option_name, const char *input) {
193 if (!option_name || !input) {
194 return NULL;
195 }
196
197 size_t value_count = 0;
198 const char **values = options_get_enum_values(option_name, &value_count);
199
200 if (!values || value_count == 0) {
201 return NULL;
202 }
203
204 // Create NULL-terminated array for levenshtein_find_similar
205 static const char *null_terminated[64]; // Max 64 enum values
206 for (size_t i = 0; i < value_count && i < 63; i++) {
207 null_terminated[i] = values[i];
208 }
209 null_terminated[value_count] = NULL;
210
211 return levenshtein_find_similar(input, null_terminated);
212}
⚙️ Common definitions, error codes, macros, and types shared throughout the application
const char ** options_get_enum_values(const char *option_name, size_t *value_count)
Get all unique string values for an option.
Definition enums.c:166
Option enum value definitions - single source of truth.
asciichat_mode_t
Mode type for options parsing.
const char * levenshtein_find_similar(const char *unknown, const char *const *candidates)
Find the most similar string from a NULL-terminated array.
@ MODE_CLIENT
Client mode - network client options.
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
@ MODE_INVALID
Invalid mode.
@ MODE_SERVER
Server mode - network server options.
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network)
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
palette_type_t
Built-in palette type enumeration.
Definition palette.h:84
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:90
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:98
@ PALETTE_COOL
Ascending blocks: " ▁▂▃▄▅▆▇█".
Definition palette.h:96
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:88
@ PALETTE_DIGITAL
Digital/glitch aesthetic: " -=≡≣▰▱◼".
Definition palette.h:92
@ PALETTE_MINIMAL
Simple ASCII: " .-+*#".
Definition palette.h:94
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Levenshtein distance algorithm for fuzzy string matching.
const char * color_mode_to_string(terminal_color_mode_t mode)
Convert color mode enum to string.
asciichat_mode_t asciichat_string_to_mode(const char *str)
Convert string to mode enum.
const char * render_mode_to_string(render_mode_t mode)
const char * asciichat_mode_to_string(asciichat_mode_t mode)
Convert mode enum to string.
const char * asciichat_suggest_mode(const char *input)
Suggest closest matching mode using fuzzy matching.
bool asciichat_is_valid_mode_string(const char *str)
Check if string is a valid mode name.
size_t asciichat_get_all_mode_strings(const char ***out_names)
Get all mode strings.
const char * palette_type_to_string(palette_type_t type)
Convert palette type enum to string.
const char * asciichat_suggest_enum_value(const char *option_name, const char *input)
Suggest closest matching enum value using fuzzy matching.
Centralized enum and mode string conversion API.
ASCII Palette Management for Video-to-ASCII Conversion.
Mode name mapping for string conversion.
asciichat_mode_t mode
🖥️ Cross-platform terminal interface for ascii-chat
render_mode_t
Render mode preferences.
Definition terminal.h:662
@ RENDER_MODE_FOREGROUND
Foreground colors only (text color)
Definition terminal.h:664
@ RENDER_MODE_BACKGROUND
Background colors (block colors)
Definition terminal.h:666
@ RENDER_MODE_HALF_BLOCK
Unicode half-block characters (mixed foreground/background)
Definition terminal.h:668
terminal_color_mode_t
Terminal color support levels.
Definition terminal.h:578
@ TERM_COLOR_NONE
No color support (monochrome terminal)
Definition terminal.h:582
@ TERM_COLOR_16
16-color support (standard ANSI colors)
Definition terminal.h:584
@ TERM_COLOR_256
256-color support (extended ANSI palette)
Definition terminal.h:586
@ TERM_COLOR_AUTO
Auto-detect color support from terminal capabilities.
Definition terminal.h:580
@ TERM_COLOR_TRUECOLOR
24-bit truecolor support (RGB colors)
Definition terminal.h:588