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>
This commit is contained in:
qsmodo 2021-09-12 15:26:07 -03:00 committed by Berke Kocaoğlu
parent 91d9b3128e
commit 156a53780c
7 changed files with 74 additions and 7 deletions

View File

@ -12,6 +12,21 @@ enum {
*/ */
#endif #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 #ifdef _IMAGE_CONFIG
/* levels (in percent) to use when zooming via '-' and '+': /* levels (in percent) to use when zooming via '-' and '+':

2
main.c
View File

@ -314,6 +314,7 @@ void load_image(int new)
close_info(); close_info();
open_info(); open_info();
arl_setup(&arl, files[fileidx].path); arl_setup(&arl, files[fileidx].path);
win_set_title(&win, files[fileidx].path);
if (img.multi.cnt > 0 && img.multi.animate) if (img.multi.cnt > 0 && img.multi.animate)
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
@ -939,6 +940,7 @@ int main(int argc, char **argv)
load_image(fileidx); load_image(fileidx);
} }
win_open(&win); win_open(&win);
win_set_title(&win, files[fileidx].path);
win_set_cursor(&win, CURSOR_WATCH); win_set_cursor(&win, CURSOR_WATCH);
atexit(cleanup); atexit(cleanup);

View File

@ -18,6 +18,7 @@
#include "sxiv.h" #include "sxiv.h"
#define _IMAGE_CONFIG #define _IMAGE_CONFIG
#define _TITLE_CONFIG
#include "config.h" #include "config.h"
#include "version.h" #include "version.h"
@ -31,8 +32,8 @@ const opt_t *options = (const opt_t*) &_options;
void print_usage(void) void print_usage(void)
{ {
printf("usage: sxiv [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] " printf("usage: sxiv [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] "
"[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] " "[-g GEOMETRY] [-N NAME] [-T TITLE] [-n NUM] [-S DELAY] [-s MODE] "
"FILES...\n"); "[-z ZOOM] FILES...\n");
} }
void print_version(void) void print_version(void)
@ -66,13 +67,15 @@ void parse_options(int argc, char **argv)
_options.hide_bar = false; _options.hide_bar = false;
_options.geometry = NULL; _options.geometry = NULL;
_options.res_name = NULL; _options.res_name = NULL;
_options.title_prefix = TITLE_PREFIX;
_options.title_suffixmode = TITLE_SUFFIXMODE;
_options.quiet = false; _options.quiet = false;
_options.thumb_mode = false; _options.thumb_mode = false;
_options.clean_cache = false; _options.clean_cache = false;
_options.private_mode = 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) { switch (opt) {
case '?': case '?':
print_usage(); print_usage();
@ -149,6 +152,16 @@ void parse_options(int argc, char **argv)
error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg); error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
_options.scalemode = s - scalemodes; _options.scalemode = s - scalemodes;
break; 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': case 't':
_options.thumb_mode = true; _options.thumb_mode = true;
break; break;

15
sxiv.1
View File

@ -14,6 +14,8 @@ sxiv \- Simple X Image Viewer
.IR GEOMETRY ] .IR GEOMETRY ]
.RB [ \-N .RB [ \-N
.IR NAME ] .IR NAME ]
.RB [ \-T
.IR TITLE ]
.RB [ \-n .RB [ \-n
.IR NUM ] .IR NUM ]
.RB [ \-S .RB [ \-S
@ -64,6 +66,19 @@ more information on GEOMETRY argument.
.BI "\-N " NAME .BI "\-N " NAME
Set the resource name of sxiv's X window to NAME. Set the resource name of sxiv's X window to NAME.
.TP .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 .BI "\-n " NUM
Start at picture number NUM. Start at picture number NUM.
.TP .TP

8
sxiv.h
View File

@ -116,6 +116,12 @@ typedef enum {
FF_TN_INIT = 4 FF_TN_INIT = 4
} fileflags_t; } fileflags_t;
typedef enum {
SUFFIX_EMPTY,
SUFFIX_BASENAME,
SUFFIX_FULLPATH,
} suffixmode_t;
typedef struct { typedef struct {
const char *name; /* as given by user */ const char *name; /* as given by user */
const char *path; /* always absolute */ const char *path; /* always absolute */
@ -280,6 +286,8 @@ struct opt {
long embed; long embed;
char *geometry; char *geometry;
char *res_name; char *res_name;
const char *title_prefix;
suffixmode_t title_suffixmode;
/* misc flags: */ /* misc flags: */
bool quiet; bool quiet;

View File

@ -35,6 +35,7 @@ void exif_auto_orientate(const fileinfo_t*);
Imlib_Image img_open(const fileinfo_t*); Imlib_Image img_open(const fileinfo_t*);
static char *cache_dir; static char *cache_dir;
extern const int fileidx;
char* tns_cache_filepath(const char *filepath) 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) if (!tns->dirty)
tns_highlight(tns, *tns->sel, true); tns_highlight(tns, *tns->sel, true);
} }
win_set_title(tns->win, tns->files[fileidx].path);
return *tns->sel != old; return *tns->sel != old;
} }

View File

@ -276,7 +276,9 @@ void win_open(win_t *win)
} }
free(icon_data); 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_class = RES_CLASS;
classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv"; 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); 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); const unsigned int title_max = strlen(path) + strlen(options->title_prefix) + 1;
XSetIconName(win->env.dpy, win->xwin, title); 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], XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8, XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,