git ssb

0+

cel / noice



Commit 131ad0d7d190a6b75282372e050a0f67b05a027d

Toggle to only show known files and directories

lostd committed on 11/26/2014, 4:15:17 PM
Parent: 414c9e27fcbb182c36d112d5236db8de3a7f2e24

Files changed

config.def.hchanged
noice.cchanged
config.def.hView
@@ -43,5 +43,9 @@
4343 /* Shell */
4444 { '!', SEL_SH },
4545 /* Change dir */
4646 { 'c', SEL_CD },
47 + /* Toggle known */
48 + { 's', SEL_KNOW },
4749 };
50 +
51 +int know = 0;
noice.cView
@@ -54,8 +54,9 @@
5454 SEL_PGDN,
5555 SEL_PGUP,
5656 SEL_SH,
5757 SEL_CD,
58 + SEL_KNOW,
5859 };
5960
6061 struct key {
6162 int sym; /* Key pressed */
@@ -223,9 +224,91 @@
223224 return 1;
224225 return 0;
225226 }
226227
228 +char *
229 +mksuper(char **a, unsigned int n)
230 +{
231 + unsigned int i, len;
232 + char *super, *regex;
233 +
234 + if (n == 0)
235 + return NULL;
236 + if (n == 1)
237 + return a[1];
238 +
239 + super = xstrdup("(");
240 + for (i = 0; i < n; i++) {
241 + regex = a[i];
242 + len = strlen(super) + 1 + strlen(regex) + 1;
243 + super = xrealloc(super, len);
244 + if (i != 0)
245 + strlcat(super, "|", len);
246 + strlcat(super, regex, len);
247 + }
248 + len = strlen(super) + 1 + 1;
249 + super = xrealloc(super, len);
250 + strlcat(super, ")", len);
251 +
252 + return super;
253 +}
254 +
255 +/* Combine all but the last assoc regex in an OR-ed super regex */
256 +char *
257 +mksuperassoc(void)
258 +{
259 + unsigned int i;
260 + char **regexes;
261 + char *super;
262 +
263 + regexes = xmalloc((LEN(assocs) - 1) * sizeof(*regexes));
264 + for (i = 0; i < LEN(assocs) - 1; i++)
265 + regexes[i] = assocs[i].regex;
266 + super = mksuper(regexes, LEN(assocs) - 1);
267 + free(regexes);
268 +
269 + return super;
270 +}
271 +
272 +/* Returns 1 if path is known and 0 otherwise */
227273 int
274 +known(char *path)
275 +{
276 + int fd, r;
277 + char *super;
278 + regex_t regex;
279 + struct stat sb;
280 +
281 + /* Get path info */
282 + fd = open(path, O_RDONLY | O_NONBLOCK);
283 + if (fd == -1) {
284 + return 0;
285 + }
286 + r = fstat(fd, &sb);
287 + if (r == -1) {
288 + close(fd);
289 + return 0;
290 + }
291 + close(fd);
292 +
293 + /* Show directories */
294 + if (S_ISDIR(sb.st_mode))
295 + return 1;
296 + /* And regular files */
297 + if (!S_ISREG(sb.st_mode))
298 + return 0;
299 +
300 + super = mksuperassoc();
301 + r = regcomp(&regex, super, REG_NOSUB | REG_EXTENDED);
302 + if (r != 0)
303 + return 1;
304 + if (regexec(&regex, path, 0, NULL, 0) != REG_NOMATCH)
305 + return 1;
306 +
307 + return 0;
308 +}
309 +
310 +int
228311 entrycmp(const void *va, const void *vb)
229312 {
230313 const struct entry *a, *b;
231314
@@ -436,15 +519,19 @@
436519 || strcmp(dp->d_name, "..") == 0)
437520 continue;
438521 if (filter(re, dp->d_name) == 0)
439522 continue;
440- *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
441- (*dents)[n].name = xstrdup(dp->d_name);
523 + newpath = makepath(path, dp->d_name);
442524 /* Get mode flags */
443- newpath = makepath(path, dp->d_name);
444525 r = lstat(newpath, &sb);
445526 if (r == -1)
446527 printerr(1, "lstat");
528 + if (know && !known(newpath)) {
529 + free(newpath);
530 + continue;
531 + }
532 + *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
533 + (*dents)[n].name = xstrdup(dp->d_name);
447534 (*dents)[n].mode = sb.st_mode;
448535 n++;
449536 }
450537
@@ -739,8 +826,13 @@
739826 free(filter);
740827 filter = xstrdup(ifilter); /* Reset filter */
741828 DPRINTF_S(path);
742829 goto out;
830 + case SEL_KNOW:
831 + know = !know;
832 + /* Save current */
833 + oldpath = makepath(path, dents[cur].name);
834 + goto out;
743835 }
744836 }
745837
746838 out:

Built with git-ssb-web