From 156a53780c7b27aeb7b4de7de56450f26c55a192 Mon Sep 17 00:00:00 2001 From: qsmodo <75080827+qsmodo@users.noreply.github.com> Date: Sun, 12 Sep 2021 15:26:07 -0300 Subject: [PATCH] set title based on prefix and suffix (#23) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Guilherme Rugai Freire <41879254+GRFreire@users.noreply.github.com> Co-authored-by: NRK Co-authored-by: Berke Kocaoğlu --- config.def.h | 15 +++++++++++++++ main.c | 2 ++ options.c | 19 ++++++++++++++++--- sxiv.1 | 15 +++++++++++++++ sxiv.h | 8 ++++++++ thumbs.c | 2 ++ window.c | 20 ++++++++++++++++---- 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,