ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
strings.h File Reference

Centralized enum and mode string conversion API. More...

Go to the source code of this file.

Functions

const char * asciichat_mode_to_string (asciichat_mode_t mode)
 Convert mode enum to string.
 
asciichat_mode_t asciichat_string_to_mode (const char *str)
 Convert string to mode enum.
 
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 * asciichat_suggest_mode (const char *input)
 Suggest closest matching mode using fuzzy matching.
 
const char * color_mode_to_string (terminal_color_mode_t mode)
 Convert color mode enum to string.
 
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.
 

Detailed Description

Centralized enum and mode string conversion API.

Single source of truth for all enum/mode ↔ string conversions. Use these functions instead of hardcoded string literals.

Definition in file options/strings.h.

Function Documentation

◆ asciichat_get_all_mode_strings()

size_t asciichat_get_all_mode_strings ( const char ***  out_names)

Get all mode strings.

Parameters
out_namesOutput array of mode name pointers
Returns
Number of mode names

Definition at line 85 of file options/strings.c.

85 {
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}

References mode_string_mapping_t::name.

Referenced by asciichat_suggest_mode().

◆ asciichat_is_valid_mode_string()

bool asciichat_is_valid_mode_string ( const char *  str)

Check if string is a valid mode name.

Parameters
strString to check
Returns
true if valid mode name, false otherwise

Definition at line 81 of file options/strings.c.

81 {
83}
@ MODE_INVALID
Invalid mode.
asciichat_mode_t asciichat_string_to_mode(const char *str)
Convert string to mode enum.

References asciichat_string_to_mode(), and MODE_INVALID.

◆ asciichat_mode_to_string()

const char * asciichat_mode_to_string ( asciichat_mode_t  mode)

Convert mode enum to string.

Parameters
modeMode enum value
Returns
Mode name string (e.g., "server", "client", "mirror")

Definition at line 53 of file options/strings.c.

53 {
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}
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.

References MODE_DISCOVERY, and mode_string_mapping_t::name.

◆ asciichat_string_to_mode()

asciichat_mode_t asciichat_string_to_mode ( const char *  str)

Convert string to mode enum.

Parameters
strMode name string
Returns
Mode enum value, or MODE_UNKNOWN if invalid

Definition at line 67 of file options/strings.c.

67 {
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}
asciichat_mode_t mode

References mode_string_mapping_t::mode, MODE_INVALID, and mode_string_mapping_t::name.

Referenced by asciichat_is_valid_mode_string().

◆ asciichat_suggest_enum_value()

const char * asciichat_suggest_enum_value ( const char *  option_name,
const char *  input 
)

Suggest closest matching enum value using fuzzy matching.

Parameters
option_nameOption name (e.g., "color-mode", "palette")
inputInvalid enum value string
Returns
Suggested enum value, or NULL if no close match

Definition at line 192 of file options/strings.c.

192 {
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}
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
const char * levenshtein_find_similar(const char *unknown, const char *const *candidates)
Find the most similar string from a NULL-terminated array.

References levenshtein_find_similar(), and options_get_enum_values().

Referenced by parse_color_mode(), parse_palette_type(), and parse_render_mode().

◆ asciichat_suggest_mode()

const char * asciichat_suggest_mode ( const char *  input)

Suggest closest matching mode using fuzzy matching.

Parameters
inputInvalid mode string
Returns
Suggested mode name, or NULL if no close match

Definition at line 109 of file options/strings.c.

109 {
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}
size_t asciichat_get_all_mode_strings(const char ***out_names)
Get all mode strings.

References asciichat_get_all_mode_strings(), and levenshtein_find_similar().

◆ color_mode_to_string()

const char * color_mode_to_string ( terminal_color_mode_t  mode)

Convert color mode enum to string.

Parameters
modeColor mode value
Returns
Color mode string (e.g., "auto", "256", "truecolor")

Definition at line 131 of file options/strings.c.

131 {
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}
@ 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

References TERM_COLOR_16, TERM_COLOR_256, TERM_COLOR_AUTO, TERM_COLOR_NONE, and TERM_COLOR_TRUECOLOR.

Referenced by keyboard_help_render().

◆ palette_type_to_string()

const char * palette_type_to_string ( palette_type_t  type)

Convert palette type enum to string.

Parameters
typePalette type value
Returns
Palette type string (e.g., "standard", "blocks", "cool")

Definition at line 169 of file options/strings.c.

169 {
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}
@ 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

References PALETTE_BLOCKS, PALETTE_COOL, PALETTE_CUSTOM, PALETTE_DIGITAL, PALETTE_MINIMAL, and PALETTE_STANDARD.