From 72bda973f40520160d8ffd917742a992ba2bf930 Mon Sep 17 00:00:00 2001 From: n3f4s Date: Tue, 13 Jul 2021 14:22:46 +0200 Subject: [PATCH] Pull path from pipe when -n given --- src/imv.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/src/imv.c b/src/imv.c index 7842800..59cc08a 100644 --- a/src/imv.c +++ b/src/imv.c @@ -939,22 +939,51 @@ int imv_run(struct imv *imv) } if (imv->starting_path) { - ssize_t index = imv_navigator_find_path(imv->navigator, imv->starting_path); - if (index == -1) { - index = (int) strtol(imv->starting_path, NULL, 10); - index -= 1; /* input is 1-indexed, internally we're 0 indexed */ - if (errno == EINVAL) { - index = -1; + if (imv->paths_from_stdin) { + bool is_number = true; + for(int i=0; istarting_path); ++i) { + if (!isdigit(imv->starting_path[i])) { + is_number = false; + break; + } + } + ssize_t index = -1; + if (is_number) { + index = strtol(imv->starting_path, NULL, 10); + } + bool cont = true; + while (cont) { + imv_window_pump_events(imv->window, event_handler, imv); + if (index == -1) { + ssize_t img_index = imv_navigator_find_path(imv->navigator, imv->starting_path); + if(img_index != -1) { + imv_navigator_select_abs(imv->navigator, img_index); + cont = false; + } + } else { + if(imv_navigator_length(imv->navigator) >= index) { + imv_navigator_select_abs(imv->navigator, index); + cont = false; + } + } + } + } else { + ssize_t index = imv_navigator_find_path(imv->navigator, imv->starting_path); + if (index == -1) { + index = (int) strtol(imv->starting_path, NULL, 10); + index -= 1; /* input is 1-indexed, internally we're 0 indexed */ + if (errno == EINVAL) { + index = -1; + } + } + + if (index >= 0) { + imv_navigator_select_abs(imv->navigator, index); + } else { + imv_log(IMV_ERROR, "Invalid starting image: %s\n", imv->starting_path); } } - - if (index >= 0) { - imv_navigator_select_abs(imv->navigator, index); - } else { - imv_log(IMV_ERROR, "Invalid starting image: %s\n", imv->starting_path); - } } - /* Push any startup commands into the event queue */ for (size_t i = 0; i < imv->startup_commands->len; ++i) { command_callback(imv->startup_commands->items[i], imv);