Line data Source code
1 : #include "path_complete.h"
2 : #include "platform/terminal.h"
3 : #include <stdio.h>
4 : #include <stdlib.h>
5 : #include <string.h>
6 : #include <dirent.h>
7 :
8 : /* ── Completion state ────────────────────────────────────────────────── */
9 :
10 : static struct {
11 : char (*names)[256]; /* sorted match names (bare, no trailing '/') */
12 : int count;
13 : int idx; /* currently highlighted entry */
14 : int view_start; /* first visible entry in the display row */
15 : char dir[2048]; /* directory part (ends with '/') */
16 : char expected[4096]; /* il->buf[0..cur] when this set was built */
17 : char suffix[4096]; /* il->buf[cur..len] when this set was built */
18 : } g_comp;
19 :
20 25 : static void g_comp_free(void) {
21 25 : free(g_comp.names);
22 25 : memset(&g_comp, 0, sizeof(g_comp));
23 25 : }
24 :
25 23 : static int name_cmp(const void *a, const void *b) {
26 23 : return strcmp((const char *)a, (const char *)b);
27 : }
28 :
29 : /* ── Rendering ───────────────────────────────────────────────────────── */
30 :
31 : /* Render the completion bar one row below the input line.
32 : * Does nothing when there are no completions (preserves status bar). */
33 0 : static void render_completions(const InputLine *il) {
34 0 : if (g_comp.count == 0) return;
35 0 : int row = il->trow + 1;
36 0 : printf("\033[%d;1H\033[2K", row);
37 :
38 0 : int tcols = terminal_cols();
39 :
40 : /* Keep view_start <= idx */
41 0 : if (g_comp.idx < g_comp.view_start)
42 0 : g_comp.view_start = g_comp.idx;
43 :
44 : /* Advance view_start until idx fits inside the visible window */
45 0 : for (;;) {
46 0 : int pos = 2 + (g_comp.view_start > 0 ? 2 : 0);
47 0 : int last = g_comp.view_start - 1;
48 0 : for (int i = g_comp.view_start; i < g_comp.count; i++) {
49 0 : int w = (int)strlen(g_comp.names[i]) + 2;
50 0 : int need = (i < g_comp.count - 1) ? 3 : 0;
51 0 : if (pos + w + need > tcols) break;
52 0 : last = i;
53 0 : pos += w;
54 : }
55 0 : if (last >= g_comp.idx) break;
56 0 : g_comp.view_start++;
57 : }
58 :
59 0 : printf("\033[%d;1H ", row);
60 0 : if (g_comp.view_start > 0)
61 0 : printf("\033[2m< \033[0m");
62 :
63 0 : int pos = 2 + (g_comp.view_start > 0 ? 2 : 0);
64 0 : for (int i = g_comp.view_start; i < g_comp.count; i++) {
65 0 : int w = (int)strlen(g_comp.names[i]) + 2;
66 0 : if (pos + w + (i < g_comp.count - 1 ? 3 : 0) > tcols) {
67 0 : printf("...");
68 0 : break;
69 : }
70 0 : if (i == g_comp.idx) printf("\033[7m");
71 0 : printf("%s", g_comp.names[i]);
72 0 : if (i == g_comp.idx) printf("\033[0m");
73 0 : printf(" ");
74 0 : pos += w;
75 : }
76 0 : fflush(stdout);
77 : }
78 :
79 : /* ── Helpers ─────────────────────────────────────────────────────────── */
80 :
81 : /* Apply g_comp.names[g_comp.idx]: replace il->buf[0..cur] with dir+name,
82 : * then reattach the stored suffix. */
83 19 : static void apply_comp(InputLine *il) {
84 19 : char head[4096];
85 19 : snprintf(head, sizeof(head), "%s%s", g_comp.dir, g_comp.names[g_comp.idx]);
86 19 : snprintf(il->buf, il->bufsz, "%s%s", head, g_comp.suffix);
87 19 : il->len = strlen(il->buf);
88 19 : il->cur = strlen(head);
89 19 : snprintf(g_comp.expected, sizeof(g_comp.expected), "%s", head);
90 19 : }
91 :
92 : /* Copy il->buf[0..il->cur] into head (NUL-terminated).
93 : * Returns 0 on success, -1 if head would overflow. */
94 21 : static int make_head(const InputLine *il, char *head, size_t headsz) {
95 21 : if (il->cur >= headsz) return -1;
96 21 : memcpy(head, il->buf, il->cur);
97 21 : head[il->cur] = '\0';
98 21 : return 0;
99 : }
100 :
101 : /* ── Callbacks ───────────────────────────────────────────────────────── */
102 :
103 17 : static void path_tab_fn(InputLine *il) {
104 17 : char head[4096];
105 17 : if (make_head(il, head, sizeof(head)) != 0) return;
106 :
107 : /* Cycling: head still matches last completion → advance */
108 17 : if (g_comp.count > 0 && strcmp(head, g_comp.expected) == 0) {
109 5 : g_comp.idx = (g_comp.idx + 1) % g_comp.count;
110 5 : apply_comp(il);
111 5 : return;
112 : }
113 :
114 : /* Fresh scan based on head */
115 12 : g_comp_free();
116 12 : snprintf(g_comp.suffix, sizeof(g_comp.suffix), "%s", il->buf + il->cur);
117 :
118 : const char *prefix;
119 12 : char *slash = strrchr(head, '/');
120 12 : if (slash) {
121 12 : size_t dlen = (size_t)(slash - head + 1);
122 12 : if (dlen >= sizeof(g_comp.dir)) return;
123 12 : memcpy(g_comp.dir, head, dlen);
124 12 : g_comp.dir[dlen] = '\0';
125 12 : prefix = slash + 1;
126 : } else {
127 0 : strcpy(g_comp.dir, "./");
128 0 : prefix = head;
129 : }
130 :
131 12 : DIR *d = opendir(g_comp.dir);
132 12 : if (!d) return;
133 :
134 12 : int cap = 0;
135 12 : size_t pfxlen = strlen(prefix);
136 : struct dirent *ent;
137 58 : while ((ent = readdir(d)) != NULL) {
138 46 : if (ent->d_name[0] == '.' && pfxlen == 0) continue;
139 37 : if (pfxlen > 0 && strncmp(ent->d_name, prefix, pfxlen) != 0) continue;
140 23 : if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
141 21 : if (g_comp.count == cap) {
142 11 : int nc = cap ? cap * 2 : 16;
143 11 : char (*tmp)[256] = realloc(g_comp.names,
144 11 : (size_t)nc * sizeof(*g_comp.names));
145 11 : if (!tmp) { closedir(d); g_comp_free(); return; }
146 11 : g_comp.names = tmp;
147 11 : cap = nc;
148 : }
149 21 : snprintf(g_comp.names[g_comp.count], 256, "%s", ent->d_name);
150 21 : g_comp.count++;
151 : }
152 12 : closedir(d);
153 :
154 12 : if (g_comp.count == 0) return;
155 :
156 11 : qsort(g_comp.names, (size_t)g_comp.count,
157 : sizeof(g_comp.names[0]), name_cmp);
158 11 : g_comp.idx = 0;
159 11 : g_comp.view_start = 0;
160 11 : apply_comp(il);
161 : }
162 :
163 4 : static void path_shift_tab_fn(InputLine *il) {
164 4 : char head[4096];
165 4 : if (make_head(il, head, sizeof(head)) != 0) return;
166 4 : if (g_comp.count == 0 || strcmp(head, g_comp.expected) != 0) return;
167 3 : g_comp.idx = (g_comp.idx + g_comp.count - 1) % g_comp.count;
168 3 : apply_comp(il);
169 : }
170 :
171 : /* ── Public API ──────────────────────────────────────────────────────── */
172 :
173 12 : void path_complete_attach(InputLine *il) {
174 12 : il->tab_fn = path_tab_fn;
175 12 : il->shift_tab_fn = path_shift_tab_fn;
176 12 : il->render_below = render_completions;
177 12 : }
178 :
179 13 : void path_complete_reset(void) {
180 13 : g_comp_free();
181 13 : }
|