Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use stat for sorting by size like we do with mtime #695

Closed
hidroto opened this issue Feb 15, 2023 · 1 comment
Closed

use stat for sorting by size like we do with mtime #695

hidroto opened this issue Feb 15, 2023 · 1 comment

Comments

@hidroto
Copy link

hidroto commented Feb 15, 2023

I think we should change sorting by size to not need a preload of the filelist. there is no need to preload the filelist when we sort by mtime and we can use stat to get the size of the images much like we get the mtime. like so

diff --git a/src/filelist.c b/src/filelist.c                                  
index 8da6e58..51331ce 100644                                                
--- a/src/filelist.c                
+++ b/src/filelist.c   
@@ -466,7 +466,20 @@ int feh_cmp_pixels(void *file1, void *file2)                                              
                                                                                
 int feh_cmp_size(void *file1, void *file2)                                     
 { 
-       return((FEH_FILE(file1)->info->size - FEH_FILE(file2)->info->size));
+       struct stat s1, s2;
+       
+       if (stat(FEH_FILE(file1)->filename, &s1)) {
+               feh_print_stat_error(FEH_FILE(file1)->filename);
+               return(-1);
+       }
+   
+       if (stat(FEH_FILE(file2)->filename, &s2)) {
+               feh_print_stat_error(FEH_FILE(file2)->filename);
+               return(-1);
+       }
+        
+       /* gib_list_sort is not stable, so explicitly return 0 as -1 */
+       return(s1.st_size < s2.st_size ? -1 : 1);
 }       
    
 int feh_cmp_format(void *file1, void *file2)
diff --git a/src/filelist.h b/src/filelist.h
index 7f263dc..b4b8f98 100644
--- a/src/filelist.h
+++ b/src/filelist.h
@@ -71,11 +71,11 @@ enum sort_type {
        SORT_NAME,
        SORT_FILENAME,
        SORT_DIRNAME,
-       SORT_MTIME,
+       SORT_SIZE, 
+       SORT_MTIME, // everything after SORT_MTIME requires a preload of the filelist
        SORT_WIDTH, 
        SORT_HEIGHT,
        SORT_PIXELS,
-       SORT_SIZE, 
        SORT_FORMAT
 };

Is there a reason we are not doing this. sorting by time does not modify the FEH_FILE struct and I think that this should sort the same.

@hidroto
Copy link
Author

hidroto commented Feb 15, 2023

sorry somehow submitted twice

@hidroto hidroto closed this as completed Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant