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 File Reference

Implementation of shell completion generation from options registry. More...

Go to the source code of this file.

Macros

#define OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE   32

Functions

asciichat_error_t completions_generate_bash (FILE *output)
asciichat_error_t completions_generate_fish (FILE *output)
asciichat_error_t completions_generate_zsh (FILE *output)
asciichat_error_t completions_generate_powershell (FILE *output)
asciichat_error_t completions_generate_for_shell (completion_format_t format, FILE *output)
 Generate shell completions and write to output stream.
const char * completions_get_shell_name (completion_format_t format)
 Get human-readable shell name for format.
completion_format_t completions_parse_shell_name (const char *shell_name)
 Parse shell name string to completion format.
option_descriptor_t * completions_collect_all_modes_unique (size_t *count)
 Collect options from all modes with deduplication.

Detailed Description

Implementation of shell completion generation from options registry.

Definition in file completions.c.

Macro Definition Documentation

◆ OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE

#define OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE   32

Definition at line 8 of file completions.c.

Referenced by completions_parse_shell_name().

Function Documentation

◆ completions_collect_all_modes_unique()

option_descriptor_t * completions_collect_all_modes_unique ( size_t * count)

Collect options from all modes with deduplication.

Iterates through all completion modes (MODE_DISCOVERY, MODE_SERVER, MODE_CLIENT, MODE_MIRROR, MODE_DISCOVERY_SERVICE) and collects unique options by long_name. Useful for generators that need to show completions for options across multiple modes.

Parameters
[out]countPointer to receive the count of unique options
Returns
Dynamically allocated array of option_descriptor_t, must be freed by caller. Returns NULL if no options found.
Note
The caller must free the returned pointer with SAFE_FREE()

Definition at line 100 of file completions.c.

