From 591be8cecfaef143824e76c73f7c586261297c9c Mon Sep 17 00:00:00 2001 From: N-R-K Date: Tue, 3 May 2022 15:34:23 +0000 Subject: Add thumb-info (#265) Closes: https://github.com/nsxiv/nsxiv/issues/88 Closes: https://github.com/nsxiv/nsxiv/pull/253 --- commands.c | 9 ++++++++- examples/image-info | 3 ++- examples/thumb-info | 20 ++++++++++++++++++++ main.c | 29 +++++++++++++++++------------ nsxiv.1 | 27 ++++++++++++++++++++++----- 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100755 examples/thumb-info diff --git a/commands.c b/commands.c index 5f523ba..111d1e9 100644 --- a/commands.c +++ b/commands.c @@ -84,6 +84,8 @@ bool cg_switch_mode(arg_t _) load_image(fileidx); mode = MODE_IMAGE; } + close_info(); + open_info(); return true; } @@ -429,7 +431,12 @@ bool ci_slideshow(arg_t _) bool ct_move_sel(arg_t dir) { - return tns_move_selection(&tns, dir, prefix); + bool dirty = tns_move_selection(&tns, dir, prefix); + if (dirty) { + close_info(); + open_info(); + } + return dirty; } bool ct_reload_all(arg_t _) diff --git a/examples/image-info b/examples/image-info index f608527..5f06123 100755 --- a/examples/image-info +++ b/examples/image-info @@ -4,9 +4,10 @@ # Called by nsxiv(1) whenever an image gets loaded. # The output is displayed in nsxiv's status bar. # Arguments: -# $1: path to image file +# $1: path to image file (as provided by the user) # $2: image width # $3: image height +# $4: fully resolved path to the image file s=" " # field separator diff --git a/examples/thumb-info b/examples/thumb-info new file mode 100755 index 0000000..b422f9c --- /dev/null +++ b/examples/thumb-info @@ -0,0 +1,20 @@ +#!/bin/sh + +# Example for $XDG_CONFIG_HOME/nsxiv/exec/thumb-info +# Called by nsxiv(1) whenever the selected thumbnail changes. +# The output is displayed in nsxiv's status bar. +# Arguments: +# $1: path to image file (as provided by the user) +# $2: empty +# $3: empty +# $4: fully resolved path to the image file + +s=" " # field separator + +exec 2>/dev/null + +filename=$(basename -- "$4") +filesize=$(du -Hh -- "$4" | cut -f 1) + +echo "${filesize}${s}${filename}" + diff --git a/main.c b/main.c index 686fc60..29bb1ec 100644 --- a/main.c +++ b/main.c @@ -76,7 +76,7 @@ typedef struct { } extcmd_t; static struct { - extcmd_t f; + extcmd_t f, ft; int fd; unsigned int i, lastsep; pid_t pid; @@ -283,16 +283,21 @@ void close_info(void) void open_info(void) { spawn_t pfd; - char w[12], h[12]; - char *argv[5]; + char w[12] = "", h[12] = ""; + char *argv[6]; + char *cmd = mode == MODE_IMAGE ? info.f.cmd : info.ft.cmd; + bool ferr = mode == MODE_IMAGE ? info.f.err : info.ft.err; - if (info.f.err || info.fd >= 0 || win.bar.h == 0) + if (ferr || info.fd >= 0 || win.bar.h == 0) return; win.bar.l.buf[0] = '\0'; - snprintf(w, sizeof(w), "%d", img.w); - snprintf(h, sizeof(h), "%d", img.h); - construct_argv(argv, ARRLEN(argv), info.f.cmd, files[fileidx].name, w, h, NULL); - pfd = spawn(info.f.cmd, argv, X_READ); + if (mode == MODE_IMAGE) { + snprintf(w, sizeof(w), "%d", img.w); + snprintf(h, sizeof(h), "%d", img.h); + } + construct_argv(argv, ARRLEN(argv), cmd, files[fileidx].name, w, h, + files[fileidx].path, NULL); + pfd = spawn(cmd, argv, X_READ); if (pfd.readfd >= 0) { fcntl(pfd.readfd, F_SETFL, O_NONBLOCK); info.fd = pfd.readfd; @@ -396,7 +401,7 @@ static void bar_put(win_bar_t *bar, const char *fmt, ...) static void update_info(void) { unsigned int i, fn, fw; - const char * mark; + const char *mark; win_bar_t *l = &win.bar.l, *r = &win.bar.r; /* update bar contents */ @@ -411,7 +416,7 @@ static void update_info(void) bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1); else if (tns.initnext < filecnt) bar_put(l, "Caching... %0*d", fw, tns.initnext + 1); - else + else if (info.ft.err) strncpy(l->buf, files[fileidx].name, l->size); bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt); } else { @@ -916,8 +921,8 @@ int main(int argc, char *argv[]) dsuffix = "/.config"; } if (homedir != NULL) { - extcmd_t *cmd[] = { &info.f, &keyhandler.f, &wintitle.f }; - const char *name[] = { "image-info", "key-handler", "win-title" }; + extcmd_t *cmd[] = { &info.f, &info.ft, &keyhandler.f, &wintitle.f }; + const char *name[] = { "image-info", "thumb-info", "key-handler", "win-title" }; const char *s = "/nsxiv/exec/"; for (i = 0; i < ARRLEN(cmd); i++) { diff --git a/nsxiv.1 b/nsxiv.1 index 729d0ec..4488146 100644 --- a/nsxiv.1 +++ b/nsxiv.1 @@ -450,19 +450,36 @@ There is also an example script installed together with nsxiv as .IR EGPREFIX/win-title . .SH STATUS BAR The information displayed on the left side of the status bar can be replaced -with the output of a user-provided script, which is called by nsxiv whenever an -image gets loaded. The path of this script is +with the output of user-provided script. +.P +The script that is called by nsxiv whenever an image gets loaded is located at .I $XDG_CONFIG_HOME/nsxiv/exec/image-info and the arguments given to it are: .IP $1 4 -path to image file +path to image file (as provided by the user) .IP $2 4 image width .IP $3 4 image height +.IP $4 4 +fully resolved path to the image file .P -There is also an example script installed together with nsxiv as -.IR EGPREFIX/image-info . +In thumbnail mode, the script that is called is located at +.I $XDG_CONFIG_HOME/nsxiv/exec/thumb-info +and the arguments given to it are: +.IP $1 4 +path to image file (as provided by the user) +.IP $2 4 +empty +.IP $3 4 +empty +.IP $4 4 +fully resolved path to the image file +.P +There are also example scripts installed together with nsxiv as +.IR EGPREFIX/image-info +and +.IR EGPREFIX/thumb-info . .SH EXTERNAL KEY HANDLER Additional external keyboard commands can be defined using a handler program located in -- cgit v1.2.3-54-g00ecf