ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
completions.h File Reference

Auto-generated shell completions from options registry. More...

Go to the source code of this file.

Enumerations

enum  completion_format_t {
  COMPLETION_FORMAT_BASH , COMPLETION_FORMAT_FISH , COMPLETION_FORMAT_ZSH , COMPLETION_FORMAT_POWERSHELL ,
  COMPLETION_FORMAT_UNKNOWN
}
 Supported shell completion formats. More...

Functions

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

Auto-generated shell completions from options registry.

This module provides auto-generated completion scripts for various shells (bash, fish, zsh, powershell) by extracting option metadata from the centralized options registry.

Usage:

# Generate bash completions
eval "$(ascii-chat --completions bash)"
# Generate fish completions
ascii-chat --completions fish | source
# Generate zsh completions
eval "$(ascii-chat --completions zsh)"
# Generate PowerShell completions
ascii-chat --completions powershell | Out-String | Invoke-Expression

Architecture:

The completion system has three layers:

  1. Format Enum (completion_format_t): Identifies target shell
  2. Format Generators: Shell-specific implementations (bash.c, fish.c, etc.)
  3. Dispatcher: Routes to appropriate generator based on format

All generators extract metadata from the centralized options registry via:

Adding New Shells:

To add support for a new shell:

  1. Create lib/options/completions/SHELL_NAME.h and .c
  2. Implement completions_generate_SHELL_NAME() function
  3. Add case to completions_generate_for_shell() dispatcher
  4. Update completion_parse_shell_name() to recognize new shell
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file completions.h.

Enumeration Type Documentation

◆ completion_format_t

Supported shell completion formats.

Enumerator
COMPLETION_FORMAT_BASH 

Bash shell completion

COMPLETION_FORMAT_FISH 

Fish shell completion

COMPLETION_FORMAT_ZSH 

Zsh shell completion

COMPLETION_FORMAT_POWERSHELL 

PowerShell completion

COMPLETION_FORMAT_UNKNOWN 

Unknown/invalid format

Definition at line 66 of file completions.h.

66 {
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

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 */
111 const size_t modes_len = sizeof(modes) / sizeof(modes[0]);
112
113 for (size_t m = 0; m < modes_len; m++) {
114 size_t mode_count = 0;
115 const option_descriptor_t *mode_opts = options_registry_get_for_mode(modes[m], &mode_count);
116 if (mode_opts) {
117 /* Collect unique options by long_name */
118 for (size_t i = 0; i < mode_count; i++) {
119 bool already_has = false;
120 for (size_t j = 0; j < combined_count; j++) {
121 if (strcmp(combined_opts[j].long_name, mode_opts[i].long_name) == 0) {
122 already_has = true;
123 break;
124 }
125 }
126 if (!already_has) {
127 combined_count++;
129 combined_opts, combined_count * sizeof(option_descriptor_t), option_descriptor_t *);
130 if (temp) {
131 combined_opts = temp;
132 combined_opts[combined_count - 1] = mode_opts[i];
133 }
134 }
135 }
136 SAFE_FREE(mode_opts);
137 }
138 }
139
140 *count = combined_count;
141 return combined_opts;
142}
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:285
#define SAFE_FREE(ptr)
Definition common.h:377
asciichat_mode_t
Mode type for options parsing.
Definition options.h:919
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:921
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:925
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:923
@ MODE_SERVER
Server mode - network server options.
Definition options.h:920
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:922
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:924
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:170
The complete registry entry for one command-line/configuration option.
Definition builder.h:249

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_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}
#define SET_ERRNO(code,...)
asciichat_error_t completions_generate_bash(FILE *output)
Definition bash.c:379
asciichat_error_t completions_generate_powershell(FILE *output)
Definition powershell.c:130
asciichat_error_t completions_generate_fish(FILE *output)
Definition fish.c:130
asciichat_error_t completions_generate_zsh(FILE *output)
Definition zsh.c:142
@ ERROR_INVALID_PARAM

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.

Referenced by action_completions().

◆ 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.

Referenced by action_completions().

◆ 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

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.

Referenced by action_completions().