Skip to content

Commit

Permalink
Automatically reload image on exec
Browse files Browse the repository at this point in the history
Resets cache and reloads the current image after executing external
command.

Signed-off-by: Artem Senichev <artemsen@gmail.com>
  • Loading branch information
artemsen committed Dec 22, 2023
1 parent 530bf16 commit de898bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
30 changes: 23 additions & 7 deletions src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ struct image_entry image_list_current(const struct image_list* ctx)
return entry;
}

int image_list_cur_exec(const struct image_list* ctx)
int image_list_exec(const struct image_list* ctx)
{
const char* template = ctx->config->exec_cmd;
const char* path = ctx->current->file_path;
Expand Down Expand Up @@ -511,14 +511,30 @@ int image_list_cur_exec(const struct image_list* ctx)
return rc;
}

bool image_list_cur_reload(struct image_list* ctx)
bool image_list_reset(struct image_list* ctx)
{
struct image* image = image_from_file(ctx->current->file_path);
if (image) {
image_free(ctx->current);
ctx->current = image;
// reset cache
preloader_ctl(ctx, false);
if (ctx->prev) {
image_free(ctx->prev);
ctx->prev = NULL;
}
if (ctx->next) {
image_free(ctx->next);
ctx->next = NULL;
}

// reload current image
image_free(ctx->current);
ctx->current = image_from_file(ctx->entries[ctx->index]->path);
if (ctx->current) {
preloader_ctl(ctx, true);
return true;
}
return !!image;

// open nearest image
return image_list_jump(ctx, jump_next_file) ||
image_list_jump(ctx, jump_prev_file);
}

bool image_list_jump(struct image_list* ctx, enum list_jump jump)
Expand Down
8 changes: 4 additions & 4 deletions src/imagelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ struct image_entry image_list_current(const struct image_list* ctx);
* @param ctx image list context
* @return error code from the system call
*/
int image_list_cur_exec(const struct image_list* ctx);
int image_list_exec(const struct image_list* ctx);

/**
* Reload the current image.
* Reset cache and reload current image.
* @param ctx image list context
* @return false if reload failed
* @return false if reset failed (no more images)
*/
bool image_list_cur_reload(struct image_list* ctx);
bool image_list_reset(struct image_list* ctx);

/**
* Move through image list.
Expand Down
13 changes: 10 additions & 3 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,24 +444,31 @@ bool viewer_on_keyboard(void* data, struct ui* ui, xkb_keysym_t key)
ctx->config->antialiasing ? "on" : "off");
return true;
case cfgact_reload:
if (image_list_cur_reload(ctx->list)) {
if (image_list_reset(ctx->list)) {
reset_state(ctx, ui);
set_message(ctx, "Image reloaded");
return true;
} else {
set_message(ctx, "Reload failed");
printf("No more images, exit\n");
ui_stop(ui);
return false;
}
case cfgact_info:
ctx->config->show_info = !ctx->config->show_info;
return true;
case cfgact_exec: {
const int rc = image_list_cur_exec(ctx->list);
const int rc = image_list_exec(ctx->list);
if (rc) {
set_message(ctx, "Execute failed: code %d", rc);
} else {
set_message(ctx, "Execute success");
}
if (!image_list_reset(ctx->list)) {
printf("No more images, exit\n");
ui_stop(ui);
return false;
}
reset_state(ctx, ui);
return true;
}
case cfgact_quit:
Expand Down

0 comments on commit de898bf

Please sign in to comment.