aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c43
-rw-r--r--window.c26
-rw-r--r--window.h2
3 files changed, 47 insertions, 24 deletions
diff --git a/main.c b/main.c
index 193dbbe..d6fb6f9 100644
--- a/main.c
+++ b/main.c
@@ -22,7 +22,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
@@ -87,6 +86,8 @@ void cleanup(void) {
}
void check_add_file(char *filename) {
+ const char *bn;
+
if (filename == NULL || *filename == '\0')
return;
@@ -108,9 +109,12 @@ void check_add_file(char *filename) {
}
files[fileidx].loaded = false;
files[fileidx].name = s_strdup(filename);
- files[fileidx].base = s_strdup(basename(filename));
if (*filename == '/')
files[fileidx].path = files[fileidx].name;
+ if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
+ files[fileidx].base = ++bn;
+ else
+ files[fileidx].base = files[fileidx].name;
fileidx++;
}
@@ -221,7 +225,7 @@ void load_image(int new) {
}
void update_info(void) {
- unsigned int i, fw, pw;
+ int i, fw, pw, fi, ln, rn;
char frame_info[16];
const char *size_unit;
float size = filesize;
@@ -235,8 +239,13 @@ void update_info(void) {
snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
pw, tns.cnt, filecnt);
} else {
- snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
- pw, tns.sel + 1, filecnt, BAR_SEPARATOR, files[tns.sel].base);
+ fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
+ pw, tns.sel + 1, filecnt, BAR_SEPARATOR);
+ ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
+ files[tns.sel].name) + fi;
+ if (win_textwidth(win_bar_l, ln, true) > win.w)
+ snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
+ files[tns.sel].base);
}
win_set_title(&win, "sxiv");
win_set_bar_info(&win, win_bar_l, NULL);
@@ -247,18 +256,28 @@ void update_info(void) {
for (i = img.multi.cnt; i > 0; i /= 10)
fw++;
snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
- BAR_SEPARATOR, fw, img.multi.sel + 1, img.multi.cnt);
+ BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt);
} else {
frame_info[0] = '\0';
}
- snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
- pw, fileidx + 1, filecnt, BAR_SEPARATOR, files[fileidx].base);
- snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
- size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
- (int) (img.zoom * 100.0), frame_info);
+ fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
+ pw, fileidx + 1, filecnt, BAR_SEPARATOR);
+ ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
+ files[fileidx].name) + fi;
+ rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
+ size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
+ (int) (img.zoom * 100.0), frame_info);
+
+ if (win_textwidth(win_bar_l, ln, true) +
+ win_textwidth(win_bar_r, rn, true) > win.w)
+ {
+ snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
+ files[fileidx].base);
+ }
+ win_set_bar_info(&win, win_bar_l, win_bar_r);
+
snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
win_set_title(&win, win_title);
- win_set_bar_info(&win, win_bar_l, win_bar_r);
}
}
diff --git a/window.c b/window.c
index f66703e..5d2eff3 100644
--- a/window.c
+++ b/window.c
@@ -338,16 +338,6 @@ void win_clear(win_t *win) {
XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
}
-int win_textwidth(const char *text, unsigned int len) {
- XRectangle r;
-
- if (font.set) {
- XmbTextExtents(font.set, text, len, NULL, &r);
- return r.width;
- } else {
- return XTextWidth(font.xfont, text, len);
- }
-}
void win_draw_bar(win_t *win) {
win_env_t *e;
int len, x, y, w, tw = 0, seplen;
@@ -369,7 +359,7 @@ void win_draw_bar(win_t *win) {
if (win->lbar != NULL) {
len = strlen(win->lbar);
- while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w)
+ while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w)
len--;
w -= tw + 2 * H_TEXT_PAD;
if (font.set)
@@ -381,7 +371,7 @@ void win_draw_bar(win_t *win) {
len = strlen(win->rbar);
seplen = strlen(BAR_SEPARATOR);
rt = win->rbar;
- while (len > 0 && (tw = win_textwidth(rt, len)) > w) {
+ while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) {
rt = strstr(rt, BAR_SEPARATOR);
if (rt != NULL) {
rt += seplen;
@@ -427,6 +417,18 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
}
+int win_textwidth(const char *text, unsigned int len, bool with_padding) {
+ XRectangle r;
+ int padding = with_padding ? 2 * H_TEXT_PAD : 0;
+
+ if (font.set) {
+ XmbTextExtents(font.set, text, len, NULL, &r);
+ return r.width + padding;
+ } else {
+ return XTextWidth(font.xfont, text, len) + padding;
+ }
+}
+
void win_set_title(win_t *win, const char *title) {
if (win == NULL || win->xwin == None)
return;
diff --git a/window.h b/window.h
index 63a6aab..f542374 100644
--- a/window.h
+++ b/window.h
@@ -76,6 +76,8 @@ void win_draw(win_t*);
void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int,
unsigned long);
+int win_textwidth(const char*, unsigned int, bool);
+
void win_set_title(win_t*, const char*);
void win_set_bar_info(win_t*, const char*, const char*);
void win_set_cursor(win_t*, cursor_t);