ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
completions.c
Go to the documentation of this file.
1
7#include <string.h>
8#include <ctype.h>
9#include <ascii-chat/options/completions/completions.h>
10#include <ascii-chat/options/registry.h>
11#include <ascii-chat/common.h>
12#include <ascii-chat/log/logging.h>
13
14/* Forward declarations for format-specific generators */
15asciichat_error_t completions_generate_bash(FILE *output);
16asciichat_error_t completions_generate_fish(FILE *output);
17asciichat_error_t completions_generate_zsh(FILE *output);
18asciichat_error_t completions_generate_powershell(FILE *output);
19
20asciichat_error_t completions_generate_for_shell(completion_format_t format, FILE *output) {
21 if (!output) {
22 return SET_ERRNO(ERROR_INVALID_PARAM, "Output stream cannot be NULL");
23 }
24
25 switch (format) {
26 case COMPLETION_FORMAT_BASH:
27 return completions_generate_bash(output);
28 case COMPLETION_FORMAT_FISH:
29 return completions_generate_fish(output);
30 case COMPLETION_FORMAT_ZSH:
31 return completions_generate_zsh(output);
32 case COMPLETION_FORMAT_POWERSHELL:
34 default:
35 return SET_ERRNO(ERROR_INVALID_PARAM, "Unknown completion format: %d", format);
36 }
37}
38
39const char *completions_get_shell_name(completion_format_t format) {
40 switch (format) {
41 case COMPLETION_FORMAT_BASH:
42 return "bash";
43 case COMPLETION_FORMAT_FISH:
44 return "fish";
45 case COMPLETION_FORMAT_ZSH:
46 return "zsh";
47 case COMPLETION_FORMAT_POWERSHELL:
48 return "powershell";
49 default:
50 return "unknown";
51 }
52}
53
54completion_format_t completions_parse_shell_name(const char *shell_name) {
55 if (!shell_name) {
56 return COMPLETION_FORMAT_UNKNOWN;
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)) {
63 return COMPLETION_FORMAT_UNKNOWN;
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) {
71 return COMPLETION_FORMAT_BASH;
72 } else if (strcmp(lower, "fish") == 0) {
73 return COMPLETION_FORMAT_FISH;
74 } else if (strcmp(lower, "zsh") == 0) {
75 return COMPLETION_FORMAT_ZSH;
76 } else if (strcmp(lower, "powershell") == 0 || strcmp(lower, "ps") == 0) {
77 return COMPLETION_FORMAT_POWERSHELL;
78 }
79
80 return COMPLETION_FORMAT_UNKNOWN;
81}
82
96option_descriptor_t *completions_collect_all_modes_unique(size_t *count) {
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 */
105 asciichat_mode_t modes[] = {MODE_DISCOVERY, MODE_SERVER, MODE_CLIENT, MODE_MIRROR, MODE_DISCOVERY_SERVICE};
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++;
123 option_descriptor_t *temp = (option_descriptor_t *)SAFE_REALLOC(
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}
completion_format_t completions_parse_shell_name(const char *shell_name)
Definition completions.c:54
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_for_shell(completion_format_t format, FILE *output)
Definition completions.c:20
asciichat_error_t completions_generate_fish(FILE *output)
Definition fish.c:130
asciichat_error_t completions_generate_zsh(FILE *output)
Definition zsh.c:105
const char * completions_get_shell_name(completion_format_t format)
Definition completions.c:39
option_descriptor_t * completions_collect_all_modes_unique(size_t *count)
Collect options from all modes with deduplication.
Definition completions.c:96
const option_descriptor_t * options_registry_get_for_mode(asciichat_mode_t mode, size_t *num_options)
Definition public_api.c:217