From c6ce270ee1e54fc7ae9c4029923bccedbcac5ad2 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Fri, 16 Aug 2019 19:08:54 +0100 Subject: imv: Add index/all argument to close command --- src/imv.c | 17 ++++++++++++++--- src/list.c | 5 +++++ src/list.h | 2 ++ src/navigator.c | 38 ++++++++++++++++++++++++++++++++++++++ src/navigator.h | 7 +++++++ 5 files changed, 66 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/imv.c b/src/imv.c index 9dbc7d5..991de70 100644 --- a/src/imv.c +++ b/src/imv.c @@ -1536,9 +1536,20 @@ static void command_close(struct list *args, const char *argstr, void *data) (void)args; (void)argstr; struct imv *imv = data; - char* path = strdup(imv_navigator_selection(imv->navigator)); - imv_navigator_remove(imv->navigator, path); - free(path); + size_t index = imv_navigator_index(imv->navigator); + + if (args->len == 2) { + const char *arg = args->items[1]; + if (!strcmp("all", arg)) { + imv_navigator_remove_all(imv->navigator); + imv->slideshow.elapsed = 0; + return; + } + + index = (size_t)strtol(arg, NULL, 10) - 1; + } + + imv_navigator_remove_at(imv->navigator, index); imv->slideshow.elapsed = 0; } diff --git a/src/list.c b/src/list.c index b719cf0..ba5ce87 100644 --- a/src/list.c +++ b/src/list.c @@ -66,6 +66,11 @@ void list_insert(struct list *list, size_t index, void *item) list->len += 1; } +void list_clear(struct list *list) +{ + list->len = 0; +} + struct list *list_from_string(const char *string, char delim) { struct list *list = list_create(); diff --git a/src/list.h b/src/list.h index bb88c2c..abb8b85 100644 --- a/src/list.h +++ b/src/list.h @@ -24,6 +24,8 @@ void list_remove(struct list *list, size_t index); void list_insert(struct list *list, size_t index, void *item); +void list_clear(struct list *list); + struct list *list_from_string(const char *string, char delim); int list_find( diff --git a/src/navigator.c b/src/navigator.c index 98c1976..6b77643 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -215,6 +215,44 @@ void imv_navigator_remove(struct imv_navigator *nav, const char *path) nav->changed = 1; } +void imv_navigator_remove_at(struct imv_navigator *nav, size_t index) +{ + if (index >= nav->paths->len) { + return; + } + struct nav_item *item = nav->paths->items[index]; + free(item->path); + free(item); + list_remove(nav->paths, index); + + if (nav->cur_path == index) { + /* We just removed the current path */ + if (nav->last_move_direction < 0) { + /* Move left */ + imv_navigator_select_rel(nav, -1); + } else { + /* Try to stay where we are, unless we ran out of room */ + if (nav->cur_path == nav->paths->len) { + nav->cur_path = 0; + nav->wrapped = 1; + } + } + } + nav->changed = 1; +} + +void imv_navigator_remove_all(struct imv_navigator *nav) +{ + for (size_t i = 0; i < nav->paths->len; ++i) { + struct nav_item *item = nav->paths->items[i]; + free(item->path); + free(item); + } + list_clear(nav->paths); + nav->cur_path = 0; + nav->changed = 1; +} + ssize_t imv_navigator_find_path(struct imv_navigator *nav, const char *path) { /* first try to match the exact path */ diff --git a/src/navigator.h b/src/navigator.h index 06b5c92..6234206 100644 --- a/src/navigator.h +++ b/src/navigator.h @@ -34,6 +34,13 @@ void imv_navigator_select_abs(struct imv_navigator *nav, ssize_t index); * based on the last direction the selection moved. */ void imv_navigator_remove(struct imv_navigator *nav, const char *path); +/* Removes the given index. The current selection is updated if necessary, + * based on the last direction the selection moved. */ +void imv_navigator_remove_at(struct imv_navigator *nav, size_t index); + +/* Removes all paths */ +void imv_navigator_remove_all(struct imv_navigator *nav); + /* Return the index of the path given. Returns -1 if not found. */ ssize_t imv_navigator_find_path(struct imv_navigator *nav, const char *path); -- cgit v1.2.3