]> git.mdlowis.com Git - projs/tide.git/commitdiff
added ability to read directories on file load. This replaces the hack for send ls...
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 24 Feb 2020 17:51:59 +0000 (12:51 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 24 Feb 2020 17:51:59 +0000 (12:51 -0500)
src/fetch.c
src/lib/buf.c
src/lib/gapbuf.c

index 12285b7351924c9249b61a0c7ab6b44a64e3a6e5..3f09d0a6dcb460cd6ae10937e2ab676d9f289604 100644 (file)
@@ -56,9 +56,9 @@ static Rule*** BuiltinRules = (Rule**[]){
         &(Rule){ LAUNCH, "man $M2 \"$M1\" | col -b | tide -", NULL },
         &(Rule){ COMPLETE, NULL, NULL }
     },
-    (Rule*[]){ /* If it's an existing directory, open it with system default */
+    (Rule*[]){ /* If it's an existing directory, open it tide */
         &(Rule){ ISDIR, "$data", NULL },
-        &(Rule){ LAUNCH, "cd $data && ls -ap | tide -", NULL },
+        &(Rule){ LAUNCH, "edit \"$data\"", NULL },
         &(Rule){ COMPLETE, NULL, NULL }
     },
     (Rule*[]){ /* look for files in /usr/include */
index 252dfa0e373968deea68ff2b7ab46651c5160fb6..3bae4fb4b405ee24b538a4c6261441b251e32985 100644 (file)
@@ -140,11 +140,6 @@ void buf_load(Buf* buf, char* path)
     require(buf != NULL);
     if (path)
     {
-        /* process the file path and address */
-        if (path[0] == '.' && path[1] == '/')
-        {
-            path += 2;
-        }
         buf->path = strdup(path);
         gapbuf_load(&buf->contents, buf->path);
 
index a5b915cce63344014c4f7114b637dac7baa54118..e66b0ff08c75f27dda9fffef992324441c2f293c 100644 (file)
@@ -7,6 +7,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#include <dirent.h>
+#include <vec.h>
 #include "config.h"
 
 size_t gapbuf_end(GapBuf* buf)
@@ -80,6 +82,44 @@ static void syncgap(GapBuf* buf, size_t off)
     }
 }
 
+static int cmpnames(const void* a, const void* b)
+{
+    char *sa = *((char**)a), *sb = *((char**)b);
+    return strcmp(sa, sb);
+}
+
+static void loaddir(GapBuf* buf, char* path, int fd)
+{
+    Sel sel = { 0 };
+    vec_t entries;
+    vec_init(&entries, sizeof(char*));
+    DIR* dir = fdopendir(fd);
+    struct dirent* entry = NULL;
+    struct stat attrs = {0};
+    while ( (entry = readdir(dir)) )
+    {
+        char* epath = strmcat(path, "/", entry->d_name, 0);
+        memset(&attrs, 0, sizeof(attrs));
+        stat(epath, &attrs);
+        char* ename = strmcat(entry->d_name, (S_ISDIR(attrs.st_mode) ? "/" : 0), 0);
+        vec_push_back(&entries, &ename);
+        free(epath);
+    }
+    vec_sort(&entries, cmpnames);
+    for (int i = 0; i < vec_size(&entries); i++)
+    {
+        char* name = *((char**)vec_at(&entries, i));
+        for (int i = 0; name[i]; i++)
+        {
+            gapbuf_putb(buf, name[i], &sel);
+        }
+        gapbuf_putb(buf, '\n', &sel);
+        free(name);
+    }
+    free(entries.elem_buffer);
+    chdir(path);
+}
+
 void gapbuf_init(GapBuf* buf)
 {
     require(buf != NULL);
@@ -125,9 +165,18 @@ void gapbuf_load(GapBuf* buf, char* path)
         buf->bufsize  = pagealign(sb.st_size);
         buf->bufstart = malloc(buf->bufsize);
         buf->bufend   = buf->bufstart + buf->bufsize;
-        buf->gapstart = buf->bufstart + sb.st_size;
+        buf->gapstart = buf->bufstart;
         buf->gapend   = buf->bufend;
-        (void)readfd(fd, buf->bufstart, sb.st_size);
+
+        if (S_ISDIR(sb.st_mode))
+        {
+            loaddir(buf, path, fd);
+        }
+        else
+        {
+            buf->gapstart += sb.st_size;
+            (void)readfd(fd, buf->bufstart, sb.st_size);
+        }
     }
     if (fd > 0)
     {