From: John Tsiombikas Date: Sun, 22 Jan 2023 20:28:50 +0000 (+0200) Subject: quick & dirty file list sorting X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=commitdiff_plain;h=85b4b3f42fd47fbc9baff6f0aa988037f1b73bc1;p=oftp quick & dirty file list sorting --- 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)