Skip to content

Commit

Permalink
Add support for reverse image list order
Browse files Browse the repository at this point in the history
Adds new order mode: "reverse".

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Sep 11, 2024
1 parent 36949f0 commit a586885
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions extra/swayimg.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Set order of the image list:
.nf
\fInone\fR: unsorted, order is system depended;
\fIalpha\fR: sorted alphabetically (default);
\fIreverse\fR: reversed alphabetically;
\fIrandom\fR: randomize list.
.\" ----------------------------------------------------------------------------
.IP "\fB\-s\fR, \fB\-\-scale\fR=\fIMODE\fR"
Expand Down
2 changes: 1 addition & 1 deletion extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ shadow = #000000ff
# Image list configuration
################################################################################
[list]
# Default order (none/alpha/random)
# Default order (none/alpha/reverse/random)
order = alpha
# Looping list of images (yes/no)
loop = yes
Expand Down
1 change: 1 addition & 0 deletions extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Set order of the image list:
.nf
\fInone\fR: unsorted, order is system depended;
\fIalpha\fR: sorted alphabetically (default);
\fIreverse\fR: reversed alphabetically;
\fIrandom\fR: randomize list.
.\" ----------------------------------------------------------------------------
.IP "\fBloop\fR\fR = \fI[yes|no]\fR"
Expand Down
2 changes: 1 addition & 1 deletion extra/zsh.completion
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
_arguments \
'(-g --gallery)'{-g,--gallery}'[start in gallery mode]' \
'(-r --recursive)'{-r,--recursive}'[read directories recursively]' \
'(-o --order)'{-o,--order=}'[set sort order for image list]:order:(none alpha random)' \
'(-o --order)'{-o,--order=}'[set sort order for image list]:order:(none alpha reverse random)' \
'(-s --scale=SCALE)'{-s,--scale=}'[set initial image scale]:scale:(optimal width height fit fill real)' \
'(-l --slideshow)'{-l,--slideshow}'[activate slideshow mode on startup]' \
'(-f --fullscreen)'{-f,--fullscreen}'[show image in full screen mode]' \
Expand Down
32 changes: 26 additions & 6 deletions src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static struct image_list ctx;
static const char* order_names[] = {
[order_none] = "none",
[order_alpha] = "alpha",
[order_reverse] = "reverse",
[order_random] = "random",
};

Expand Down Expand Up @@ -188,11 +189,20 @@ static size_t next_dir(size_t start, bool forward)
* Compare sources callback for `qsort`.
* @return negative if a < b, positive if a > b, 0 otherwise
*/
static int compare_sources(const void* a, const void* b)
static int compare_alpha(const void* a, const void* b)
{
return strcoll(*(const char**)a, *(const char**)b);
}

/**
* Compare sources callback for `qsort`.
* @return negative if a > b, positive if a < b, 0 otherwise
*/
static int compare_reverse(const void* a, const void* b)
{
return -strcoll(*(const char**)a, *(const char**)b);
}

/**
* Shuffle the image list.
*/
Expand Down Expand Up @@ -284,11 +294,21 @@ size_t image_list_init(struct config* cfg, const char** sources, size_t num)
}

if (ctx.size != 0) {
// sort or shuffle
if (ctx.order == order_alpha) {
qsort(ctx.sources, ctx.size, sizeof(*ctx.sources), compare_sources);
} else if (ctx.order == order_random) {
shuffle_list();
// sort list
switch (ctx.order) {
case order_none:
break;
case order_alpha:
qsort(ctx.sources, ctx.size, sizeof(*ctx.sources),
compare_alpha);
break;
case order_reverse:
qsort(ctx.sources, ctx.size, sizeof(*ctx.sources),
compare_reverse);
break;
case order_random:
shuffle_list();
break;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/imagelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

/** Order of file list. */
enum list_order {
order_none, ///< Unsorted (system depended)
order_alpha, ///< Alphanumeric sort
order_random ///< Random order
order_none, ///< Unsorted (system depended)
order_alpha, ///< Alphanumeric sort
order_reverse, ///< Reversed alphanumeric sort
order_random ///< Random order
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static const struct cmdarg arguments[] = {
APP_CFG_SECTION, APP_CFG_MODE, APP_MODE_GALLERY },
{ 'r', "recursive", NULL, "read directories recursively",
IMGLIST_SECTION, IMGLIST_RECURSIVE, "yes" },
{ 'o', "order", "ORDER", "set sort order for image list: none/[alpha]/random",
{ 'o', "order", "ORDER", "set sort order for image list: none/[alpha]/reverse/random",
IMGLIST_SECTION, IMGLIST_ORDER, NULL },
{ 's', "scale", "SCALE", "set initial image scale: [optimal]/fit/width/height/fill/real",
VIEWER_SECTION, VIEWER_SCALE, NULL },
Expand Down

0 comments on commit a586885

Please sign in to comment.