aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2017-11-29 21:13:10 +0000
committerHarry Jeffery <harry@exec64.co.uk>2017-11-29 21:13:56 +0000
commit4e3dfb80f367179a93ab4410f7cebd766bcc3bde (patch)
tree1a4ff2c75dbcea5372eb3ee85bb58621273933fa
parent957a3efd3ea2d7b087f0b70f8cd4b733dfa4d29d (diff)
downloadimv-4e3dfb80f367179a93ab4410f7cebd766bcc3bde.tar.gz
Make title+overlay configurable
Fixes #110
-rw-r--r--files/imv_config20
-rw-r--r--src/imv.c93
2 files changed, 84 insertions, 29 deletions
diff --git a/files/imv_config b/files/imv_config
index 1cd58af..f676963 100644
--- a/files/imv_config
+++ b/files/imv_config
@@ -38,6 +38,26 @@
# Disable imv's builtin binds so they don't conflict with the ones in this config
suppress_default_binds = true
+# Set the text used for the title and overlay. Environment variables
+# can be used, as can $(command) shell expansion, etc.
+# imv exports details about its state and the current image through various
+# environment variables.
+
+# Available values:
+# imv_current_file - path of currently opened image
+# imv_scaling_mode - name of the current scaling mode
+# imv_loading - 1 when a new image is loading, 0 otherwise
+# imv_current_index - index of current image, from 1-N
+# imv_file_count - total number of files
+# imv_width - width of current image
+# imv_height - height of current image
+# imv_scale - scaling of current image in percent
+# imv_slideshow_duration - number of seconds each image is shown for
+# imv_slideshow_elapsed - how long the current image has been shown for
+
+# title_text = imv - [${imv_current_index}/${imv_file_count}] $imv_current_file
+# overlay_text = ${imv_width}x${imv_height} @$ {imv_scale}% [$imv_scaling_mode]
+
[aliases]
# Create some command aliases
q = quit
diff --git a/src/imv.c b/src/imv.c
index 904678b..02a1f0d 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -70,6 +70,8 @@ struct imv {
size_t stdin_image_data_len;
char *input_buffer;
char *starting_path;
+ char *title_text;
+ char *overlay_text;
SDL_Window *window;
SDL_Renderer *renderer;
@@ -108,8 +110,7 @@ static bool setup_window(struct imv *imv);
static void handle_event(struct imv *imv, SDL_Event *event);
static void render_window(struct imv *imv);
static void update_env_vars(struct imv *imv);
-static size_t generate_title_text(struct imv *imv, char *buf, size_t len);
-static size_t generate_overlay_text(struct imv *imv, char *buf, size_t len);
+static size_t generate_env_text(struct imv *imv, char *buf, size_t len, const char *format);
static const char *add_bind(struct imv *imv, const char *keys, const char *command)
{
@@ -161,6 +162,16 @@ struct imv *imv_create(void)
imv->stdin_image_data_len = 0;
imv->input_buffer = NULL;
imv->starting_path = NULL;
+ imv->title_text = strdup(
+ "imv - [${imv_current_index}/${imv_file_count}]"
+ " [${imv_width}x${imv_height}] [${imv_scale}%]"
+ " $imv_current_file [$imv_scaling_mode]"
+ );
+ imv->overlay_text = strdup(
+ "[${imv_current_index}/${imv_file_count}]"
+ " [${imv_width}x${imv_height}] [${imv_scale}%]"
+ " $imv_current_file [$imv_scaling_mode]"
+ );
imv->window = NULL;
imv->renderer = NULL;
imv->font = NULL;
@@ -487,7 +498,7 @@ int imv_run(struct imv *imv)
imv->view->playing = true;
char title[1024];
- generate_title_text(imv, &title[0], sizeof title);
+ generate_env_text(imv, &title[0], sizeof title, imv->title_text);
imv_viewport_set_title(imv->view, title);
}
@@ -772,7 +783,7 @@ static void render_window(struct imv *imv)
/* update window title */
char title_text[1024];
- generate_title_text(imv, &title_text[0], sizeof title_text);
+ generate_env_text(imv, &title_text[0], sizeof title_text, imv->title_text);
imv_viewport_set_title(imv->view, title_text);
/* first we draw the background */
@@ -805,7 +816,7 @@ static void render_window(struct imv *imv)
SDL_Color fg = {255,255,255,255};
SDL_Color bg = {0,0,0,160};
char overlay_text[1024];
- generate_overlay_text(imv, overlay_text, sizeof overlay_text);
+ generate_env_text(imv, overlay_text, sizeof overlay_text, imv->overlay_text);
imv_printf(imv->renderer, imv->font, 0, 0, &fg, &bg, "%s", overlay_text);
}
@@ -953,6 +964,18 @@ static int handle_ini_value(void *user, const char *section, const char *name,
return 1;
}
+ if(!strcmp(name, "overlay_text")) {
+ free(imv->overlay_text);
+ imv->overlay_text = strdup(value);
+ return 1;
+ }
+
+ if(!strcmp(name, "title_text")) {
+ free(imv->title_text);
+ imv->title_text = strdup(value);
+ return 1;
+ }
+
if(!strcmp(name, "suppress_default_binds")) {
const bool suppress_default_binds = parse_bool(value);
if(suppress_default_binds) {
@@ -1173,40 +1196,52 @@ void command_set_slideshow_duration(struct list *args, const char *argstr, void
static void update_env_vars(struct imv *imv)
{
+ char str[64];
+
setenv("imv_current_file", imv_navigator_selection(imv->navigator), 1);
+ setenv("imv_scaling_mode", scaling_label[imv->scaling_mode], 1);
+ setenv("imv_loading", imv->loading ? "1" : "0", 1);
+
+ snprintf(str, sizeof str, "%zu", imv_navigator_index(imv->navigator) + 1);
+ setenv("imv_current_index", str, 1);
+
+ snprintf(str, sizeof str, "%zu", imv_navigator_length(imv->navigator));
+ setenv("imv_file_count", str, 1);
+
+ snprintf(str, sizeof str, "%d", imv_image_width(imv->image));
+ setenv("imv_width", str, 1);
+
+ snprintf(str, sizeof str, "%d", imv_image_height(imv->image));
+ setenv("imv_height", str, 1);
+
+ snprintf(str, sizeof str, "%d", (int)(imv->view->scale * 100.0));
+ setenv("imv_scale", str, 1);
+
+ snprintf(str, sizeof str, "%zu", imv->slideshow_image_duration / 1000);
+ setenv("imv_slidshow_duration", str, 1);
+
+ snprintf(str, sizeof str, "%zu", imv->slideshow_time_elapsed / 1000);
+ setenv("imv_slidshow_elapsed", str, 1);
}
-static size_t generate_title_text(struct imv *imv, char *buf, size_t buf_len)
+static size_t generate_env_text(struct imv *imv, char *buf, size_t buf_len, const char *format)
{
- const char *current_path = imv_navigator_selection(imv->navigator);
- const size_t index_cur = imv_navigator_index(imv->navigator);
- const size_t index_len = imv_navigator_length(imv->navigator);
+ update_env_vars(imv);
- size_t len = 0;
+ /* const char *format = "imv - [${imv_current_index}/${imv_file_count}] [${imv_width}x${imv_height}] [${imv_scale}%] $imv_current_file [$imv_scaling_mode]"; */
- if (imv->loading) {
- len += snprintf(buf, buf_len, "imv - [%zu/%zu] [LOADING] %s [%s]",
- index_cur + 1, index_len, current_path,
- scaling_label[imv->scaling_mode]);
- } else {
- len += snprintf(buf, buf_len, "imv - [%zu/%zu] [%ix%i] [%.2f%%] %s [%s]",
- index_cur + 1, index_len,
- imv_image_width(imv->image), imv_image_height(imv->image),
- 100.0 * imv->view->scale,
- current_path, scaling_label[imv->scaling_mode]);
-
- if(imv->slideshow_image_duration >= 1000) {
- len += snprintf(buf + len, buf_len - len, "[%lu/%lus]",
- imv->slideshow_time_elapsed / 1000 + 1, imv->slideshow_image_duration / 1000);
+ size_t len = 0;
+ wordexp_t word;
+ if(wordexp(format, &word, 0) == 0) {
+ for(size_t i = 0; i < word.we_wordc; ++i) {
+ len += snprintf(buf + len, buf_len - len, "%s ", word.we_wordv[i]);
}
+ wordfree(&word);
+ } else {
+ len += snprintf(buf, buf_len, "error expanding text");
}
return len;
}
-static size_t generate_overlay_text(struct imv *imv, char *buf, size_t len)
-{
- return generate_title_text(imv, buf, len) + strlen("imv - ");
-}
-
/* vim:set ts=2 sts=2 sw=2 et: */