ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 93 of file options/strings.c.

93 {
94 static const char *names[MODE_NAME_CAPACITY]; // Max 32 modes
95 size_t count = 0;
96
97 for (size_t i = 0;
98 mode_registry[i].name != NULL && count < MODE_NAME_CAPACITY; i++) {
99 // The HTTP API entry point is selected by its dedicated launcher and is
100 // not a user-facing mode for generic mode completion/help lists.
101 if (mode_registry[i].mode == MODE_API_SERVER ||
102 strcmp(mode_registry[i].name, "acds") == 0)
103 continue;
104 // Skip duplicates (like "acds" alias)
105 bool duplicate = false;
106 for (size_t j = 0; j < count; j++) {
107 if (strcmp(names[j], mode_registry[i].name) == 0) {
108 duplicate = true;
109 break;
110 }
111 }
112 if (!duplicate) {
113 names[count++] = mode_registry[i].name;
114 }
115 }
116
117 if (out_names) {
118 *out_names = names;
119 }
120 return count;
121}
@ MODE_API_SERVER
Image-to-ASCII HTTP API server.
Definition options.h:923
#define MODE_NAME_CAPACITY

References MODE_API_SERVER, and MODE_NAME_CAPACITY.

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 89 of file options/strings.c.

89 {
91}
@ MODE_INVALID
Invalid mode.
Definition options.h:924
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 60 of file options/strings.c.

60 {
61 // Special case: MODE_DISCOVERY is the default mode, show as "default" to
62 // users
63 if (mode == MODE_DISCOVERY) {
64 return "default";
65 }
66
67 for (size_t i = 0; mode_registry[i].name != NULL; i++) {
68 if (mode_registry[i].mode == mode) {
69 return mode_registry[i].name;
70 }
71 }
72 return "unknown";
73}
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921

References MODE_DISCOVERY.

◆ 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 75 of file options/strings.c.

75 {
76 if (!str) {
77 return MODE_INVALID;
78 }
79
80 for (size_t i = 0; mode_registry[i].name != NULL; i++) {
81 if (strcmp(mode_registry[i].name, str) == 0) {
82 return mode_registry[i].mode;
83 }
84 }
85
86 return MODE_INVALID;
87}

References MODE_INVALID.

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 207 of file options/strings.c.

208 {
209 if (!option_name || !input) {
210 return NULL;
211 }
212
213 size_t value_count = 0;
214 const char **values =
215 options_registry_get_enum_values(option_name, NULL, &value_count);
216 return values ? levenshtein_find_similar(input, values) : NULL;
217}
const char * levenshtein_find_similar(const char *unknown, const char *const *candidates)
Find the most similar string from a NULL-terminated array.
const char ** options_registry_get_enum_values(const char *option_name, const char ***descriptions, size_t *count)
Get enum values for an option.
Definition public_api.c:276

References levenshtein_find_similar(), and options_registry_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 123 of file options/strings.c.

123 {
124 if (!input) {
125 return NULL;
126 }
127
128 const char **names = NULL;
129 size_t count = asciichat_get_all_mode_strings(&names);
130
131 // Create a NULL-terminated array for libflags suggestion matching.
132 static const char
133 *null_terminated[MODE_NAME_CAPACITY + 1]; // Max 32 modes + NULL
134 for (size_t i = 0; i < count && i < MODE_NAME_CAPACITY; i++) {
135 null_terminated[i] = names[i];
136 }
137 null_terminated[count] = NULL;
138
139 return levenshtein_find_similar(input, null_terminated);
140}
size_t asciichat_get_all_mode_strings(const char ***out_names)
Get all mode strings.

References asciichat_get_all_mode_strings(), levenshtein_find_similar(), and MODE_NAME_CAPACITY.

◆ 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 146 of file options/strings.c.

146 {
147 switch (mode) {
148 case TERM_COLOR_AUTO:
149 return OPT_COLOR_MODE_AUTO;
150 case TERM_COLOR_NONE:
151 return OPT_COLOR_MODE_NONE;
152 case TERM_COLOR_16:
153 return OPT_COLOR_MODE_16;
154 case TERM_COLOR_256:
155 return OPT_COLOR_MODE_256;
156 case TERM_COLOR_TRUECOLOR:
158 default:
159 return "unknown";
160 }
161}
#define OPT_COLOR_MODE_256
Definition enums.h:106
#define OPT_COLOR_MODE_NONE
Definition enums.h:103
#define OPT_COLOR_MODE_TRUECOLOR
Definition enums.h:107
#define OPT_COLOR_MODE_AUTO
Definition enums.h:102
#define OPT_COLOR_MODE_16
Definition enums.h:105

References OPT_COLOR_MODE_16, OPT_COLOR_MODE_256, OPT_COLOR_MODE_AUTO, OPT_COLOR_MODE_NONE, and OPT_COLOR_MODE_TRUECOLOR.

Referenced by ui_session_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 184 of file options/strings.c.

184 {
185 switch (type) {
186 case PALETTE_STANDARD:
188 case PALETTE_BLOCKS:
189 return OPT_PALETTE_BLOCKS;
190 case PALETTE_DIGITAL:
191 return OPT_PALETTE_DIGITAL;
192 case PALETTE_MINIMAL:
193 return OPT_PALETTE_MINIMAL;
194 case PALETTE_COOL:
195 return OPT_PALETTE_COOL;
196 case PALETTE_CUSTOM:
197 return OPT_PALETTE_CUSTOM;
198 default:
199 return "unknown";
200 }
201}
#define OPT_PALETTE_CUSTOM
Definition enums.h:130
#define OPT_PALETTE_COOL
Definition enums.h:129
#define OPT_PALETTE_MINIMAL
Definition enums.h:128
#define OPT_PALETTE_BLOCKS
Definition enums.h:126
#define OPT_PALETTE_STANDARD
Definition enums.h:125
#define OPT_PALETTE_DIGITAL
Definition enums.h:127
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:102
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
@ PALETTE_COOL
Ascending blocks: " ▁▂▃▄▅▆▇█".
Definition palette.h:108
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:100
@ PALETTE_DIGITAL
Digital/glitch aesthetic: " -=≡≣▰▱◼".
Definition palette.h:104
@ PALETTE_MINIMAL
Simple ASCII: " .-+*#".
Definition palette.h:106

References OPT_PALETTE_BLOCKS, OPT_PALETTE_COOL, OPT_PALETTE_CUSTOM, OPT_PALETTE_DIGITAL, OPT_PALETTE_MINIMAL, OPT_PALETTE_STANDARD, PALETTE_BLOCKS, PALETTE_COOL, PALETTE_CUSTOM, PALETTE_DIGITAL, PALETTE_MINIMAL, and PALETTE_STANDARD.