ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
enums.c
Go to the documentation of this file.
1
7#include <string.h>
10#include <ascii-chat/common.h>
11
12/* ═══════════════════════════════════════════════════════════════════════════
13 * Enum-to-String Mapping
14 *
15 * Each enum type has a mapping array that links enum values to their string
16 * representations with descriptions. This eliminates duplication and ensures consistency.
17 * ═════════════════════════════════════════════════════════════════════════ */
18
22static const enum_to_string_entry_t log_level_map[] = {
23 /* These enum values come from log/log.h */
24 {0, OPT_LOG_LEVEL_DEV, "development level"},
25 {1, OPT_LOG_LEVEL_DEBUG, "debug messages"},
26 {2, OPT_LOG_LEVEL_INFO, "info messages"},
27 {3, OPT_LOG_LEVEL_WARN, "warnings"},
28 {4, OPT_LOG_LEVEL_ERROR, "errors"},
29 {5, OPT_LOG_LEVEL_FATAL, "fatal errors"},
30 {-1, NULL, NULL} /* Terminator */
31};
32
36static const enum_to_string_entry_t color_mode_map[] = {
37 {0, OPT_COLOR_MODE_AUTO, "auto-detect based on terminal"},
38 {1, OPT_COLOR_MODE_NONE, "disable colors"},
39 {2, OPT_COLOR_MODE_16, "16 color mode"},
40 {3, OPT_COLOR_MODE_256, "256 color mode"},
41 {4, OPT_COLOR_MODE_TRUECOLOR, "true color (24-bit RGB)"},
42 {-1, NULL, NULL} /* Terminator */
43};
44
48static const enum_to_string_entry_t color_filter_map[] = {
62 {-1, NULL, NULL} /* Terminator */
63};
64
68static const enum_to_string_entry_t palette_map[] = {
69 {0, OPT_PALETTE_STANDARD, "standard palette"},
70 {1, OPT_PALETTE_BLOCKS, "block characters"},
71 {2, OPT_PALETTE_DIGITAL, "digital display"},
72 {3, OPT_PALETTE_MINIMAL, "minimal palette"},
73 {4, OPT_PALETTE_COOL, "cool colors"},
74 {5, OPT_PALETTE_CUSTOM, "custom palette"},
75 {-1, NULL, NULL} /* Terminator */
76};
77
81static const enum_to_string_entry_t render_mode_map[] = {
83 {RENDER_MODE_FOREGROUND, OPT_RENDER_MODE_FG, "text foreground (short)"},
85 {RENDER_MODE_BACKGROUND, OPT_RENDER_MODE_BG, "text background (short)"},
86 {RENDER_MODE_HALF_BLOCK, OPT_RENDER_MODE_HALF_BLOCK, "half-block characters"},
87 {-1, NULL, NULL} /* Terminator */
88};
89
93static const enum_to_string_entry_t reconnect_map[] = {
94 {0, OPT_RECONNECT_OFF, "disable auto-reconnect"},
95 {1, OPT_RECONNECT_AUTO, "enable auto-reconnect"},
96 {-1, NULL, NULL} /* Terminator */
97};
98
99/* ═══════════════════════════════════════════════════════════════════════════
100 * Enum Registry - Maps option names to their value mappings
101 * ═════════════════════════════════════════════════════════════════════════ */
102
106typedef struct {
107 const char *option_name;
110
114static const enum_map_descriptor_t enum_registry[] = {
115 {.option_name = "log-level", .map = log_level_map},
116 {.option_name = "color-mode", .map = color_mode_map},
117 {.option_name = "color-filter", .map = color_filter_map},
118 {.option_name = "palette", .map = palette_map},
119 {.option_name = "render-mode", .map = render_mode_map},
120 {.option_name = "reconnect", .map = reconnect_map},
121 {.option_name = NULL, .map = NULL} /* Terminator */
122};
123
134const enum_to_string_entry_t *options_get_enum_entries(const char *option_name, size_t *entry_count) {
135 if (!option_name || !entry_count) {
136 return NULL;
137 }
138
139 for (size_t i = 0; enum_registry[i].option_name != NULL; i++) {
140 if (strcmp(enum_registry[i].option_name, option_name) == 0) {
141 /* Count entries up to terminator */
142 size_t count = 0;
143 for (size_t j = 0; enum_registry[i].map[j].enum_value != -1; j++) {
144 count++;
145 }
146 *entry_count = count;
147 return enum_registry[i].map;
148 }
149 }
150
151 *entry_count = 0;
152 return NULL;
153}
154
166const char **options_get_enum_values(const char *option_name, size_t *value_count) {
167 if (!option_name || !value_count) {
168 return NULL;
169 }
170
171 /* Find the option's mapping in the registry */
172 const enum_map_descriptor_t *descriptor = NULL;
173 for (size_t i = 0; enum_registry[i].option_name != NULL; i++) {
174 if (strcmp(enum_registry[i].option_name, option_name) == 0) {
175 descriptor = &enum_registry[i];
176 break;
177 }
178 }
179
180 if (!descriptor || !descriptor->map) {
181 *value_count = 0;
182 return NULL;
183 }
184
185 /* Count non-terminator entries */
186 size_t count = 0;
187 for (size_t i = 0; descriptor->map[i].enum_value != -1; i++) {
188 count++;
189 }
190
191 /* Build array of unique strings (skip duplicates from aliases) */
192 const char **values = SAFE_MALLOC(count * sizeof(const char *), const char **);
193 size_t unique_count = 0;
194
195 for (size_t i = 0; descriptor->map[i].enum_value != -1; i++) {
196 const char *str = descriptor->map[i].string;
197
198 /* Check if we already have this string (for aliases) */
199 bool already_added = false;
200 for (size_t j = 0; j < unique_count; j++) {
201 if (strcmp(values[j], str) == 0) {
202 already_added = true;
203 break;
204 }
205 }
206
207 if (!already_added) {
208 values[unique_count++] = str;
209 }
210 }
211
212 *value_count = unique_count;
213 return values;
214}
215
222bool options_is_enum_option(const char *option_name) {
223 if (!option_name) {
224 return false;
225 }
226
227 for (size_t i = 0; enum_registry[i].option_name != NULL; i++) {
228 if (strcmp(enum_registry[i].option_name, option_name) == 0) {
229 return true;
230 }
231 }
232
233 return false;
234}
⚙️ 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
bool options_is_enum_option(const char *option_name)
Check if an option has enum values.
Definition enums.c:222
const enum_to_string_entry_t * options_get_enum_entries(const char *option_name, size_t *entry_count)
Get all enum entries with descriptions for an option.
Definition enums.c:134
Option enum value definitions - single source of truth.
#define OPT_COLOR_FILTER_YELLOW
Definition enums.h:101
#define OPT_PALETTE_CUSTOM
Definition enums.h:110
#define OPT_LOG_LEVEL_DEV
Definition enums.h:75
#define OPT_PALETTE_COOL
Definition enums.h:109
#define OPT_RECONNECT_AUTO
Definition enums.h:121
#define OPT_RENDER_MODE_BG
Definition enums.h:116
#define OPT_RENDER_MODE_FOREGROUND
Definition enums.h:113
#define OPT_COLOR_FILTER_RAINBOW
Definition enums.h:102
#define OPT_LOG_LEVEL_FATAL
Definition enums.h:80
#define OPT_COLOR_FILTER_GREEN
Definition enums.h:93
#define OPT_LOG_LEVEL_ERROR
Definition enums.h:79
#define OPT_RENDER_MODE_HALF_BLOCK
Definition enums.h:117
#define OPT_COLOR_MODE_256
Definition enums.h:86
#define OPT_COLOR_FILTER_TEAL
Definition enums.h:97
#define OPT_RENDER_MODE_FG
Definition enums.h:114
#define OPT_LOG_LEVEL_INFO
Definition enums.h:77
#define OPT_COLOR_FILTER_CYAN
Definition enums.h:98
#define OPT_LOG_LEVEL_DEBUG
Definition enums.h:76
#define OPT_COLOR_MODE_NONE
Definition enums.h:84
#define OPT_PALETTE_MINIMAL
Definition enums.h:108
#define OPT_COLOR_FILTER_FUCHSIA
Definition enums.h:95
#define OPT_PALETTE_BLOCKS
Definition enums.h:106
#define OPT_COLOR_FILTER_NONE
Definition enums.h:90
#define OPT_PALETTE_STANDARD
Definition enums.h:105
#define OPT_RECONNECT_OFF
Definition enums.h:120
#define OPT_COLOR_FILTER_RED
Definition enums.h:100
#define OPT_COLOR_MODE_TRUECOLOR
Definition enums.h:87
#define OPT_COLOR_FILTER_BLACK
Definition enums.h:91
#define OPT_PALETTE_DIGITAL
Definition enums.h:107
#define OPT_COLOR_FILTER_PINK
Definition enums.h:99
#define OPT_LOG_LEVEL_WARN
Definition enums.h:78
#define OPT_RENDER_MODE_BACKGROUND
Definition enums.h:115
#define OPT_COLOR_MODE_AUTO
Definition enums.h:83
#define OPT_COLOR_MODE_16
Definition enums.h:85
#define OPT_COLOR_FILTER_WHITE
Definition enums.h:92
#define OPT_COLOR_FILTER_MAGENTA
Definition enums.h:94
#define OPT_COLOR_FILTER_ORANGE
Definition enums.h:96
#define SAFE_MALLOC(size, cast)
Definition common.h:264
Descriptor for an enum option with its string value mapping.
Definition enums.c:106
const char * option_name
Option long name (e.g., "log-level")
Definition enums.c:107
const enum_to_string_entry_t * map
Mapping array from enum values to strings.
Definition enums.c:108
Enum value mapping with description for shell completions.
Definition enums.h:27
int enum_value
The numeric enum value.
Definition enums.h:28
const char * string
The string representation of that enum value.
Definition enums.h:29
🖥️ Cross-platform terminal interface for ascii-chat
@ COLOR_FILTER_WHITE
White content on black background.
Definition terminal.h:605
@ COLOR_FILTER_FUCHSIA
Fuchsia (#FF00AA)
Definition terminal.h:611
@ COLOR_FILTER_NONE
No filtering (default)
Definition terminal.h:601
@ COLOR_FILTER_TEAL
Teal (#00DDDD)
Definition terminal.h:615
@ COLOR_FILTER_CYAN
Cyan (#00FFFF)
Definition terminal.h:617
@ COLOR_FILTER_GREEN
Green (#00FF41)
Definition terminal.h:607
@ COLOR_FILTER_MAGENTA
Magenta (#FF00FF)
Definition terminal.h:609
@ COLOR_FILTER_RAINBOW
Rainbow (cycles through spectrum over 3.5s)
Definition terminal.h:625
@ COLOR_FILTER_RED
Red (#FF3333)
Definition terminal.h:621
@ COLOR_FILTER_YELLOW
Yellow (#FFEB99)
Definition terminal.h:623
@ COLOR_FILTER_PINK
Pink (#FFB6C1)
Definition terminal.h:619
@ COLOR_FILTER_BLACK
Dark content on white background.
Definition terminal.h:603
@ COLOR_FILTER_ORANGE
Orange (#FF8800)
Definition terminal.h:613
@ 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