From 8baec61e014f2b284e2a34df06d043399722bf2f Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 3 Jan 2025 23:42:57 +0100 Subject: [PATCH] update help message --- display.c | 29 +++++++++++++++++++++++------ fm.c | 28 +++++++++++++++++++++++++++- function.c | 15 ++++++++++++--- function.h | 2 ++ type.h | 1 + 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/display.c b/display.c index f0d01a9..731c752 100644 --- a/display.c +++ b/display.c @@ -67,7 +67,12 @@ int _helpwindow (char *msg, int xoffset, int yoffset, int length) int i = 0; int j = 0; while ((msg) && (*msg != '\0')) { - if ((*msg == '\n') || (i == length)) { + if (*msg == '\n') { + if (i <= length) { + mvaddch (yoffset + j, xoffset + i, ' '); + i++; + continue; + } i = 0; j++; } @@ -80,13 +85,16 @@ int _helpwindow (char *msg, int xoffset, int yoffset, int length) return j; } -void _displaytitle (char *title, int xoffset, int yoffset) +void _displaytitle (char *title, int xoffset, int yoffset, int length) { int i; - for (i = 0; title[i] != '\0'; i++) { - mvaddch (yoffset, xoffset + i, title[i]); + for (i = 0; i < length; i++) { + mvaddch (yoffset, xoffset + i, ' '); mvaddch (yoffset + 1, xoffset + i, ACS_HLINE); } + for (i = 0; title[i] != '\0'; i++) { + mvaddch (yoffset, xoffset + i + (length - strlen (title)) / 2, title[i]); + } } void _dobound (int xsize, int ysize, int xoffset, int yoffset) @@ -109,10 +117,16 @@ void _dobound (int xsize, int ysize, int xoffset, int yoffset) int helpwindow (char *msg, int xoffset, int yoffset) { - _displaytitle ("Help message", xoffset, yoffset); + char *title = "Help message"; int length = strmaxlen (msg, '\n'); + if (length < (int)strlen (title)) { + length = strlen (title); + } + set_color (byellow); + _displaytitle (title, xoffset, yoffset, length); int j = 2; j += _helpwindow (msg, xoffset, yoffset + j, length); + _dobound (length, j, xoffset, yoffset); return j; } @@ -181,6 +195,9 @@ void displaywindow (window_t *win, list_t *list, int page, int *index_x, int *in } mvaddstr (win->yoffset + (j % n) - page, win->xoffset + (j / n) * win->width, elem->name); set_color (white); + if (j == index) { + win->index = i; + } } } @@ -273,8 +290,8 @@ void msgwindow (char *msg, int xoffset, int yoffset, int length) { set_color (black); _dobound ((length > 0) ? length : (int)strlen (msg), 1, xoffset, yoffset); - set_color (white); mvaddstr (yoffset, xoffset + ((length > 0) ? (length - (int)strlen (msg)) / 2 : 0), msg); + set_color (white); } int askwindow (char *msg, int xoffset, int yoffset, char *ok, char *ko) diff --git a/fm.c b/fm.c index dcaeb3b..17ba23b 100644 --- a/fm.c +++ b/fm.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "debug.h" #include "display.h" @@ -28,6 +29,11 @@ int xoffset = 1; int yoffset = 2; char *help = + "ret Enter directory\n" + "tab Change pane\n" + " Page up\n" + " Page down\n" + " Show help\n" " Move up\n" " Move left\n" " Move down\n" @@ -153,6 +159,20 @@ int main (int argc, char *argv[]) int ch = getch (); switch (ch) { + case '\n': + case '\r': + if ((list[pane]->tab + windir[pane]->index)->type == type_dir_e) { + elem_t *elem = list[pane]->tab + windir[pane]->index; + char *_dirname = newfilename (dirname[pane], elem->name); + free (dirname[pane]); + dirname[pane] = _dirname; + index_x[pane] = 0; + index_y[pane] = 0; + freelist (list[pane]); + list[pane] = NULL; + page[pane] = 0; + } + break; case KEY_PPAGE: case KEY_SLEFT: case 'I': @@ -167,6 +187,12 @@ int main (int argc, char *argv[]) page[pane]++; } break; + case 'h': + helpwindow (help, (COLS - strmaxlen (help, '\n')) / 2, 3 * yoffset); + while (getch () == ERR) { + usleep (200 * 1000); + } + break; case KEY_UP: case 'i': if (index_y[pane]> 0) { @@ -213,7 +239,7 @@ int main (int argc, char *argv[]) for (i = 0; i < MAXPANES; i++) { free (dirname[i]); - freelist (list[pane]); + freelist (list[i]); freewindow (windir[i]); } diff --git a/function.c b/function.c index 09561bd..8376a9c 100644 --- a/function.c +++ b/function.c @@ -26,6 +26,14 @@ int strmaxlen (char *str, char ch) return len; } +char *newfilename (char *dirname, char *filename) +{ + char *name = (char *) calloc (strlen (dirname) + strlen (SEPARATOR) + strlen (filename) + 1, 1); + CHECKALLOC (name); + strcat (strcat(strcpy (name, dirname), SEPARATOR), filename); + return name; +} + list_t *alloclist (void) { list_t *list = (list_t *) calloc (1, sizeof (list_t)); @@ -40,9 +48,7 @@ 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 (strlen (dirname) + strlen (filename) + strlen (SEPARATOR) + 1, 1); - CHECKALLOC (name); - strcat (strcat(strcpy (name, dirname), SEPARATOR), filename); + char *name = newfilename (dirname, filename); struct stat sb; if (lstat (name, &sb) == -1) { @@ -118,6 +124,9 @@ list_t *exploredir (char *dirname) struct dirent *dp = NULL; while ((dp = readdir(dir)) != NULL) { + if (strcmp (dp->d_name, ".") == 0) { + continue; + } type_t type = type_unkn_e; switch (dp->d_type) { case DT_BLK: diff --git a/function.h b/function.h index 6e75800..fb4b46a 100644 --- a/function.h +++ b/function.h @@ -17,6 +17,8 @@ int strmaxlen (char *str, char ch); +char *newfilename (char *dirname, char *filename); + list_t *alloclist (void); list_t *exploredir (char *dirname); diff --git a/type.h b/type.h index f2658f6..af34ba3 100644 --- a/type.h +++ b/type.h @@ -29,6 +29,7 @@ typedef struct { typedef struct { int active; + int index; int nbcols; int width; int xoffset; -- 2.30.2