wip display window
authorLaurent Mazet <mazet@softndesign.org>
Sun, 29 Dec 2024 20:31:47 +0000 (21:31 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 29 Dec 2024 20:31:47 +0000 (21:31 +0100)
display.c
display.h
fm.c
function.c
function.h
type.h

index ec43d04655e9c2f2d39ce31d17a7409a83d2193b..194e1bd4b277eca23bc31255e525d8f7338f9754 100644 (file)
--- a/display.c
+++ b/display.c
@@ -117,14 +117,15 @@ int helpwindow (char *msg, int xoffset, int yoffset)
     return j;
 }
 
-void displaywindow (list_t *list, window_t *win, int index)
+void displaywindow (window_t *win, list_t *list, int index)
 {
     int i;
-    int n = (list->nb + win->nbcols - 1) / win->nbcols;
+    int nbcols = win->xsize / list->width;
+    int n = (list->nb + nbcols - 1) / nbcols;
     for (i = 0; i < list->nb; i++) {
         int j = (i % n) - index;
-        if ((j >= 0) && (j < win->nbrows)) {
-            mvaddstr (win->yoffset + j, win->xoffset + (i % n) * win->width, (list->tab + i)->name);
+        if ((j >= 0) && (j < win->ysize)) {
+            mvaddstr (win->yoffset + j, win->xoffset + (i % n) * list->width, (list->tab + i)->name);
         }
     }
 }
index 07f71020a3d48a2f552a19f7bba654eba04e930c..2f33b50f3b3e0219906555588815125f2fdd1688 100644 (file)
--- a/display.h
+++ b/display.h
@@ -1,11 +1,15 @@
 #ifndef __DISPLAY_H__
 #define __DISPLAY_H__
 
+#include "type.h"
+
 #define KEY_ESC 0x1b
 #define KEY_DELETE 0x014a
 
 int helpwindow (char *msg, int xoffset, int yoffset);
 
+void displaywindow (window_t *win, list_t *list, int index);
+
 char *getwindow (int length, int xoffset, int yoffset);
 
 void msgwindow (char *msg, int xoffset, int yoffset, int length);
diff --git a/fm.c b/fm.c
index 25f737801cacdf4dbc394558d7d63748b3fd1a3a..fc1301f5d8832494ac39b15e6d4eb551e8ce5a8c 100644 (file)
--- a/fm.c
+++ b/fm.c
@@ -7,6 +7,7 @@
 #include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "debug.h"
 #include "display.h"
@@ -16,6 +17,9 @@
 char *progname = NULL;
 char *version = "0.1";
 
+char *dirname = NULL;
+int xoffset = 1;
+int yoffset = 1;
 int wide = 0;
 
 char *help =
@@ -29,7 +33,8 @@ char *help =
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-h] [-w]\n", progname);
+    fprintf (fd, "usage: %s [-d dir] [-h] [-w]\n", progname);
+    fprintf (fd, " -d: dir name (%s)\n", (dirname) ? dirname : ".");
     fprintf (fd, " -h: help message\n");
     fprintf (fd, " -w: wide board (%d)\n", wide);
     fprintf (fd, "%s version %s\n", progname, version);
@@ -40,7 +45,6 @@ int usage (int ret)
 /* main function */
 int main (int argc, char *argv[])
 {
-
     /* get basename */
     char *pt = progname = argv[0];
     while (*pt) {
@@ -59,6 +63,17 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
+        case 'd':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
+                return usage (1);
+            }
+            if (dirname == NULL) {
+                free (dirname);
+            }
+            dirname = strdup (arg);
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -75,8 +90,11 @@ int main (int argc, char *argv[])
             return usage (c != 'h');
         }
     }
+    if (dirname == NULL) {
+        dirname = strdup (".");
+    }
 
-    /* init curses window */
+    /* init curses */
     initscr ();
     noecho ();
     cbreak ();
@@ -85,39 +103,55 @@ int main (int argc, char *argv[])
     curs_set (0);
     start_color ();
 
