From 85b4b3f42fd47fbc9baff6f0aa988037f1b73bc1 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sun, 22 Jan 2023 22:28:50 +0200 Subject: [PATCH] quick & dirty file list sorting --- src/main.c | 13 ++++++++++--- src/tui_list.c | 5 ++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index d678219..fa7dbb1 100644 --- a/src/main.c +++ b/src/main.c @@ -103,8 +103,15 @@ static int cmpnames(const void *a, const void *b) { const char *sa = *(const char**)a; const char *sb = *(const char**)b; - infomsg("cmp(%s, %s)\n", sa, sb); - return strcmp(sa, sb); + int isdir_a, isdir_b; + + isdir_a = sa[strlen(sa) - 1] == '/'; + isdir_b = sb[strlen(sb) - 1] == '/'; + + if(isdir_a == isdir_b) { + return strcmp(sa, sb); + } + return isdir_a ? -1 : 1; } void updateui(void) @@ -132,7 +139,7 @@ void updateui(void) ent = ent->next; } - //tui_sort_list(uilist, cmpnames); + tui_sort_list(uilist, cmpnames); tui_list_select(uilist, 0); ftp->modified &= ~FTP_MOD_REMDIR; diff --git a/src/tui_list.c b/src/tui_list.c index 728522e..b57e5ff 100644 --- a/src/tui_list.c +++ b/src/tui_list.c @@ -178,13 +178,16 @@ int tui_list_sel_end(struct tui_widget *w) void tui_sort_list(struct tui_widget *w, int (*cmpfunc)(const void*, const void*)) { + int nelem; struct tui_list *wl = (struct tui_list*)w; assert(wl->type == TUI_LIST); if(!cmpfunc) { cmpfunc = (int (*)(const void*, const void*))strcmp; } - qsort(wl->entries, darr_size(wl->entries), sizeof *wl->entries, cmpfunc); + + nelem = darr_size(wl->entries); + qsort(wl->entries, nelem, sizeof *wl->entries, cmpfunc); } static void draw_list(struct tui_widget *w, void *cls) -- 1.7.10.4