Add thumb-info (#265)

Closes: https://github.com/nsxiv/nsxiv/issues/88
Closes: https://github.com/nsxiv/nsxiv/pull/253
This commit is contained in:
N-R-K 2022-05-03 15:34:23 +00:00 committed by GitHub
parent 6922d5d01b
commit 591be8cecf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 19 deletions

View file

@ -84,6 +84,8 @@ bool cg_switch_mode(arg_t _)
load_image(fileidx); load_image(fileidx);
mode = MODE_IMAGE; mode = MODE_IMAGE;
} }
close_info();
open_info();
return true; return true;
} }
@ -429,7 +431,12 @@ bool ci_slideshow(arg_t _)
bool ct_move_sel(arg_t dir) 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 _) bool ct_reload_all(arg_t _)

View file

@ -4,9 +4,10 @@
# Called by nsxiv(1) whenever an image gets loaded. # Called by nsxiv(1) whenever an image gets loaded.
# The output is displayed in nsxiv's status bar. # The output is displayed in nsxiv's status bar.
# Arguments: # Arguments:
# $1: path to image file # $1: path to image file (as provided by the user)
# $2: image width # $2: image width
# $3: image height # $3: image height
# $4: fully resolved path to the image file
s=" " # field separator s=" " # field separator

20
examples/thumb-info Executable file
View file

@ -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}"

23
main.c
View file

@ -76,7 +76,7 @@ typedef struct {
} extcmd_t; } extcmd_t;
static struct { static struct {
extcmd_t f; extcmd_t f, ft;
int fd; int fd;
unsigned int i, lastsep; unsigned int i, lastsep;
pid_t pid; pid_t pid;
@ -283,16 +283,21 @@ void close_info(void)
void open_info(void) void open_info(void)
{ {
spawn_t pfd; spawn_t pfd;
char w[12], h[12]; char w[12] = "", h[12] = "";
char *argv[5]; 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; return;
win.bar.l.buf[0] = '\0'; win.bar.l.buf[0] = '\0';
if (mode == MODE_IMAGE) {
snprintf(w, sizeof(w), "%d", img.w); snprintf(w, sizeof(w), "%d", img.w);
snprintf(h, sizeof(h), "%d", img.h); 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); 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) { if (pfd.readfd >= 0) {
fcntl(pfd.readfd, F_SETFL, O_NONBLOCK); fcntl(pfd.readfd, F_SETFL, O_NONBLOCK);
info.fd = pfd.readfd; info.fd = pfd.readfd;
@ -411,7 +416,7 @@ static void update_info(void)
bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1); bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
else if (tns.initnext < filecnt) else if (tns.initnext < filecnt)
bar_put(l, "Caching... %0*d", fw, tns.initnext + 1); bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
else else if (info.ft.err)
strncpy(l->buf, files[fileidx].name, l->size); strncpy(l->buf, files[fileidx].name, l->size);
bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt); bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
} else { } else {
@ -916,8 +921,8 @@ int main(int argc, char *argv[])
dsuffix = "/.config"; dsuffix = "/.config";
} }
if (homedir != NULL) { if (homedir != NULL) {
extcmd_t *cmd[] = { &info.f, &keyhandler.f, &wintitle.f }; extcmd_t *cmd[] = { &info.f, &info.ft, &keyhandler.f, &wintitle.f };
const char *name[] = { "image-info", "key-handler", "win-title" }; const char *name[] = { "image-info", "thumb-info", "key-handler", "win-title" };
const char *s = "/nsxiv/exec/"; const char *s = "/nsxiv/exec/";
for (i = 0; i < ARRLEN(cmd); i++) { for (i = 0; i < ARRLEN(cmd); i++) {

27
nsxiv.1
View file

@ -450,19 +450,36 @@ There is also an example script installed together with nsxiv as
.IR EGPREFIX/win-title . .IR EGPREFIX/win-title .
.SH STATUS BAR .SH STATUS BAR
The information displayed on the left side of the status bar can be replaced 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 with the output of user-provided script.
image gets loaded. The path of this script is .P
The script that is called by nsxiv whenever an image gets loaded is located at
.I $XDG_CONFIG_HOME/nsxiv/exec/image-info .I $XDG_CONFIG_HOME/nsxiv/exec/image-info
and the arguments given to it are: and the arguments given to it are:
.IP $1 4 .IP $1 4
path to image file path to image file (as provided by the user)
.IP $2 4 .IP $2 4
image width image width
.IP $3 4 .IP $3 4
image height image height
.IP $4 4
fully resolved path to the image file
.P .P
There is also an example script installed together with nsxiv as In thumbnail mode, the script that is called is located at
.IR EGPREFIX/image-info . .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 .SH EXTERNAL KEY HANDLER
Additional external keyboard commands can be defined using a handler program Additional external keyboard commands can be defined using a handler program
located in located in