ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
fish.c
Go to the documentation of this file.
1
6
7#include <string.h>
8#include <stdio.h>
12#include <ascii-chat/common.h>
13
18static void fish_escape_help(FILE *output, const char *text) {
19 if (!text) {
20 return;
21 }
22
23 for (const char *p = text; *p; p++) {
24 if (*p == '\'') {
25 // Escape single quotes by ending quote, adding escaped quote, starting quote again
26 fprintf(output, "'\\''");
27 } else if (*p == '\n' || *p == '\t') {
28 // Convert newlines and tabs to spaces
29 fprintf(output, " ");
30 } else {
31 fputc(*p, output);
32 }
33 }
34}
35
36static void fish_write_option(FILE *output, const option_descriptor_t *opt, const char *condition) {
37 if (!opt) {
38 return;
39 }
40
41 // Get completion metadata for this option
43
44 // Determine if we should exclude file completion
45 bool exclusive = false;
46
47 if (meta) {
48 if (meta->input_type == OPTION_INPUT_ENUM && meta->enum_values && meta->enum_values[0] != NULL) {
49 exclusive = true;
50 // Enum values as completions
51 // Output short option once with first value
52 if (opt->short_name != '\0') {
53 fprintf(output, "complete -c ascii-chat %s -s %c -x -a '%s' -d '", condition, opt->short_name, meta->enum_values[0]);
54 fish_escape_help(output, opt->help_text);
55 fprintf(output, "'\n");
56 }
57 // Output long option with all values
58 for (size_t i = 0; meta->enum_values[i] != NULL; i++) {
59 fprintf(output, "complete -c ascii-chat %s -l %s -x -a '%s' -d '", condition, opt->long_name, meta->enum_values[i]);
60 fish_escape_help(output, opt->help_text);
61 fprintf(output, "'\n");
62 }
63 return;
64 } else if (meta->input_type == OPTION_INPUT_FILEPATH) {
65 // File paths use default fish file completion
66 exclusive = false;
67 } else if (meta->examples && meta->examples[0] != NULL) {
68 exclusive = true;
69 // Example values as completions (practical values, higher priority than calculated ranges)
70 // Output short option once with first example
71 if (opt->short_name != '\0') {
72 fprintf(output, "complete -c ascii-chat %s -s %c -x -a '%s' -d '", condition, opt->short_name, meta->examples[0]);
73 fish_escape_help(output, opt->help_text);
74 fprintf(output, "'\n");
75 }
76 // Output long option with all examples
77 for (size_t i = 0; meta->examples[i] != NULL; i++) {
78 fprintf(output, "complete -c ascii-chat %s -l %s -x -a '%s' -d '", condition, opt->long_name, meta->examples[i]);
79 fish_escape_help(output, opt->help_text);
80 fprintf(output, "'\n");
81 }
82 return;
83 } else if (meta->input_type == OPTION_INPUT_NUMERIC) {
84 exclusive = true;
85 // Numeric range - suggest min, middle, max values
86 if (opt->short_name != '\0') {
87 fprintf(output, "complete -c ascii-chat %s -s %c -x -a '%d' -d 'numeric (%d-%d)'\n", condition, opt->short_name, meta->numeric_range.min, meta->numeric_range.min, meta->numeric_range.max);
88 }
89 fprintf(output, "complete -c ascii-chat %s -l %s -x -a '%d' -d 'numeric (%d-%d)'\n", condition, opt->long_name, meta->numeric_range.min, meta->numeric_range.min, meta->numeric_range.max);
90 if (meta->numeric_range.max > meta->numeric_range.min) {
91 int middle = (meta->numeric_range.min + meta->numeric_range.max) / 2;
92 fprintf(output, "complete -c ascii-chat %s -l %s -x -a '%d' -d 'numeric (middle)'\n", condition, opt->long_name, middle);
93 fprintf(output, "complete -c ascii-chat %s -l %s -x -a '%d' -d 'numeric (max)'\n", condition, opt->long_name, meta->numeric_range.max);
94 }
95 return;
96 }
97 }
98
99 // Basic completion without values
100 if (opt->short_name != '\0') {
101 if (exclusive) {
102 fprintf(output, "complete -c ascii-chat %s -s %c -x -d '", condition, opt->short_name);
103 fish_escape_help(output, opt->help_text);
104 fprintf(output, "'\n");
105 } else {
106 fprintf(output, "complete -c ascii-chat %s -s %c -d '", condition, opt->short_name);
107 fish_escape_help(output, opt->help_text);
108 fprintf(output, "'\n");
109 }
110 }
111 if (exclusive) {
112 fprintf(output, "complete -c ascii-chat %s -l %s -x -d '", condition, opt->long_name);
113 fish_escape_help(output, opt->help_text);
114 fprintf(output, "'\n");
115 } else {
116 fprintf(output, "complete -c ascii-chat %s -l %s -d '", condition, opt->long_name);
117 fish_escape_help(output, opt->help_text);
118 fprintf(output, "'\n");
119 }
120}
121
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}
#define SET_ERRNO(code,...)
@ OPTION_INPUT_NUMERIC
Numeric value with optional min/max/step.
Definition builder.h:188
@ OPTION_INPUT_FILEPATH
File path completion.
Definition builder.h:190
@ OPTION_INPUT_ENUM
Choose from fixed set of enum values.
Definition builder.h:187
Option enum value definitions - single source of truth.
asciichat_error_t completions_generate_fish(FILE *output)
Generate fish completion script.
Definition fish.c:122
Fish shell completion generator.
#define SAFE_FREE(ptr)
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
@ 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.
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
const option_metadata_t * options_registry_get_metadata(const char *long_name)
Get complete metadata for an option.
Definition public_api.c:251
🔤 String Manipulation and Shell Escaping Utilities
The complete registry entry for one command-line/configuration option.
Definition builder.h:251
const char * help_text
Description for –help.
Definition builder.h:261
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
Completion metadata attached to one option descriptor.
Definition builder.h:201
int max
Maximum value (or 0 if no limit).
Definition builder.h:210
const char ** enum_values
Enum value strings (null-terminated, e.g., {"auto", "none", "16", "256", "truecolor",...
Definition builder.h:203
int min
Minimum value (or 0 if no limit).
Definition builder.h:209
option_input_type_t input_type
What kind of input this option expects.
Definition builder.h:221
const char ** examples
Example values or command invocations (null-terminated array).
Definition builder.h:215
struct option_metadata_t::@061372117173071240020206164036331043262170062306 numeric_range