ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
zsh.h File Reference

Zsh shell completion generator. More...

Go to the source code of this file.

Functions

asciichat_error_t completions_generate_zsh (FILE *output)
 Generate zsh completion script.

Detailed Description

Zsh shell completion generator.

Definition in file zsh.h.

Function Documentation

◆ completions_generate_zsh()

asciichat_error_t completions_generate_zsh ( FILE * output)

Generate zsh completion script.

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}
#define SET_ERRNO(code,...)
@ OPTION_TYPE_ACTION
Definition builder.h:186
@ OPTION_TYPE_BOOL
Boolean flag (–flag, no value).
Definition builder.h:181
#define SAFE_FREE(ptr)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ 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 mode_descriptor_t * get_modes_by_group(const char *group, size_t *out_count)
Get all mode descriptors grouped by category.
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:208
The complete registry entry for one command-line/configuration option.
Definition builder.h:271
const char * long_name
Long option name (e.g., "port").
Definition builder.h:273
char short_name
Short option char (e.g., 'p', or '\0' if none).
Definition builder.h:274
option_type_t type
Value type.
Definition builder.h:277