summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqsmodo <75080827+qsmodo@users.noreply.github.com>2021-09-12 20:26:07 +0200
committerBerke Kocaoğlu <berke.kocaoglu@metu.edu.tr>2021-09-16 21:55:31 +0200
commit156a53780c7b27aeb7b4de7de56450f26c55a192 (patch)
tree6d8820c2b8d862e7b878cb9cf9bf31d32e26eb56
parent91d9b3128eb5502306f688e68c42e83b3020cb56 (diff)
downloadnsxiv-156a53780c7b27aeb7b4de7de56450f26c55a192.tar.zst
set title based on prefix and suffix (#23)
Co-authored-by: Guilherme Rugai Freire <41879254+GRFreire@users.noreply.github.com> Co-authored-by: NRK <nrk@disroot.org> Co-authored-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr>
-rw-r--r--config.def.h15
-rw-r--r--main.c2
-rw-r--r--options.c19
-rw-r--r--sxiv.115
-rw-r--r--sxiv.h8
-rw-r--r--thumbs.c2
-rw-r--r--window.c20
7 files changed, 74 insertions, 7 deletions
diff --git a/config.def.h b/config.def.h
index dd8d44d..449f43b 100644
--- a/config.def.h
+++ b/config.def.h
@@ -12,6 +12,21 @@ enum {
*/
#endif
+
+#ifdef _TITLE_CONFIG
+
+/* default title prefix */
+static const char *TITLE_PREFIX = "sxiv - ";
+
+/* default title suffixmode, available options are:
+ * SUFFIX_EMPTY
+ * SUFFIX_BASENAME
+ * SUFFIX_FULLPATH
+ */
+static const suffixmode_t TITLE_SUFFIXMODE = SUFFIX_BASENAME;
+
+#endif
+
#ifdef _IMAGE_CONFIG
/* levels (in percent) to use when zooming via '-' and '+':
diff --git a/main.c b/main.c
index 593e1c1..21d69bc 100644
--- a/main.c
+++ b/main.c
@@ -314,6 +314,7 @@ void load_image(int new)
close_info();
open_info();
arl_setup(&arl, files[fileidx].path);
+ win_set_title(&win, files[fileidx].path);
if (img.multi.cnt > 0 && img.multi.animate)
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
@@ -939,6 +940,7 @@ int main(int argc, char **argv)
load_image(fileidx);
}
win_open(&win);
+ win_set_title(&win, files[fileidx].path);
win_set_cursor(&win, CURSOR_WATCH);
atexit(cleanup);
diff --git a/options.c b/options.c
index 394220f..e89b6f1 100644
--- a/options.c
+++ b/options.c
@@ -18,6 +18,7 @@
#include "sxiv.h"
#define _IMAGE_CONFIG
+#define _TITLE_CONFIG
#include "config.h"
#include "version.h"
@@ -31,8 +32,8 @@ const opt_t *options = (const opt_t*) &_options;
void print_usage(void)
{
printf("usage: sxiv [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] "
- "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] "
- "FILES...\n");
+ "[-g GEOMETRY] [-N NAME] [-T TITLE] [-n NUM] [-S DELAY] [-s MODE] "
+ "[-z ZOOM] FILES...\n");
}
void print_version(void)
@@ -66,13 +67,15 @@ void parse_options(int argc, char **argv)
_options.hide_bar = false;
_options.geometry = NULL;
_options.res_name = NULL;
+ _options.title_prefix = TITLE_PREFIX;
+ _options.title_suffixmode = TITLE_SUFFIXMODE;
_options.quiet = false;
_options.thumb_mode = false;
_options.clean_cache = false;
_options.private_mode = false;
- while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:tvZz:")) != -1) {
+ while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:T:tvZz:")) != -1) {
switch (opt) {
case '?':
print_usage();
@@ -149,6 +152,16 @@ void parse_options(int argc, char **argv)
error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
_options.scalemode = s - scalemodes;
break;
+ case 'T':
+ if ((s = strrchr(optarg, ':')) != NULL) {
+ *s = '\0';
+ n = strtol(++s, &end, 0);
+ if (*end != '\0' || n < SUFFIX_EMPTY || n > SUFFIX_FULLPATH)
+ error(EXIT_FAILURE, 0, "Invalid argument for option -T suffixmode: %s", s);
+ _options.title_suffixmode = n;
+ }
+ _options.title_prefix = optarg;
+ break;
case 't':
_options.thumb_mode = true;
break;
diff --git a/sxiv.1 b/sxiv.1
index e714ed6..ed32bf2 100644
--- a/sxiv.1
+++ b/sxiv.1
@@ -14,6 +14,8 @@ sxiv \- Simple X Image Viewer
.IR GEOMETRY ]
.RB [ \-N
.IR NAME ]
+.RB [ \-T
+.IR TITLE ]
.RB [ \-n
.IR NUM ]
.RB [ \-S
@@ -64,6 +66,19 @@ more information on GEOMETRY argument.
.BI "\-N " NAME
Set the resource name of sxiv's X window to NAME.
.TP
+.BI "\-T " TITLE
+Set the window title to TITLE. Use the format `prefix:suffixmode'. Any string
+literal is accepted for prefix, and the format of suffixmode is:
+
+.EX
+ Value Format
+ 0 Empty
+ 1 Basename of file
+ 2 Full path to file
+.EE
+
+By defualt, prefix is set to "sxiv - " and suffixmode is set to 1 (basename).
+.TP
.BI "\-n " NUM
Start at picture number NUM.
.TP
diff --git a/sxiv.h b/sxiv.h
index c914837..902446d 100644
--- a/sxiv.h
+++ b/sxiv.h
@@ -116,6 +116,12 @@ typedef enum {
FF_TN_INIT = 4
} fileflags_t;
+typedef enum {
+ SUFFIX_EMPTY,
+ SUFFIX_BASENAME,
+ SUFFIX_FULLPATH,
+} suffixmode_t;
+
typedef struct {
const char *name; /* as given by user */
const char *path; /* always absolute */
@@ -280,6 +286,8 @@ struct opt {
long embed;
char *geometry;
char *res_name;
+ const char *title_prefix;
+ suffixmode_t title_suffixmode;
/* misc flags: */
bool quiet;
diff --git a/thumbs.c b/thumbs.c
index 4238aa0..3aa03fb 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -35,6 +35,7 @@ void exif_auto_orientate(const fileinfo_t*);
Imlib_Image img_open(const fileinfo_t*);
static char *cache_dir;
+extern const int fileidx;
char* tns_cache_filepath(const char *filepath)
{
@@ -531,6 +532,7 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
if (!tns->dirty)
tns_highlight(tns, *tns->sel, true);
}
+ win_set_title(tns->win, tns->files[fileidx].path);
return *tns->sel != old;
}
diff --git a/window.c b/window.c
index bd9b0c3..dd17134 100644
--- a/window.c
+++ b/window.c
@@ -276,7 +276,9 @@ void win_open(win_t *win)
}
free(icon_data);
- win_set_title(win, "sxiv");
+ /* These two atoms won't change and thus only need to be set once. */
+ XStoreName(win->env.dpy, win->xwin, "sxiv");
+ XSetIconName(win->env.dpy, win->xwin, "sxiv");
classhint.res_class = RES_CLASS;
classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
@@ -486,10 +488,20 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
}
-void win_set_title(win_t *win, const char *title)
+void win_set_title(win_t *win, const char *path)
{
- XStoreName(win->env.dpy, win->xwin, title);
- XSetIconName(win->env.dpy, win->xwin, title);
+ const unsigned int title_max = strlen(path) + strlen(options->title_prefix) + 1;
+ char title[title_max];
+ const char *basename = strrchr(path, '/') + 1;
+
+ /* Return if window is not ready yet */
+ if (win->xwin == None)
+ return;
+
+ snprintf(title, title_max, "%s%s", options->title_prefix,
+ (options->title_suffixmode == SUFFIX_BASENAME) ? basename : path);
+ if (options->title_suffixmode == SUFFIX_EMPTY)
+ *(title+strlen(options->title_prefix)) = '\0';
XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,