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);
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 _)

View File

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

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

25
main.c
View File

@ -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';
if (mode == MODE_IMAGE) {
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);
}
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++) {

27
nsxiv.1
View File

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