100 {
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 SAFE_REALLOC(ptr, size, cast)
#define SAFE_FREE(ptr)
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
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
The complete registry entry for one command-line/configuration option.
Definition builder.h:251

References MODE_CLIENT, MODE_DISCOVERY, MODE_DISCOVERY_SERVICE, MODE_MCP_SERVER, MODE_MIRROR, MODE_SERVER, options_registry_get_for_mode(), SAFE_FREE, and SAFE_REALLOC.

◆ completions_generate_bash()

asciichat_error_t completions_generate_bash ( FILE * output)

Definition at line 376 of file bash.c.

376 {
377 if (!output) {
378 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
379 }
380
381 bash_write_header(output);
382 asciichat_error_t err = bash_write_all_options(output);
383 if (err != ASCIICHAT_OK) {
384 return err;
385 }
386 bash_write_completion_logic(output);
387 bash_write_footer(output);
388
389 return ASCIICHAT_OK;
390}
#define SET_ERRNO(code,...)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM

Referenced by completions_generate_for_shell().

◆ completions_generate_fish()

asciichat_error_t completions_generate_fish ( FILE * output)

Definition at line 122 of file fish.c.

122 {
123 if (!output) {
124 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
125 }
126
127 fprintf(output, "# Fish completion script for ascii-chat\n"
128 "# Generated from options registry - DO NOT EDIT MANUALLY\n"
129 "\n"
130 "complete -c ascii-chat -f\n"
131 "\n"
132 "function __fish_ascii_chat_using_mode\n"
133 " set -l cmd (commandline -opc)\n"
134 " for arg in $cmd\n"
135 " if contains -- $arg server client mirror discovery-service mcp-server\n"
136 " echo $arg\n"
137 " return 0\n"
138 " end\n"
139 " end\n"
140 " return 1\n"
141 "end\n"
142 "\n"
143 "function __fish_ascii_chat_mode_is\n"
144 " test (string match -q $argv[1] (command __fish_ascii_chat_using_mode))\n"
145 "end\n"
146 "\n"
147 "function __fish_ascii_chat_no_mode\n"
148 " not __fish_ascii_chat_using_mode > /dev/null\n"
149 "end\n"
150 "\n"
151 "# Modes\n"
152 "complete -c ascii-chat -n __fish_ascii_chat_no_mode -a server -d 'Run a video chat server'\n"
153 "complete -c ascii-chat -n __fish_ascii_chat_no_mode -a client -d 'Connect to a video chat server'\n"
154 "complete -c ascii-chat -n __fish_ascii_chat_no_mode -a mirror -d 'View webcam locally without network'\n"
155 "\n");
156
157 /* Binary options - use unified display API matching help system */
158 size_t binary_count = 0;
159 const option_descriptor_t *binary_opts = options_registry_get_for_display(MODE_DISCOVERY, true, &binary_count);
160
161 fprintf(output, "# Binary-level options (same as 'ascii-chat --help')\n");
162 if (binary_opts) {
163 for (size_t i = 0; i < binary_count; i++) {
164 fish_write_option(output, &binary_opts[i], "");
165 }
166 SAFE_FREE(binary_opts);
167 }
168 fprintf(output, "\n");
169
170 /* Server options - use unified display API matching help system */
171 size_t server_count = 0;
172 const option_descriptor_t *server_opts = options_registry_get_for_display(MODE_SERVER, false, &server_count);
173
174 fprintf(output, "# Server options (same as 'ascii-chat server --help')\n");
175 if (server_opts) {
176 for (size_t i = 0; i < server_count; i++) {
177 fish_write_option(output, &server_opts[i], "-n '__fish_seen_subcommand_from server'");
178 }
179 SAFE_FREE(server_opts);
180 }
181 fprintf(output, "\n");
182
183 /* Client options - use unified display API matching help system */
184 size_t client_count = 0;
185 const option_descriptor_t *client_opts = options_registry_get_for_display(MODE_CLIENT, false, &client_count);
186
187 fprintf(output, "# Client options (same as 'ascii-chat client --help')\n");
188 if (client_opts) {
189 for (size_t i = 0; i < client_count; i++) {
190 fish_write_option(output, &client_opts[i], "-n '__fish_seen_subcommand_from client'");
191 }
192 SAFE_FREE(client_opts);
193 }
194 fprintf(output, "\n");
195
196 /* Mirror options - use unified display API matching help system */
197 size_t mirror_count = 0;
198 const option_descriptor_t *mirror_opts = options_registry_get_for_display(MODE_MIRROR, false, &mirror_count);
199
200 fprintf(output, "# Mirror options (same as 'ascii-chat mirror --help')\n");
201 if (mirror_opts) {
202 for (size_t i = 0; i < mirror_count; i++) {
203 fish_write_option(output, &mirror_opts[i], "-n '__fish_seen_subcommand_from mirror'");
204 }
205 SAFE_FREE(mirror_opts);
206 }
207 fprintf(output, "\n");
208
209 fprintf(output, "complete -c ascii-chat -n __fish_ascii_chat_no_mode -a mcp-server -d 'Render ASCII art through MCP'\n");
210 size_t mcp_count = 0;
211 const option_descriptor_t *mcp_opts = options_registry_get_for_display(MODE_MCP_SERVER, false, &mcp_count);
212 for (size_t i = 0; i < mcp_count; i++) {
213 fish_write_option(output, &mcp_opts[i], "-n '__fish_seen_subcommand_from mcp-server'");
214 }
215 SAFE_FREE(mcp_opts);
216
217 /* Discovery-service options */
218 size_t discovery_svc_count = 0;
219 const option_descriptor_t *discovery_svc_opts = options_registry_get_for_display(MODE_DISCOVERY_SERVICE, false, &discovery_svc_count);
220
221 fprintf(output, "# Discovery-service options (same as 'ascii-chat discovery-service --help')\n");
222 if (discovery_svc_opts) {
223 for (size_t i = 0; i < discovery_svc_count; i++) {
224 fish_write_option(output, &discovery_svc_opts[i], "-n '__fish_seen_subcommand_from discovery-service'");
225 }
226 SAFE_FREE(discovery_svc_opts);
227 }
228
229 return ASCIICHAT_OK;
230}
const option_descriptor_t * options_registry_get_for_display(asciichat_mode_t mode, bool for_binary_help, size_t *num_options)
Get options for help/completions display with unified filtering.
Definition public_api.c:204

References ASCIICHAT_OK, ERROR_INVALID_PARAM, MODE_CLIENT, MODE_DISCOVERY, MODE_DISCOVERY_SERVICE, MODE_MCP_SERVER, MODE_MIRROR, MODE_SERVER, options_registry_get_for_display(), SAFE_FREE, and SET_ERRNO.

Referenced by completions_generate_for_shell().

◆ completions_generate_for_shell()

asciichat_error_t completions_generate_for_shell ( completion_format_t format,
FILE * output )

Generate shell completions and write to output stream.

Generates a complete shell completion script for the specified shell format by extracting option metadata from the centralized registry.

Parameters
formatShell format to generate completions for
outputFILE stream to write completion script to (e.g., stdout)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 24 of file completions.c.

24 {
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}
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_fish(FILE *output)
Definition fish.c:122
asciichat_error_t completions_generate_zsh(FILE *output)
Definition zsh.c:141
@ COMPLETION_FORMAT_BASH
Definition completions.h:67
@ COMPLETION_FORMAT_ZSH
Definition completions.h:69
@ COMPLETION_FORMAT_POWERSHELL
Definition completions.h:70
@ COMPLETION_FORMAT_FISH
Definition completions.h:68

References COMPLETION_FORMAT_BASH, COMPLETION_FORMAT_FISH, COMPLETION_FORMAT_POWERSHELL, COMPLETION_FORMAT_ZSH, completions_generate_bash(), completions_generate_fish(), completions_generate_powershell(), completions_generate_zsh(), ERROR_INVALID_PARAM, and SET_ERRNO.

◆ completions_generate_powershell()

asciichat_error_t completions_generate_powershell ( FILE * output)

Definition at line 130 of file powershell.c.

130 {
131 if (!output) {
132 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
133 }
134
135 fprintf(output, "# PowerShell completion script for ascii-chat\n"
136 "# Generated from options registry - DO NOT EDIT MANUALLY\n"
137 "# Usage: ascii-chat --completions powershell | Out-String | Invoke-Expression\n"
138 "\n"
139 "$script:AsciiChatCompleter = {\n"
140 " param($wordToComplete, $commandAst, $cursorPosition)\n"
141 "\n"
142 " $words = @($commandAst.CommandElements | ForEach-Object { if ($null -ne $_.Value) { $_.Value } "
143 "else { $_.Extent.Text } })\n"
144 " $mode = $null\n"
145 "\n"
146 " foreach ($word in $words) {\n"
147 " if ($word -in @('server', 'client', 'mirror', 'discovery-service', 'mcp-server')) {\n"
148 " $mode = $word\n"
149 " break\n"
150 " }\n"
151 " }\n"
152 "\n"
153 " $binaryOptions = @(\n");
154
155 /* Binary options - use unified display API matching help system */
156 size_t binary_count = 0;
157 const option_descriptor_t *binary_opts = options_registry_get_for_display(MODE_DISCOVERY, true, &binary_count);
158
159 if (binary_opts) {
160 for (size_t i = 0; i < binary_count; i++) {
161 ps_write_option(output, &binary_opts[i]);
162 }
163 SAFE_FREE(binary_opts);
164 }
165
166 fprintf(output, " )\n\n $serverOptions = @(\n");
167
168 /* Server options - use unified display API matching help system */
169 size_t server_count = 0;
170 const option_descriptor_t *server_opts = options_registry_get_for_display(MODE_SERVER, false, &server_count);
171
172 if (server_opts) {
173 for (size_t i = 0; i < server_count; i++) {
174 ps_write_option(output, &server_opts[i]);
175 }
176 SAFE_FREE(server_opts);
177 }
178
179 fprintf(output, " )\n\n $clientOptions = @(\n");
180
181 /* Client options - use unified display API matching help system */
182 size_t client_count = 0;
183 const option_descriptor_t *client_opts = options_registry_get_for_display(MODE_CLIENT, false, &client_count);
184
185 if (client_opts) {
186 for (size_t i = 0; i < client_count; i++) {
187 ps_write_option(output, &client_opts[i]);
188 }
189 SAFE_FREE(client_opts);
190 }
191
192 fprintf(output, " )\n\n $mirrorOptions = @(\n");
193
194 /* Mirror options - use unified display API matching help system */
195 size_t mirror_count = 0;
196 const option_descriptor_t *mirror_opts = options_registry_get_for_display(MODE_MIRROR, false, &mirror_count);
197
198 if (mirror_opts) {
199 for (size_t i = 0; i < mirror_count; i++) {
200 ps_write_option(output, &mirror_opts[i]);
201 }
202 SAFE_FREE(mirror_opts);
203 }
204
205 fprintf(output, " )\n\n $mcpOptions = @(\n");
206 size_t mcp_count = 0;
207 const option_descriptor_t *mcp_opts = options_registry_get_for_display(MODE_MCP_SERVER, false, &mcp_count);
208 for (size_t i = 0; i < mcp_count; i++) {
209 ps_write_option(output, &mcp_opts[i]);
210 }
211 SAFE_FREE(mcp_opts);
212 fprintf(output, " )\n\n $discoverySvcOptions = @(\n");
213
214 /* Discovery-service options */
215 size_t discovery_svc_count = 0;
216 const option_descriptor_t *discovery_svc_opts = options_registry_get_for_display(MODE_DISCOVERY_SERVICE, false, &discovery_svc_count);
217
218 if (discovery_svc_opts) {
219 for (size_t i = 0; i < discovery_svc_count; i++) {
220 ps_write_option(output, &discovery_svc_opts[i]);
221 }
222 SAFE_FREE(discovery_svc_opts);
223 }
224
225 fprintf(output, " )\n"
226 "\n"
227 " $options = $binaryOptions\n"
228 " \n"
229 " if ($mode -eq 'server') {\n"
230 " $options += $serverOptions\n"
231 " } elseif ($mode -eq 'client') {\n"
232 " $options += $clientOptions\n"
233 " } elseif ($mode -eq 'mirror') {\n"
234 " $options += $mirrorOptions\n"
235 " } elseif ($mode -eq 'mcp-server') {\n"
236 " $options += $mcpOptions\n"
237 " } elseif ($mode -eq 'discovery-service') {\n"
238 " $options += $discoverySvcOptions\n"
239 " }\n"
240 "\n"
241 " $previousIndex = $words.Count - 1\n"
242 " if ($wordToComplete) { $previousIndex-- }\n"
243 " $valueOption = $options | Where-Object { $_.Name -eq $words[$previousIndex] } | Select-Object -First 1\n"
244 " if ($valueOption.Values -and -not $wordToComplete.StartsWith('-')) {\n"
245 " $valueOption.Values | Where-Object { $_ -like \"$wordToComplete*\" } | ForEach-Object {\n"
246 " [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $valueOption.Description)\n"
247 " }\n"
248 " return\n"
249 " }\n"
250 "\n"
251 " if (-not $mode -and -not $wordToComplete.StartsWith('-')) {\n"
252 " @('server', 'client', 'mirror', 'discovery-service', 'mcp-server') | Where-Object { $_ -like "
253 "\"$wordToComplete*\" } | "
254 "ForEach-Object {\n"
255 " [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', \"Mode: $_\")\n"
256 " }\n"
257 " } else {\n"
258 " $options | Sort-Object -Property Name -Unique | Where-Object { $_.Name -like \"$wordToComplete*\" } | "
259 "ForEach-Object {\n"
260 " [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterName', $_.Description)\n"
261 " }\n"
262 " }\n"
263 "}\n"
264 "\n"
265 "Register-ArgumentCompleter -Native -CommandName ascii-chat -ScriptBlock $script:AsciiChatCompleter\n");
266
267 return ASCIICHAT_OK;
268}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, MODE_CLIENT, MODE_DISCOVERY, MODE_DISCOVERY_SERVICE, MODE_MCP_SERVER, MODE_MIRROR, MODE_SERVER, options_registry_get_for_display(), SAFE_FREE, and SET_ERRNO.

Referenced by completions_generate_for_shell().

◆ completions_generate_zsh()

asciichat_error_t completions_generate_zsh ( FILE * output)

Definition at line 141 of file zsh.c.

141 {
142 if (!output) {
143 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
144 }
145
146 // Before a mode, expose the complete registry; after a mode, use its own options.
147 size_t binary_count = 0;
148 const option_descriptor_t *binary_opts = options_registry_get_for_display(MODE_DISCOVERY, true, &binary_count);
149
150 fprintf(output, "# Zsh completion script for ascii-chat\n"
151 "# Generated from options registry - DO NOT EDIT MANUALLY\n"
152 "\n"
153 "# Configure completion for ascii-chat\n"
154 "# Use only _complete (no corrections or approximate matching)\n"
155 "zstyle ':completion:*:ascii-chat:*' completer _complete\n"
156 "\n"
157 "# Device index completion helpers\n"
158 "_ascii_chat_webcam_indices() {\n"
159 " local -a indices\n"
160 " local output\n"
161 " output=$(\"${words[1]}\" --list-webcams 2>/dev/null) || return\n"
162 " while IFS= read -r line; do\n"
163 " [[ \"$line\" =~ ^[[:space:]]*([0-9]+)[[:space:]]+(.+)$ ]] && \\\n"
164 " indices+=(\"${match[1]}:${match[2]}\")\n"
165 " done <<< \"$output\"\n"
166 " _describe 'webcam' indices\n"
167 "}\n"
168 "\n"
169 "_ascii_chat_microphone_indices() {\n"
170 " local -a indices\n"
171 " local output\n"
172 " output=$(\"${words[1]}\" --list-microphones 2>/dev/null) || return\n"
173 " while IFS= read -r line; do\n"
174 " [[ \"$line\" =~ ^[[:space:]]*([0-9-]+)[[:space:]]+(.+)$ ]] && \\\n"
175 " indices+=(\"${match[1]}:${match[2]}\")\n"
176 " done <<< \"$output\"\n"
177 " _describe 'microphone' indices\n"
178 "}\n"
179 "\n"
180 "_ascii_chat_speakers_indices() {\n"
181 " local -a indices\n"
182 " local output\n"
183 " output=$(\"${words[1]}\" --list-speakers 2>/dev/null) || return\n"
184 " while IFS= read -r line; do\n"
185 " [[ \"$line\" =~ ^[[:space:]]*([0-9-]+)[[:space:]]+(.+)$ ]] && \\\n"
186 " indices+=(\"${match[1]}:${match[2]}\")\n"
187 " done <<< \"$output\"\n"
188 " _describe 'speakers' indices\n"
189 "}\n"
190 "\n"
191 "_ascii_chat_binary_grouped() {\n"
192 " local curcontext=$curcontext\n"
193 " local -a context line state_descr args\n"
194 " local state\n"
195 " local -A opt_args\n\n");
196
197 /* Write binary-level options in grouped format */
198 if (binary_opts) {
199 zsh_write_combined_args(output, binary_opts, binary_count, true);
200 }
201
202 fprintf(output, "}\n\n_ascii_chat() {\n"
203 " local i mode=''\n"
204 " for (( i=2; i<CURRENT; ++i )); do\n"
205 " case $words[i] in\n"
206 " --) break ;;\n"
207 " --*=*) continue ;;\n");
208 // Skip required option values so a file called 'mirror' is not a mode.
209 for (size_t i = 0; i < binary_count; ++i) {
210 const option_descriptor_t *opt = &binary_opts[i];
211 if (opt->type == OPTION_TYPE_ACTION || opt->type == OPTION_TYPE_BOOL || opt->optional_arg)
212 continue;
213 fprintf(output, " --%s", opt->long_name);
214 if (opt->short_name)
215 fprintf(output, "|-%c", opt->short_name);
216 fprintf(output, ") (( ++i )) ;;\n");
217 }
218 fprintf(output, " server|client|mirror|discovery-service|acds|mcp-server)\n"
219 " mode=$words[i]\n"
220 " words=(\"$words[1]\" \"${(@)words[i+1,-1]}\")\n"
221 " (( CURRENT -= i - 1 ))\n"
222 " break ;;\n"
223 " esac\n"
224 " done\n"
225 " case $mode in\n"
226 " # Server-like modes\n"
227 " server)\n"
228 " _ascii_chat_server\n"
229 " return\n"
230 " ;;\n"
231 " discovery-service|acds)\n"
232 " _ascii_chat_discovery_service\n"
233 " return\n"
234 " ;;\n"
235 "\n"
236 " mcp-server)\n"
237 " _ascii_chat_mcp_server\n"
238 " return\n"
239 " ;;\n"
240 " # Client-like modes\n"
241 " client)\n"
242 " _ascii_chat_client\n"
243 " return\n"
244 " ;;\n"
245 " mirror)\n"
246 " _ascii_chat_mirror\n"
247 " return\n"
248 " ;;\n"
249 " *) _ascii_chat_binary_grouped ;;\n"
250 " esac\n"
251 "}\n\n"
252 "_ascii_chat_modes() {\n"
253 " local -a server_modes client_modes\n"
254 " server_modes=(\n");
255
256 /* Generate server-like modes from registry */
257 size_t mode_server_count = 0;
258 const mode_descriptor_t *mode_server_descs = get_modes_by_group("server-like", &mode_server_count);
259 if (mode_server_descs) {
260 for (size_t i = 0; i < mode_server_count; i++) {
261 fprintf(output, " '%s:%s'\n", mode_server_descs[i].name, mode_server_descs[i].description);
262 }
263 SAFE_FREE(mode_server_descs);
264 }
265
266 fprintf(output, " )\n"
267 " client_modes=(\n");
268
269 /* Generate client-like modes from registry (exclude discovery since it's the default mode) */
270 size_t mode_client_count = 0;
271 const mode_descriptor_t *mode_client_descs = get_modes_by_group("client-like", &mode_client_count);
272 if (mode_client_descs) {
273 for (size_t i = 0; i < mode_client_count; i++) {
274 /* Skip discovery mode - it's the default when no mode is specified */
275 if (strcmp(mode_client_descs[i].name, "discovery") == 0) {
276 continue;
277 }
278 fprintf(output, " '%s:%s'\n", mode_client_descs[i].name, mode_client_descs[i].description);
279 }
280 SAFE_FREE(mode_client_descs);
281 }
282
283 fprintf(output, " )\n"
284 " _describe -t server-modes 'server-like modes' server_modes\n"
285 " _describe -t client-modes 'client-like modes' client_modes\n"
286 "}\n\n"
287 "# Server-like modes: handle incoming connections and stream management\n"
288 "_ascii_chat_server() {\n"
289 " local curcontext=$curcontext\n"
290 " local -a context line state_descr args\n"
291 " local state\n"
292 " local -A opt_args\n\n");
293
294 /* Server options - grouped by category */
295 size_t server_count = 0;
296 const option_descriptor_t *server_opts = options_registry_get_for_display(MODE_SERVER, false, &server_count);
297
298 if (server_opts) {
299 zsh_write_combined_args(output, server_opts, server_count, false);
300 SAFE_FREE(server_opts);
301 }
302
303 fprintf(output, "}\n"
304 "\n"
305 "_ascii_chat_discovery_service() {\n"
306 " local curcontext=$curcontext\n"
307 " local -a context line state_descr args\n"
308 " local state\n"
309 " local -A opt_args\n\n");
310
311 /* Discovery-service options - grouped by category */
312 size_t discovery_svc_count = 0;
313 const option_descriptor_t *discovery_svc_opts = options_registry_get_for_display(MODE_DISCOVERY_SERVICE, false, &discovery_svc_count);
314
315 if (discovery_svc_opts) {
316 zsh_write_combined_args(output, discovery_svc_opts, discovery_svc_count, false);
317 SAFE_FREE(discovery_svc_opts);
318 }
319
320 fprintf(output, "}\n"
321 "\n"
322 "# Client-like modes: connect to servers or render local media\n"
323 "_ascii_chat_client() {\n"
324 " local curcontext=$curcontext\n"
325 " local -a context line state_descr args\n"
326 " local state\n"
327 " local -A opt_args\n\n");
328
329 /* Client options - grouped by category */
330 size_t client_count = 0;
331 const option_descriptor_t *client_opts = options_registry_get_for_display(MODE_CLIENT, false, &client_count);
332
333 if (client_opts) {
334 zsh_write_combined_args(output, client_opts, client_count, false);
335 SAFE_FREE(client_opts);
336 }
337
338 fprintf(output, "}\n"
339 "\n"
340 "_ascii_chat_mirror() {\n"
341 " local curcontext=$curcontext\n"
342 " local -a context line state_descr args\n"
343 " local state\n"
344 " local -A opt_args\n\n");
345
346 /* Mirror options - grouped by category */
347 size_t mirror_count = 0;
348 const option_descriptor_t *mirror_opts = options_registry_get_for_display(MODE_MIRROR, false, &mirror_count);
349
350 if (mirror_opts) {
351 zsh_write_combined_args(output, mirror_opts, mirror_count, false);
352 SAFE_FREE(mirror_opts);
353 }
354
355 fprintf(output, "}\n"
356 "\n"
357 "# Register completion functions (must come after definitions)\n"
358 "compdef _ascii_chat ascii-chat\n"
359 "compdef _ascii_chat 'build/bin/ascii-chat'\n");
360
361 fprintf(output, "_ascii_chat_mcp_server() {\n"
362 " local curcontext=$curcontext\n"
363 " local -a context line state_descr args\n"
364 " local state\n"
365 " local -A opt_args\n\n");
366 size_t mcp_count = 0;
367 const option_descriptor_t *mcp_opts = options_registry_get_for_display(MODE_MCP_SERVER, false, &mcp_count);
368 if (mcp_opts) {
369 zsh_write_combined_args(output, mcp_opts, mcp_count, false);
370 SAFE_FREE(mcp_opts);
371 }
372 fprintf(output, "}\n\n");
373
374 SAFE_FREE(binary_opts);
375
376 return ASCIICHAT_OK;
377}
@ OPTION_TYPE_ACTION
Action that executes and may exit (–list-webcams, etc.).
Definition builder.h:176
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:171
const mode_descriptor_t * get_modes_by_group(const char *group, size_t *out_count)
Get all mode descriptors grouped by category.
const char * long_name
Long option name (e.g., "port").
Definition builder.h:253
char short_name
Short option char (e.g., 'p', or '\0' if none).
Definition builder.h:254
option_type_t type
Value type.
Definition builder.h:257
bool optional_arg
If true, argument is optional (for callbacks like –verbose).
Definition builder.h:285

References ASCIICHAT_OK, ERROR_INVALID_PARAM, get_modes_by_group(), option_descriptor_t::long_name, MODE_CLIENT, MODE_DISCOVERY, MODE_DISCOVERY_SERVICE, MODE_MCP_SERVER, MODE_MIRROR, MODE_SERVER, OPTION_TYPE_ACTION, OPTION_TYPE_BOOL, option_descriptor_t::optional_arg, options_registry_get_for_display(), SAFE_FREE, SET_ERRNO, option_descriptor_t::short_name, and option_descriptor_t::type.

Referenced by completions_generate_for_shell().

◆ completions_get_shell_name()

const char * completions_get_shell_name ( completion_format_t format)

Get human-readable shell name for format.

Parameters
formatCompletion format
Returns
Shell name string (e.g., "bash", "fish"), or "unknown" if invalid

Definition at line 43 of file completions.c.

43 {
44 switch (format) {
53 default:
54 return "unknown";
55 }
56}
#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

References COMPLETION_FORMAT_BASH, COMPLETION_FORMAT_FISH, COMPLETION_FORMAT_POWERSHELL, COMPLETION_FORMAT_ZSH, OPT_COMPLETION_SHELL_BASH, OPT_COMPLETION_SHELL_FISH, OPT_COMPLETION_SHELL_POWERSHELL, and OPT_COMPLETION_SHELL_ZSH.

◆ completions_parse_shell_name()

completion_format_t completions_parse_shell_name ( const char * shell_name)

Parse shell name string to completion format.

Converts a shell name (e.g., "bash", "fish") to the corresponding completion format enum. Case-insensitive.

Parameters
shell_nameShell name string (e.g., "bash", "fish", "zsh")
Returns
Completion format, or COMPLETION_FORMAT_UNKNOWN if not recognized

Definition at line 58 of file completions.c.

58 {
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}
#define OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE
Definition completions.c:8
@ COMPLETION_FORMAT_UNKNOWN
Definition completions.h:71

References COMPLETION_FORMAT_BASH, COMPLETION_FORMAT_FISH, COMPLETION_FORMAT_POWERSHELL, COMPLETION_FORMAT_UNKNOWN, COMPLETION_FORMAT_ZSH, OPT_COMPLETION_SHELL_BASH, OPT_COMPLETION_SHELL_FISH, OPT_COMPLETION_SHELL_POWERSHELL, OPT_COMPLETION_SHELL_ZSH, and OPTIONS_COMPLETIONS_LOWER_BUFFER_SIZE.