Merge pull request #85 from whatevsz/wip-cycle

Add -x switch to exit imv when reaching end of file list.
This commit is contained in:
Harry Jeffery 2016-04-03 14:16:35 +01:00
commit 7febfc4676
5 changed files with 40 additions and 12 deletions

View file

@ -60,6 +60,9 @@ To cycle through a folder of pictures, showing each one for 10 seconds:
imv -t 10 ~/Pictures/London imv -t 10 ~/Pictures/London
The `-x` switch can be used to exit imv after the last picture instead of
cycling through the list.
Installation Installation
------------ ------------

View file

@ -6,7 +6,7 @@
.Nd view images .Nd view images
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl adfhlrSsu .Op Fl adfhlrSsux
.Op Fl b Ar color .Op Fl b Ar color
.Op Fl e Ar font:size .Op Fl e Ar font:size
.Op Fl n Ar position .Op Fl n Ar position
@ -71,6 +71,8 @@ Setting this to zero disables slideshow mode, which is the default.
.It Fl u .It Fl u
Use nearest neighbour resampling. Use nearest neighbour resampling.
Recommended for pixel art. Recommended for pixel art.
.It Fl x
Exit when reaching end of file list.
.El .El
.Ss Reading from standard input .Ss Reading from standard input
When run with argument When run with argument

View file