+    /* init windows */
+    window_t *windir = allocwindow ();
+    windir->xoffset = xoffset;
+    windir->yoffset = yoffset;
+    windir->xsize = COLS - 2 * xoffset;
+    windir->ysize = LINES - 2 * yoffset;
+
+    list_t *list  = NULL;
+    int index = 0;
+
     /* event loop */
     int stop = 0;
     while (!stop) {
-        char *ptr = NULL;
 
-        //boardwindow (current);
+        if (list == NULL) {
+            list = exploredir (dirname);
+        }
+
+        displaywindow (windir, list, index);
 
         int ch = getch ();
         switch (ch) {
         case KEY_UP:
         case 'i':
+            index--;
             break;
         case KEY_LEFT:
         case 'j':
             break;
         case KEY_DOWN:
         case 'k':
+            index++;
             break;
         case KEY_RIGHT:
         case 'l':
             break;
         case KEY_ESC:
         case 'q':
-            //if (askwindow (" Restart (Y/N) ", max (board->xoffset + (board->xsize - savelen) / 2, 1), board->yoffset + (board->ysize - 1) / 2, "Yy", "Nn") == 1) {
+            if (askwindow (" Restart (Y/N) ", max (windir->xoffset + (windir->xsize - 15) / 2, 1), windir->yoffset + (windir->ysize - 1) / 2, "Yy", "Nn") == 1) {
                 stop = 1;
-            //}
+            }
             break;
         }
     }
 
     endwin ();
 
-    //freeboard (board);
+    freelist (list);
+    freewindow (windir);
 
     return 0;
 }
index 4a1dfd34fbfaa870ba5d8cfb22f6063ee2617dec..287a0ed410cbaf3fc4daac5e7eb89a99789fcbf8 100644 (file)
@@ -28,7 +28,7 @@ int strmaxlen (char *str, char ch)
 
 list_t *alloclist (void)
 {
-    list_t *list = (list_t *) malloc (sizeof (list_t));
+    list_t *list = (list_t *) calloc (1, sizeof (list_t));
     CHECKALLOC (list);
     list->nb = 0;
     list->tab = NULL;
@@ -41,10 +41,10 @@ list_t *_addelement (list_t *list, char *dirname, char *filename, type_t type)
     size_t size = 0;
 
     if ((type == type_unkn_e) || (type == type_reg_e)) {
-        char *name = (char *) calloc (1, strlen (dirname) + strlen (filename) + 1 + 1);
+        char *name = (char *) calloc (strlen (dirname) + strlen (filename) + 1 + 1, 1);
         CHECKALLOC (name);
         strcat (strcat(strcpy (name, dirname), SEPARATOR), filename);
-        
+
         struct stat sb;
         if (lstat (name, &sb) == -1) {
             VERBOSE (ERROR, fprintf (stderr, "can't stat on file '%s'\n", name));
@@ -76,7 +76,7 @@ list_t *_addelement (list_t *list, char *dirname, char *filename, type_t type)
         }
 
         if (type == type_reg_e) {
-            size = sb.st_size;    
+            size = sb.st_size;
         }
 
         free (name);
@@ -90,10 +90,15 @@ list_t *_addelement (list_t *list, char *dirname, char *filename, type_t type)
     elem->size = size;
     elem->type = type;
 
+    int width = strlen (filename);
+    if (width > list->width) {
+        list->width = width;
+    }
+
     return list;
 }
 
-list_t *exporedir (char *dirname)
+list_t *exploredir (char *dirname)
 {
     DIR *dir = opendir (dirname);
     if (dir == NULL) {
@@ -149,7 +154,7 @@ void freelist (list_t *list)
 
 window_t *allocwindow (void)
 {
-    window_t *win = (window_t *) calloc  (sizeof (window_t), 1);
+    window_t *win = (window_t *) calloc  (1, sizeof (window_t));
     CHECKALLOC (win);
     return win;
 }
index c3165d34eff809e13a49bbba38b9a5dc4d13b9fe..6e7580059f2df216b04142acf712b6f83692d0f1 100644 (file)
@@ -19,7 +19,7 @@ int strmaxlen (char *str, char ch);
 
 list_t *alloclist (void);
 
-list_t *exporedir (char *dirname);
+list_t *exploredir (char *dirname);
 
 void freelist (list_t *list);
 
diff --git a/type.h b/type.h
index 57060dca8e54547fb928f545cabb72a326de7df3..921dacc72351611d46352baef1865731fa789423 100644 (file)
--- a/type.h
+++ b/type.h
@@ -24,6 +24,7 @@ typedef struct {
 typedef struct {
     int nb;
     elem_t *tab;
+    int width;
 } list_t;
 
 typedef struct {
@@ -31,9 +32,6 @@ typedef struct {
     int yoffset;
     int xsize;
     int ysize;
-    int nbcols;
-    int nbrows;
-    int width;
 } window_t;
 
 #endif /* __TYPE_H__ */