ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
completions.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE 32
9
11#include <string.h>
12#include <ctype.h>
15#include <ascii-chat/common.h>
16#include <ascii-chat/log/log.h>
17
18/* Forward declarations for format-specific generators */
23
25 if (!output) {
26 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
27 }
28
29 switch (format) {
31 return completions_generate_bash(output);
33 return completions_generate_fish(output);
35 return completions_generate_zsh(output);
38 default:
39 return SET_ERRNO(ERROR_INVALID_PARAM, "Unknown completion format: %d", format);
40 }
41}
42
44 switch (format) {
53 default:
54 return "unknown";
55 }
56}
57
59 if (!shell_name) {
61 }
62
63 /* Convert to lowercase for case-insensitive matching */
65 size_t len = strlen(shell_name);
66 if (len >= sizeof(lower)) {
68 }
69
70 for (size_t i = 0; i < len; i++) {
71 lower[i] = tolower((unsigned char)shell_name[i]);
72 }
73
74 if (strcmp(lower, OPT_COMPLETION_SHELL_BASH) == 0) {
76 } else if (strcmp(lower, OPT_COMPLETION_SHELL_FISH) == 0) {
78 } else if (strcmp(lower, OPT_COMPLETION_SHELL_ZSH) == 0) {
80 } else if (strcmp(lower, OPT_COMPLETION_SHELL_POWERSHELL) == 0 || strcmp(lower, "ps") == 0) {
82 }
83
85}
86
101 if (!count) {
102 return NULL;
103 }
104
105 option_descriptor_t *combined_opts = NULL;
106 size_t combined_count = 0;
107
108 /* All completion modes to iterate through */
110 const size_t modes_len = sizeof(modes) / sizeof(modes[0]);
111
112 for (size_t m = 0; m < modes_len; m++) {
113 size_t mode_count = 0;
114 const option_descriptor_t *mode_opts = options_registry_get_for_mode(modes[m], &mode_count);
115 if (mode_opts) {
116 /* Collect unique options by long_name */
117 for (size_t i = 0; i < mode_count; i++) {
118 bool already_has = false;
119 for (size_t j = 0; j < combined_count; j++) {
120 if (strcmp(combined_opts[j].long_name, mode_opts[i].long_name) == 0) {
121 already_has = true;
122 break;
123 }
124 }
125 if (!already_has) {
126 combined_count++;
127 option_descriptor_t *temp = (option_descriptor_t *)SAFE_REALLOC(combined_opts, combined_count * sizeof(option_descriptor_t), option_descriptor_t *);
128 if (temp) {
129 combined_opts = temp;
130 combined_opts[combined_count - 1] = mode_opts[i];
131 }
132 }
133 }
134 SAFE_FREE(mode_opts);
135 }
136 }
137
138 *count = combined_count;
139 return combined_opts;
140}
#define SET_ERRNO(code,...)
completion_format_t completions_parse_shell_name(const char *shell_name)
Parse shell name string to completion format.
Definition completions.c:58
asciichat_error_t completions_generate_bash(FILE *output)
Definition bash.c:376
asciichat_error_t completions_generate_powershell(FILE *output)
Definition powershell.c:130
asciichat_error_t completions_generate_for_shell(completion_format_t format, FILE *output)
Generate shell completions and write to output stream.
Definition completions.c:24
#define OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE
Definition completions.c:8
asciichat_error_t completions_generate_fish(FILE *output)
Definition fish.c:122
asciichat_error_t completions_generate_zsh(FILE *output)
Definition zsh.c:141
const char * completions_get_shell_name(completion_format_t format)
Get human-readable shell name for format.
Definition completions.c:43
option_descriptor_t * completions_collect_all_modes_unique(size_t *count)
Collect options from all modes with deduplication.
Auto-generated shell completions from options registry.
completion_format_t
Supported shell completion formats.
Definition completions.h:66
@ COMPLETION_FORMAT_BASH
Definition completions.h:67
@ COMPLETION_FORMAT_UNKNOWN
Definition completions.h:71
@ COMPLETION_FORMAT_ZSH
Definition completions.h:69
@ COMPLETION_FORMAT_POWERSHELL
Definition completions.h:70
@ COMPLETION_FORMAT_FISH
Definition completions.h:68
Option enum value definitions - single source of truth.
#define OPT_COMPLETION_SHELL_BASH
Definition enums.h:80
#define OPT_COMPLETION_SHELL_FISH
Definition enums.h:81
#define OPT_COMPLETION_SHELL_POWERSHELL
Definition enums.h:83
#define OPT_COMPLETION_SHELL_ZSH
Definition enums.h:82
#define SAFE_REALLOC(ptr, size, cast)
#define SAFE_FREE(ptr)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
asciichat_mode_t
Mode type for options parsing.
Definition options.h:916
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:918
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:922
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:920
@ MODE_SERVER
Server mode - network server options.
Definition options.h:917
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:919
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Central registry of all command-line options with mode applicability.
📝 Logging API with multiple log levels and terminal output control
const option_descriptor_t * options_registry_get_for_mode(asciichat_mode_t mode, size_t *num_options)
Get all options for a specific mode.
Definition public_api.c:181
🔤 String Manipulation and Shell Escaping Utilities
The complete registry entry for one command-line/configuration option.
Definition builder.h:251