ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
enums.c File Reference

Enum views backed by the option registry metadata. More...

Go to the source code of this file.

Functions

const enum_to_string_entry_t * options_get_enum_entries (const char *option_name, size_t *entry_count)
 Get enum entries with descriptions for an option.
const char ** options_get_enum_values (const char *option_name, size_t *value_count)
 Get enum values for an option.
bool options_is_enum_option (const char *option_name)
 Check if an option has enum values.

Detailed Description

Enum views backed by the option registry metadata.

Definition in file enums.c.

Function Documentation

◆ options_get_enum_entries()

const enum_to_string_entry_t * options_get_enum_entries ( const char * option_name,
size_t * entry_count )

Get enum entries with descriptions for an option.

Returns entries containing both string values and descriptions for shell completion.

Parameters
option_nameOption long name (e.g., "log-level")
entry_countOUTPUT: Number of entries returned
Returns
Allocated array (caller uses SAFE_FREE), or NULL. Strings are borrowed.

Definition at line 9 of file enums.c.

9 {
10 if (!entry_count)
11 return NULL;
12 *entry_count = 0;
13 if (!option_name)
14 return NULL;
15 const option_metadata_t *meta = options_registry_get_metadata(option_name);
16 if (!meta || !meta->enum_values)
17 return NULL;
18 while (meta->enum_values[*entry_count])
19 ++*entry_count;
20 enum_to_string_entry_t *entries = SAFE_CALLOC(*entry_count + 1, sizeof(*entries), enum_to_string_entry_t *);
21 for (size_t i = 0; i < *entry_count; ++i) {
22 entries[i].enum_value = meta->enum_integer_values ? meta->enum_integer_values[i] : (int)i;
23 entries[i].string = meta->enum_values[i];
24 entries[i].desc = meta->enum_descriptions ? meta->enum_descriptions[i] : NULL;
25 }
26 entries[*entry_count].enum_value = -1;
27 return entries;
28}
#define SAFE_CALLOC(count, size, cast)
const option_metadata_t * options_registry_get_metadata(const char *long_name)
Get complete metadata for an option.
Definition public_api.c:251
Enum value mapping with description for shell completions.
Definition enums.h:29
int enum_value
The numeric enum value.
Definition enums.h:30
const char * desc
Description for shell completion.
Definition enums.h:32
const char * string
The string representation of that enum value.
Definition enums.h:31
Completion metadata attached to one option descriptor.
Definition builder.h:201
const char ** enum_values
Enum value strings (null-terminated, e.g., {"auto", "none", "16", "256", "truecolor",...
Definition builder.h:203
const char ** enum_descriptions
Descriptions parallel to enum_values (null-terminated, e.g., "Auto-detect from terminal").
Definition builder.h:204
const int * enum_integer_values
Actual enum integer values (e.g., {-1, 0, 1, 2, 3} for non-sequential enums).
Definition builder.h:205

References enum_to_string_entry_t::desc, option_metadata_t::enum_descriptions, option_metadata_t::enum_integer_values, enum_to_string_entry_t::enum_value, option_metadata_t::enum_values, options_registry_get_metadata(), SAFE_CALLOC, and enum_to_string_entry_t::string.

◆ options_get_enum_values()

const char ** options_get_enum_values ( const char * option_name,
size_t * value_count )

Get enum values for an option.

Parameters
option_nameOption long name (e.g., "log-level")
value_countOUTPUT: Number of values returned
Returns
Allocated NULL-terminated array (caller uses SAFE_FREE), or NULL. Strings are borrowed.

Definition at line 30 of file enums.c.

30 {
31 if (!value_count)
32 return NULL;
33 *value_count = 0;
34 if (!option_name)
35 return NULL;
36 const char **values = options_registry_get_enum_values(option_name, NULL, value_count);
37 if (!values)
38 return NULL;
39 const char **copy = SAFE_MALLOC((*value_count + 1) * sizeof(*copy), const char **);
40 memcpy(copy, values, (*value_count + 1) * sizeof(*copy));
41 return copy;
42}
#define SAFE_MALLOC(size, cast)
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:272

References options_registry_get_enum_values(), and SAFE_MALLOC.

◆ options_is_enum_option()

bool options_is_enum_option ( const char * option_name)

Check if an option has enum values.

Parameters
option_nameOption long name
Returns
true if option has enum values, false otherwise

Definition at line 44 of file enums.c.

44 {
45 if (!option_name)
46 return false;
47 size_t count = 0;
48 return options_registry_get_enum_values(option_name, NULL, &count) != NULL;
49}

References options_registry_get_enum_values().