aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2015-11-20 11:43:38 +0000
committerHarry Jeffery <harry@exec64.co.uk>2015-11-20 11:43:38 +0000
commit81091690ba8794b38536b2f171ae56cf714295d7 (patch)
tree440bc6bd5e52d631662fe945981cec6c3ac380d1
parentf0448375624d7d81808995ebb3a2f9352c21dfc4 (diff)
downloadimv-81091690ba8794b38536b2f171ae56cf714295d7.tar.gz
Add support for -n PATH starting specifier
Fixes #36
-rw-r--r--doc/imv.16
-rw-r--r--src/main.c28
-rw-r--r--src/navigator.c21
-rw-r--r--src/navigator.h1
4 files changed, 39 insertions, 17 deletions
diff --git a/doc/imv.1 b/doc/imv.1
index 3965c8b..f8cdf74 100644
--- a/doc/imv.1
+++ b/doc/imv.1
@@ -3,7 +3,7 @@
imv \- image viewer
.SH SYNOPSIS
.nf
-\fBimv\fP [-rfaudh] [-n NUM] [-b COLOR] [-e FONT:SIZE] [images...]
+\fBimv\fP [-rfaudh] [-n <NUM|PATH>] [-b COLOR] [-e FONT:SIZE] [images...]
.fi
.sp
.SH DESCRIPTION
@@ -34,8 +34,8 @@ Show overlay.
Print help.
.SH OPTIONS
.TP
-.BI "-n " NUM
-Start at picture number NUM.
+.BI "-n " <NUM|PATH>
+Start at image number NUM, or the image with the path PATH.
.TP
.BI "-b " BG
Set the background color. Either 'checks' or a hex color value.
diff --git a/src/main.c b/src/main.c
index ed17366..5a9c54c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -38,21 +38,21 @@ struct {
int stdin;
int recursive;
int actual;
- int start_at;
int nearest_neighbour;
int solid_bg;
unsigned char bg_r;
unsigned char bg_g;
unsigned char bg_b;
int overlay;
+ const char *start_at;
const char *font;
-} g_options = {0,0,0,0,0,0,1,0,0,0,0,"FreeMono:24"};
+} g_options = {0,0,0,0,0,1,0,0,0,0,NULL,"FreeMono:24"};
void print_usage(const char* name)
{
fprintf(stdout,
"imv %s\n"
- "Usage: %s [-rfaudh] [-n NUM] [-b BG] [-e FONT:SIZE] [-] [images...]\n"
+ "Usage: %s [-rfaudh] [-n <NUM|PATH>] [-b BG] [-e FONT:SIZE] [-] [images...]\n"
"\n"
"Flags:\n"
" -: Read paths from stdin. One path per line.\n"
@@ -64,7 +64,7 @@ void print_usage(const char* name)
" -h: Print this help\n"
"\n"
"Options:\n"
- " -n NUM: Start at picture number NUM.\n"
+ " -n <NUM|PATH>: Start at picture number NUM, or with the path PATH.\n"
" -b BG: Set the background. Either 'checks' or a hex color value.\n"
" -e FONT:SIZE: Set the font used for the overlay. Defaults to FreeMono:24"
"\n"
@@ -111,8 +111,6 @@ void parse_args(int argc, char** argv)
const char* name = argv[0];
char o;
- char* end;
- int n;
while((o = getopt(argc, argv, "firaudhn:b:e:")) != -1) {
switch(o) {
@@ -127,12 +125,7 @@ void parse_args(int argc, char** argv)
case 'd': g_options.overlay = 1; break;
case 'h': print_usage(name); exit(0); break;
case 'n':
- n = strtol(optarg,&end,0);
- if(*end != '\0' || n <= 0) {
- fprintf(stderr, "Warning: wrong value for '-n'.\n");
- } else {
- g_options.start_at = n - 1;
- }
+ g_options.start_at = optarg;
break;
case 'b':
if(strcmp("checks", optarg) == 0) {
@@ -240,6 +233,15 @@ int main(int argc, char** argv)
exit(1);
}
+ /* go to the chosen starting image if possible */
+ if(g_options.start_at) {
+ int start_index = imv_navigator_find_path(&nav, g_options.start_at);
+ if(start_index < 0) {
+ start_index = strtol(g_options.start_at,NULL,10) - 1;
+ }
+ imv_navigator_set_path(&nav, start_index);
+ }
+
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "SDL Failed to Init: %s\n", SDL_GetError());
exit(1);
@@ -289,8 +291,6 @@ int main(int argc, char** argv)
double last_time = SDL_GetTicks() / 1000.0;
- imv_navigator_set_path(&nav, g_options.start_at);
-
int quit = 0;
while(!quit) {
SDL_Event e;
diff --git a/src/navigator.c b/src/navigator.c
index d719791..7ece6a0 100644
--- a/src/navigator.c
+++ b/src/navigator.c
@@ -185,6 +185,27 @@ void imv_navigator_set_path(struct imv_navigator *nav, const int path)
nav->changed = prev_path != nav->cur_path;
}
+int imv_navigator_find_path(struct imv_navigator *nav, const char *path)
+{
+ /* first try to match the exact path */
+ for(int i = 0; i < nav->num_paths; ++i) {
+ if(strcmp(path, nav->paths[i]) == 0) {
+ return i;
+ }
+ }
+
+ /* no exact matches, try the final portion of the path */
+ for(int i = 0; i < nav->num_paths; ++i) {
+ char *last_sep = strrchr(nav->paths[i], '/');
+ if(last_sep && strcmp(last_sep+1, path) == 0) {
+ return i;
+ }
+ }
+
+ /* no matches at all, give up */
+ return -1;
+}
+
int imv_navigator_has_changed(struct imv_navigator *nav)
{
if(nav->changed) {
diff --git a/src/navigator.h b/src/navigator.h
index c06bcce..39db489 100644
--- a/src/navigator.h
+++ b/src/navigator.h
@@ -38,6 +38,7 @@ void imv_navigator_next_path(struct imv_navigator *nav);
void imv_navigator_prev_path(struct imv_navigator *nav);
void imv_navigator_remove_current_path(struct imv_navigator *nav);
void imv_navigator_set_path(struct imv_navigator *nav, const int path);
+int imv_navigator_find_path(struct imv_navigator *nav, const char *path);
int imv_navigator_has_changed(struct imv_navigator *nav);