@ -55,6 +55,7 @@ struct {
int solid_bg; int solid_bg;
int list; int list;
unsigned long delay; unsigned long delay;
int cycle;
unsigned char bg_r; unsigned char bg_r;
unsigned char bg_g; unsigned char bg_g;
unsigned char bg_b; unsigned char bg_b;
@ -71,6 +72,7 @@ struct {
.solid_bg = 1, .solid_bg = 1,
.list = 0, .list = 0,
.delay = 0, .delay = 0,
.cycle = 1,
.bg_r = 0, .bg_r = 0,
.bg_g = 0, .bg_g = 0,
.bg_b = 0, .bg_b = 0,
@ -105,7 +107,7 @@ static void parse_args(int argc, char** argv)
char *argp, o; char *argp, o;
while((o = getopt(argc, argv, "firasSudhln:b:e:t:")) != -1) { while((o = getopt(argc, argv, "firasSudxhln:b:e:t:")) != -1) {
switch(o) { switch(o) {
case 'f': g_options.fullscreen = 1; break; case 'f': g_options.fullscreen = 1; break;
case 'i': case 'i':
@ -118,6 +120,7 @@ static void parse_args(int argc, char** argv)
case 'S': g_options.scaling = FULL; break; case 'S': g_options.scaling = FULL; break;
case 'u': g_options.nearest_neighbour = 1; break; case 'u': g_options.nearest_neighbour = 1; break;
case 'd': g_options.overlay = 1; break; case 'd': g_options.overlay = 1; break;
case 'x': g_options.cycle = 0; break;
case 'h': print_usage(); exit(0); break; case 'h': print_usage(); exit(0); break;
case 'l': g_options.list = 1; break; case 'l': g_options.list = 1; break;
case 'n': case 'n':
@ -345,13 +348,17 @@ int main(int argc, char** argv)
break; break;
case SDLK_LEFTBRACKET: case SDLK_LEFTBRACKET:
case SDLK_LEFT: case SDLK_LEFT:
imv_navigator_select_rel(&nav, -1); if(!imv_navigator_select_rel(&nav, -1, g_options.cycle)) {
quit = 1;
}
/* reset slideshow delay */ /* reset slideshow delay */
delay_msec = 0; delay_msec = 0;
break; break;
case SDLK_RIGHTBRACKET: case SDLK_RIGHTBRACKET:
case SDLK_RIGHT: case SDLK_RIGHT:
imv_navigator_select_rel(&nav, 1); if(!imv_navigator_select_rel(&nav, 1, g_options.cycle)) {
quit = 1;
}
/* reset slideshow delay */ /* reset slideshow delay */
delay_msec = 0; delay_msec = 0;
break; break;
@ -523,7 +530,9 @@ int main(int argc, char** argv)
delay_msec += dt; delay_msec += dt;
need_redraw = 1; need_redraw = 1;
if(delay_msec >= g_options.delay) { if(delay_msec >= g_options.delay) {
imv_navigator_select_rel(&nav, 1); if(!imv_navigator_select_rel(&nav, 1, g_options.cycle)) {
quit = 1;
}
delay_msec = 0; delay_msec = 0;
} }
} }

View file

@ -126,11 +126,12 @@ const char *imv_navigator_selection(struct imv_navigator *nav)
return nav->paths[nav->cur_path]; return nav->paths[nav->cur_path];
} }
void imv_navigator_select_rel(struct imv_navigator *nav, int direction) int imv_navigator_select_rel(struct imv_navigator *nav, int direction,
const int cycle)
{ {
int prev_path = nav->cur_path; int prev_path = nav->cur_path;
if(nav->num_paths == 0) { if(nav->num_paths == 0) {
return; return 0;
} }
if(direction > 1) { if(direction > 1) {
@ -138,17 +139,26 @@ void imv_navigator_select_rel(struct imv_navigator *nav, int direction)
} else if(direction < -1) { } else if(direction < -1) {
direction = -1; direction = -1;
} else if(direction == 0) { } else if(direction == 0) {
return; return 0;
} }
nav->cur_path += direction; nav->cur_path += direction;
if(nav->cur_path == nav->num_paths) { if(nav->cur_path == nav->num_paths) {
/* end of list reached */
if(!cycle) {
return 0;
}
nav->cur_path = 0; nav->cur_path = 0;
} else if(nav->cur_path < 0) { } else if(nav->cur_path < 0) {
/* going backwards at the beginning of the list */
if(!cycle) {
return 0;
}
nav->cur_path = nav->num_paths - 1; nav->cur_path = nav->num_paths - 1;
} }
nav->last_move_direction = direction; nav->last_move_direction = direction;
nav->changed = prev_path != nav->cur_path; nav->changed = prev_path != nav->cur_path;
return 1;
} }
void imv_navigator_remove(struct imv_navigator *nav, const char *path) void imv_navigator_remove(struct imv_navigator *nav, const char *path)
@ -175,8 +185,9 @@ void imv_navigator_remove(struct imv_navigator *nav, const char *path)
if(nav->cur_path == removed) { if(nav->cur_path == removed) {
/* We just removed the current path */ /* We just removed the current path */
if(nav->last_move_direction < 0) { if(nav->last_move_direction < 0) {
/* Move left */ /* Move left
imv_navigator_select_rel(nav, -1); * the cycle parameter does not matter here */
imv_navigator_select_rel(nav, -1, 0);
} else { } else {
/* Try to stay where we are, unless we ran out of room */ /* Try to stay where we are, unless we ran out of room */
if(nav->cur_path == nav->num_paths) { if(nav->cur_path == nav->num_paths) {

View file

@ -47,8 +47,11 @@ void imv_navigator_add(struct imv_navigator *nav, const char *path,
* guaranteed to be valid until the next call to an imv_navigator method. */ * guaranteed to be valid until the next call to an imv_navigator method. */
const char *imv_navigator_selection(struct imv_navigator *nav); const char *imv_navigator_selection(struct imv_navigator *nav);
/* Change the currently selected path. dir = -1 for previous, 1 for next */ /* Change the currently selected path. dir = -1 for previous, 1 for next
void imv_navigator_select_rel(struct imv_navigator *nav, int dir); * cycle = 1 to go to the beginning of the file list if end is reached
* cycle = 0 to return error instead
* Returns 1 on success, 0 otherwise */
int imv_navigator_select_rel(struct imv_navigator *nav, int dir, int cycle);
/* Removes the given path. The current selection is updated if necessary, /* Removes the given path. The current selection is updated if necessary,
* based on the last direction the selection moved. */ * based on the last direction the selection moved. */