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
#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 '+':

2
main.c
View file

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

View file

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

15
sxiv.1
View file

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

8
sxiv.h
View file

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

View file

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

View file

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