ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
powershell.c
Go to the documentation of this file.
1
6
7#include <string.h>
8#include <stdio.h>
11#include <ascii-chat/common.h>
12
17static void ps_escape_help(FILE *output, const char *text) {
18 if (!text) {
19 return;
20 }
21
22 for (const char *p = text; *p; p++) {
23 if (*p == '\'') {
24 // Escape single quotes by doubling them
25 fprintf(output, "''");
26 } else if (*p == '\n' || *p == '\t') {
27 // Convert newlines and tabs to spaces
28 fprintf(output, " ");
29 } else {
30 fputc(*p, output);
31 }
32 }
33}
34
35static void ps_write_option(FILE *output, const option_descriptor_t *opt) {
36 if (!opt) {
37 return;
38 }
39
40 // Get completion metadata for this option
42
43 // Build values array if metadata exists
44 if (meta) {
45 if (meta->input_type == OPTION_INPUT_ENUM && meta->enum_values && meta->enum_values[0] != NULL) {
46 // Enum values
47 if (opt->short_name != '\0') {
48 fprintf(output, " @{ Name = '-%c'; Description = '", opt->short_name);
49 ps_escape_help(output, opt->help_text);
50 fprintf(output, "'; Values = @(");
51 for (size_t i = 0; meta->enum_values[i] != NULL; i++) {
52 if (i > 0)
53 fprintf(output, ", ");
54 fprintf(output, "'%s'", meta->enum_values[i]);
55 }
56 fprintf(output, ") }\n");
57 }
58 fprintf(output, " @{ Name = '--%s'; Description = '", opt->long_name);
59 ps_escape_help(output, opt->help_text);
60 fprintf(output, "'; Values = @(");
61 for (size_t i = 0; meta->enum_values[i] != NULL; i++) {
62 if (i > 0)
63 fprintf(output, ", ");
64 fprintf(output, "'%s'", meta->enum_values[i]);
65 }
66 fprintf(output, ") }\n");
67 return;
68 } else if (meta->examples && meta->examples[0] != NULL) {
69 // Example values (practical values, higher priority than calculated ranges)
70 if (opt->short_name != '\0') {
71 fprintf(output, " @{ Name = '-%c'; Description = '", opt->short_name);
72 ps_escape_help(output, opt->help_text);
73 fprintf(output, "'; Values = @(");
74 for (size_t i = 0; meta->examples[i] != NULL; i++) {
75 if (i > 0)
76 fprintf(output, ", ");
77 fprintf(output, "'%s'", meta->examples[i]);
78 }
79 fprintf(output, ") }\n");
80 }
81 fprintf(output, " @{ Name = '--%s'; Description = '", opt->long_name);
82 ps_escape_help(output, opt->help_text);
83 fprintf(output, "'; Values = @(");
84 for (size_t i = 0; meta->examples[i] != NULL; i++) {
85 if (i > 0)
86 fprintf(output, ", ");
87 fprintf(output, "'%s'", meta->examples[i]);
88 }
89 fprintf(output, ") }\n");
90 return;
91 } else if (meta->input_type == OPTION_INPUT_NUMERIC) {
92 // Numeric range - suggest min, middle, max values
93 if (opt->short_name != '\0') {
94 fprintf(output, " @{ Name = '-%c'; Description = '", opt->short_name);
95 ps_escape_help(output, opt->help_text);
96 fprintf(output, " (numeric %d-%d)'; Values = @(", meta->numeric_range.min, meta->numeric_range.max);
97 fprintf(output, "'%d'", meta->numeric_range.min);
98 if (meta->numeric_range.max > meta->numeric_range.min) {
99 int middle = (meta->numeric_range.min + meta->numeric_range.max) / 2;
100 fprintf(output, ", '%d'", middle);
101 fprintf(output, ", '%d'", meta->numeric_range.max);
102 }
103 fprintf(output, ") }\n");
104 }
105 fprintf(output, " @{ Name = '--%s'; Description = '", opt->long_name);
106 ps_escape_help(output, opt->help_text);
107 fprintf(output, " (numeric %d-%d)'; Values = @(", meta->numeric_range.min, meta->numeric_range.max);
108 fprintf(output, "'%d'", meta->numeric_range.min);
109 if (meta->numeric_range.max > meta->numeric_range.min) {
110 int middle = (meta->numeric_range.min + meta->numeric_range.max) / 2;
111 fprintf(output, ", '%d'", middle);
112 fprintf(output, ", '%d'", meta->numeric_range.max);
113 }
114 fprintf(output, ") }\n");
115 return;
116 }
117 }
118
119 // Basic option without values
120 if (opt->short_name != '\0') {
121 fprintf(output, " @{ Name = '-%c'; Description = '", opt->short_name);
122 ps_escape_help(output, opt->help_text);
123 fprintf(output, "' }\n");
124 }
125 fprintf(output, " @{ Name = '--%s'; Description = '", opt->long_name);
126 ps_escape_help(output, opt->help_text);
127 fprintf(output, "' }\n");
128}
129
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}
#define SET_ERRNO(code,...)
@ OPTION_INPUT_NUMERIC
Numeric value with optional min/max/step.
Definition builder.h:188
@ OPTION_INPUT_ENUM
Choose from fixed set of enum values.
Definition builder.h:187
#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.
asciichat_error_t completions_generate_powershell(FILE *output)
Generate PowerShell completion script.
Definition powershell.c:130
PowerShell completion generator.
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