ascii-chat 0.11.33
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 96 of file completions.c.

96 {
97 if (!count) {
98 return NULL;
99 }
100
101 option_descriptor_t *combined_opts = NULL;
102 size_t combined_count = 0;
103
104 /* All completion modes to iterate through */
106 const size_t modes_len = sizeof(modes) / sizeof(modes[0]);
107
108 for (size_t m = 0; m < modes_len; m++) {
109 size_t mode_count = 0;
110 const option_descriptor_t *mode_opts = options_registry_get_for_mode(modes[m], &mode_count);
111 if (mode_opts) {
112 /* Collect unique options by long_name */
113 for (size_t i = 0; i < mode_count; i++) {
114 bool already_has = false;
115 for (size_t j = 0; j < combined_count; j++) {
116 if (strcmp(combined_opts[j].long_name, mode_opts[i].long_name) == 0) {
117 already_has = true;
118 break;
119 }
120 }
121 if (!already_has) {
122 combined_count++;
124 combined_opts, combined_count * sizeof(option_descriptor_t), option_descriptor_t *);
125 if (temp) {
126 combined_opts = temp;
127 combined_opts[combined_count - 1] = mode_opts[i];
128 }
129 }
130 }
131 SAFE_FREE(mode_opts);
132 }
133 }
134
135 *count = combined_count;
136 return combined_opts;
137}
#define SAFE_REALLOC(ptr, size, cast)
Definition common.h:284
#define SAFE_FREE(ptr)
Definition common.h:376
asciichat_mode_t
Mode type for options parsing.
@ MODE_CLIENT
Client mode - network client options.
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
@ MODE_SERVER
Server mode - network server options.
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network)
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
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:218
Option descriptor.
Definition builder.h:241

References MODE_CLIENT, MODE_DISCOVERY, MODE_DISCOVERY_SERVICE, 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 20 of file completions.c.

20 {
21 if (!output) {
22 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
23 }
24
25 switch (format) {
27 return completions_generate_bash(output);
29 return completions_generate_fish(output);
31 return completions_generate_zsh(output);
34 default:
35 return SET_ERRNO(ERROR_INVALID_PARAM, "Unknown completion format: %d", format);
36 }
37}
asciichat_error_t completions_generate_bash(FILE *output)
Definition bash.c:367
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:210
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ 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 39 of file completions.c.

39 {
40 switch (format) {
42 return "bash";
44 return "fish";
46 return "zsh";
48 return "powershell";
49 default:
50 return "unknown";
51 }
52}

References COMPLETION_FORMAT_BASH, COMPLETION_FORMAT_FISH, COMPLETION_FORMAT_POWERSHELL, and COMPLETION_FORMAT_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 54 of file completions.c.

54 {
55 if (!shell_name) {
57 }
58
59 /* Convert to lowercase for case-insensitive matching */
60 char lower[32] = {0};
61 size_t len = strlen(shell_name);
62 if (len >= sizeof(lower)) {
64 }
65
66 for (size_t i = 0; i < len; i++) {
67 lower[i] = tolower((unsigned char)shell_name[i]);
68 }
69
70 if (strcmp(lower, "bash") == 0) {
72 } else if (strcmp(lower, "fish") == 0) {
74 } else if (strcmp(lower, "zsh") == 0) {
76 } else if (strcmp(lower, "powershell") == 0 || strcmp(lower, "ps") == 0) {
78 }
79
81}

References COMPLETION_FORMAT_BASH, COMPLETION_FORMAT_FISH, COMPLETION_FORMAT_POWERSHELL, COMPLETION_FORMAT_UNKNOWN, and COMPLETION_FORMAT_ZSH.

Referenced by action_completions().