From 4b328da1f97f52515899aad13f50bfdbb54d044d Mon Sep 17 00:00:00 2001 From: Michal Koutenský Date: Fri, 13 Nov 2015 21:54:38 +0100 Subject: sxiv-like 'start at' option --- src/main.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index fc73455..9cd9563 100644 --- a/src/main.c +++ b/src/main.c @@ -31,13 +31,14 @@ struct { int stdin; int recursive; int actual; -} g_options = {0,0,0,0}; + long int start_at; +} g_options = {0,0,0,0,0}; void print_usage(const char* name) { fprintf(stdout, "imv %s\n" - "Usage: %s [-irfah] [images...]\n" + "Usage: %s [-irfah] [-n NUM] [images...]\n" "\n" "Flags:\n" " -i: Read paths from stdin. One path per line.\n" @@ -46,6 +47,9 @@ void print_usage(const char* name) " -a: Default to images' actual size\n" " -h: Print this help\n" "\n" + "Options:\n" + " -n NUM: Starts at picture number NUM.\n" + "\n" "Mouse:\n" " Click+Drag to Pan\n" " MouseWheel to Zoom\n" @@ -88,8 +92,10 @@ void parse_args(int argc, char** argv) const char* name = argv[0]; char o; + char* end; + long int n; - while((o = getopt(argc, argv, "firah")) != -1) { + while((o = getopt(argc, argv, "firahn:")) != -1) { switch(o) { case 'f': g_options.fullscreen = 1; break; case 'i': @@ -99,6 +105,14 @@ void parse_args(int argc, char** argv) case 'r': g_options.recursive = 1; break; case 'a': g_options.actual = 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; + } + break; case '?': fprintf(stderr, "Unknown argument '%c'. Aborting.\n", optopt); exit(1); @@ -189,6 +203,10 @@ int main(int argc, char** argv) double last_time = SDL_GetTicks() / 1000.0; + for(long int i = 0; i < g_options.start_at; ++i) { + imv_navigator_next_path(&nav); + } + int quit = 0; while(!quit) { SDL_Event e; -- cgit v1.2.3 From a9a1ccf0228cffdd65e06250ef0631b940baec72 Mon Sep 17 00:00:00 2001 From: Michal Koutenský Date: Fri, 13 Nov 2015 22:35:25 +0100 Subject: imv_navigator_set_path --- src/main.c | 14 ++++++-------- src/navigator.c | 10 ++++++++++ src/navigator.h | 1 + 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 9cd9563..44a4a6c 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,7 @@ struct { int stdin; int recursive; int actual; - long int start_at; + int start_at; } g_options = {0,0,0,0,0}; void print_usage(const char* name) @@ -48,7 +48,7 @@ void print_usage(const char* name) " -h: Print this help\n" "\n" "Options:\n" - " -n NUM: Starts at picture number NUM.\n" + " -n NUM: Start at picture number NUM.\n" "\n" "Mouse:\n" " Click+Drag to Pan\n" @@ -93,7 +93,7 @@ void parse_args(int argc, char** argv) const char* name = argv[0]; char o; char* end; - long int n; + int n; while((o = getopt(argc, argv, "firahn:")) != -1) { switch(o) { @@ -108,9 +108,9 @@ void parse_args(int argc, char** argv) case 'n': n = strtol(optarg,&end,0); if(*end != '\0' || n <= 0) { - fprintf(stderr, "Warning: wrong value for '-n'.\n"); + fprintf(stderr, "Warning: wrong value for '-n'.\n"); } else { - g_options.start_at = n - 1; + g_options.start_at = n - 1; } break; case '?': @@ -203,9 +203,7 @@ int main(int argc, char** argv) double last_time = SDL_GetTicks() / 1000.0; - for(long int i = 0; i < g_options.start_at; ++i) { - imv_navigator_next_path(&nav); - } + imv_navigator_set_path(&nav, g_options.start_at); int quit = 0; while(!quit) { diff --git a/src/navigator.c b/src/navigator.c index 1974f8a..d719791 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -175,6 +175,16 @@ void imv_navigator_remove_current_path(struct imv_navigator *nav) nav->changed = 1; } +void imv_navigator_set_path(struct imv_navigator *nav, const int path) +{ + if(path <= 0 || path >= nav->num_paths) { + return; + } + int prev_path = nav->cur_path; + nav->cur_path = path; + nav->changed = prev_path != nav->cur_path; +} + int imv_navigator_has_changed(struct imv_navigator *nav) { if(nav->changed) { diff --git a/src/navigator.h b/src/navigator.h index c6d4f8e..c06bcce 100644 --- a/src/navigator.h +++ b/src/navigator.h @@ -37,6 +37,7 @@ const char *imv_navigator_get_current_path(struct imv_navigator *nav); 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_has_changed(struct imv_navigator *nav); -- cgit v1.2.3