Krolyxon
Commit 156a537

Branches

Refs →

nsxiv156a537

set title based on prefix and suffix (#23)

Author
qsmodo
Date
2021-09-12 15:26 -0300
Committer
Berke Kocaoğlu · 2021-09-16 22:55 +0300
Parent
91d9b31
Commit
156a53780c7b27aeb7b4de7de56450f26c55a192

+74 −7

7 files changed

  1. M config.def.h +15 −0
  2. M main.c +2 −0
  3. M options.c +16 −3
  4. M sxiv.1 +15 −0
  5. M sxiv.h +8 −0
  6. M thumbs.c +2 −0
  7. M window.c +16 −4
M config.def.h View
@@ -12,6 +12,21 @@ enum {
1212 */
1313
1414 #endif
15+
16+#ifdef _TITLE_CONFIG
17+
18+/* default title prefix */
19+static const char *TITLE_PREFIX = "sxiv - ";
20+
21+/* default title suffixmode, available options are:
22+ * SUFFIX_EMPTY
23+ * SUFFIX_BASENAME
24+ * SUFFIX_FULLPATH
25+ */
26+static const suffixmode_t TITLE_SUFFIXMODE = SUFFIX_BASENAME;
27+
28+#endif
29+
1530 #ifdef _IMAGE_CONFIG
1631
1732 /* levels (in percent) to use when zooming via '-' and '+':
M main.c View
@@ -314,6 +314,7 @@ void load_image(int new)
314314 close_info();
315315 open_info();
316316 arl_setup(&arl, files[fileidx].path);
317+ win_set_title(&win, files[fileidx].path);
317318
318319 if (img.multi.cnt > 0 && img.multi.animate)
319320 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
@@ -939,6 +940,7 @@ int main(int argc, char **argv)
939940 load_image(fileidx);
940941 }
941942 win_open(&win);
943+ win_set_title(&win, files[fileidx].path);
942944 win_set_cursor(&win, CURSOR_WATCH);
943945
944946 atexit(cleanup);
M options.c View
@@ -18,6 +18,7 @@
1818
1919 #include "sxiv.h"
2020 #define _IMAGE_CONFIG
21+#define _TITLE_CONFIG
2122 #include "config.h"
2223 #include "version.h"
2324
@@ -31,8 +32,8 @@ const opt_t *options = (const opt_t*) &_options;
3132 void print_usage(void)
3233 {
3334 printf("usage: sxiv [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] "
34− "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] "
35− "FILES...\n");
35+ "[-g GEOMETRY] [-N NAME] [-T TITLE] [-n NUM] [-S DELAY] [-s MODE] "
36+ "[-z ZOOM] FILES...\n");
3637 }
3738
3839 void print_version(void)
@@ -66,13 +67,15 @@ void parse_options(int argc, char **argv)
6667 _options.hide_bar = false;
6768 _options.geometry = NULL;
6869 _options.res_name = NULL;
70+ _options.title_prefix = TITLE_PREFIX;
71+ _options.title_suffixmode = TITLE_SUFFIXMODE;
6972
7073 _options.quiet = false;
7174 _options.thumb_mode = false;
7275 _options.clean_cache = false;
7376 _options.private_mode = false;
7477
75− while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:tvZz:")) != -1) {
78+ while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:T:tvZz:")) != -1) {
7679 switch (opt) {
7780 case '?':
7881 print_usage();
@@ -149,6 +152,16 @@ void parse_options(int argc, char **argv)
149152 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
150153 _options.scalemode = s - scalemodes;
151154 break;
155+ case 'T':
156+ if ((s = strrchr(optarg, ':')) != NULL) {
157+ *s = '\0';
158+ n = strtol(++s, &end, 0);
159+ if (*end != '\0' || n < SUFFIX_EMPTY || n > SUFFIX_FULLPATH)
160+ error(EXIT_FAILURE, 0, "Invalid argument for option -T suffixmode: %s", s);
161+ _options.title_suffixmode = n;
162+ }
163+ _options.title_prefix = optarg;
164+ break;
152165 case 't':
153166 _options.thumb_mode = true;
154167 break;
M sxiv.1 View
@@ -14,6 +14,8 @@ sxiv \- Simple X Image Viewer
1414 .IR GEOMETRY ]
1515 .RB [ \-N
1616 .IR NAME ]
17+.RB [ \-T
18+.IR TITLE ]
1719 .RB [ \-n
1820 .IR NUM ]
1921 .RB [ \-S
@@ -64,6 +66,19 @@ more information on GEOMETRY argument.
6466 .BI "\-N " NAME
6567 Set the resource name of sxiv's X window to NAME.
6668 .TP
69+.BI "\-T " TITLE
70+Set the window title to TITLE. Use the format `prefix:suffixmode'. Any string
71+literal is accepted for prefix, and the format of suffixmode is:
72+
73+.EX
74+ Value Format
75+ 0 Empty
76+ 1 Basename of file
77+ 2 Full path to file
78+.EE
79+
80+By defualt, prefix is set to "sxiv - " and suffixmode is set to 1 (basename).
81+.TP
6782 .BI "\-n " NUM
6883 Start at picture number NUM.
6984 .TP
M sxiv.h View
@@ -116,6 +116,12 @@ typedef enum {
116116 FF_TN_INIT = 4
117117 } fileflags_t;
118118
119+typedef enum {
120+ SUFFIX_EMPTY,
121+ SUFFIX_BASENAME,
122+ SUFFIX_FULLPATH,
123+} suffixmode_t;
124+
119125 typedef struct {
120126 const char *name; /* as given by user */
121127 const char *path; /* always absolute */
@@ -280,6 +286,8 @@ struct opt {
280286 long embed;
281287 char *geometry;
282288 char *res_name;
289+ const char *title_prefix;
290+ suffixmode_t title_suffixmode;
283291
284292 /* misc flags: */
285293 bool quiet;
M thumbs.c View
@@ -35,6 +35,7 @@ void exif_auto_orientate(const fileinfo_t*);
3535 Imlib_Image img_open(const fileinfo_t*);
3636
3737 static char *cache_dir;
38+extern const int fileidx;
3839
3940 char* tns_cache_filepath(const char *filepath)
4041 {
@@ -531,6 +532,7 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
531532 if (!tns->dirty)
532533 tns_highlight(tns, *tns->sel, true);
533534 }
535+ win_set_title(tns->win, tns->files[fileidx].path);
534536 return *tns->sel != old;
535537 }
536538
M window.c View
@@ -276,7 +276,9 @@ void win_open(win_t *win)
276276 }
277277 free(icon_data);
278278
279− win_set_title(win, "sxiv");
279+ /* These two atoms won't change and thus only need to be set once. */
280+ XStoreName(win->env.dpy, win->xwin, "sxiv");
281+ XSetIconName(win->env.dpy, win->xwin, "sxiv");
280282
281283 classhint.res_class = RES_CLASS;
282284 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,
486488 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
487489 }
488490
489−void win_set_title(win_t *win, const char *title)
491+void win_set_title(win_t *win, const char *path)
490492 {
491− XStoreName(win->env.dpy, win->xwin, title);
492− XSetIconName(win->env.dpy, win->xwin, title);
493+ const unsigned int title_max = strlen(path) + strlen(options->title_prefix) + 1;
494+ char title[title_max];
495+ const char *basename = strrchr(path, '/') + 1;
496+
497+ /* Return if window is not ready yet */
498+ if (win->xwin == None)
499+ return;
500+
501+ snprintf(title, title_max, "%s%s", options->title_prefix,
502+ (options->title_suffixmode == SUFFIX_BASENAME) ? basename : path);
503+ if (options->title_suffixmode == SUFFIX_EMPTY)
504+ *(title+strlen(options->title_prefix)) = '\0';
493505
494506 XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
495507 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,