| @@ -21,6 +21,7 @@ |
| 21 | 21 | | |
| 22 | 22 | | #include <stdlib.h> |
| 23 | 23 | | #include <stdio.h> |
| 24 | + | #include <stdarg.h> |
| 24 | 25 | | #include <string.h> |
| 25 | 26 | | #include <fcntl.h> |
| 26 | 27 | | #include <unistd.h> |
| @@ -256,7 +257,7 @@ void open_info(void) |
| 256 | 257 | | kill(pid, SIGTERM); |
| 257 | 258 | | info.fd = -1; |
| 258 | 259 | | } |
| 259 | | − | win.bar.l[0] = '\0'; |
| 260 | + | win.bar.l.buf[0] = '\0'; |
| 260 | 261 | | |
| 261 | 262 | | if (pipe(pfd) < 0) |
| 262 | 263 | | return; |
| @@ -290,20 +291,20 @@ void read_info(void) |
| 290 | 291 | | for (i = 0; i < n; i++) { |
| 291 | 292 | | if (buf[i] == '\n') { |
| 292 | 293 | | if (info.lastsep == 0) { |
| 293 | | − | win.bar.l[info.i++] = ' '; |
| 294 | + | win.bar.l.buf[info.i++] = ' '; |
| 294 | 295 | | info.lastsep = 1; |
| 295 | 296 | | } |
| 296 | 297 | | } else { |
| 297 | | − | win.bar.l[info.i++] = buf[i]; |
| 298 | + | win.bar.l.buf[info.i++] = buf[i]; |
| 298 | 299 | | info.lastsep = 0; |
| 299 | 300 | | } |
| 300 | | − | if (info.i + 1 == sizeof(win.bar.l)) |
| 301 | + | if (info.i + 1 == win.bar.l.size) |
| 301 | 302 | | goto end; |
| 302 | 303 | | } |
| 303 | 304 | | } |
| 304 | 305 | | end: |
| 305 | 306 | | info.i -= info.lastsep; |
| 306 | | − | win.bar.l[info.i] = '\0'; |
| 307 | + | win.bar.l.buf[info.i] = '\0'; |
| 307 | 308 | | win_draw(&win); |
| 308 | 309 | | close(info.fd); |
| 309 | 310 | | info.fd = -1; |
| @@ -343,15 +344,24 @@ void load_image(int new) |
| 343 | 344 | | reset_timeout(animate); |
| 344 | 345 | | } |
| 345 | 346 | | |
| 347 | + | void bar_put(win_bar_t *bar, const char *fmt, ...) |
| 348 | + | { |
| 349 | + | size_t len = bar->size - (bar->p - bar->buf), n; |
| 350 | + | va_list ap; |
| 351 | + | |
| 352 | + | va_start(ap, fmt); |
| 353 | + | n = vsnprintf(bar->p, len, fmt, ap); |
| 354 | + | bar->p += MIN(len, n); |
| 355 | + | va_end(ap); |
| 356 | + | } |
| 357 | + | |
| 346 | 358 | | void update_info(void) |
| 347 | 359 | | { |
| 348 | | − | unsigned int i, fn, fw, n; |
| 349 | | − | unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r); |
| 350 | | − | char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN]; |
| 360 | + | unsigned int i, fn, fw; |
| 361 | + | char title[TITLE_LEN]; |
| 351 | 362 | | const char * mark; |
| 352 | 363 | | bool ow_info; |
| 353 | | − | |
| 354 | | − | for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); |
| 364 | + | win_bar_t *l = &win.bar.l, *r = &win.bar.r; |
| 355 | 365 | | |
| 356 | 366 | | /* update window title */ |
| 357 | 367 | | if (mode == MODE_THUMB) { |
| @@ -364,39 +374,41 @@ void update_info(void) |
| 364 | 374 | | /* update bar contents */ |
| 365 | 375 | | if (win.bar.h == 0) |
| 366 | 376 | | return; |
| 377 | + | for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); |
| 367 | 378 | | mark = files[fileidx].marked ? "* " : ""; |
| 379 | + | l->p = l->buf; |
| 380 | + | r->p = r->buf; |
| 368 | 381 | | if (mode == MODE_THUMB) { |
| 369 | 382 | | if (tns.loadnext < tns.end) { |
| 370 | | − | snprintf(lt, llen, "Loading... %0*d", fw, tns.loadnext); |
| 383 | + | bar_put(l, "Loading... %0*d", fw, MAX(tns.loadnext, 1)); |
| 371 | 384 | | ow_info = false; |
| 372 | 385 | | } else { |
| 373 | 386 | | ow_info = true; |
| 374 | 387 | | } |
| 375 | | − | n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt); |
| 388 | + | bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt); |
| 376 | 389 | | } else { |
| 377 | | − | n = snprintf(rt, rlen, "%s", mark); |
| 390 | + | bar_put(r, "%s", mark); |
| 378 | 391 | | if (img.ss.on) |
| 379 | | − | n += snprintf(rt + n, rlen - n, "%ds | ", img.ss.delay); |
| 392 | + | bar_put(r, "%ds | ", img.ss.delay); |
| 380 | 393 | | if (img.gamma != 0) |
| 381 | | − | n += snprintf(rt + n, rlen - n, "G%+d | ", img.gamma); |
| 382 | | − | n += snprintf(rt + n, rlen - n, "%3d%% | ", (int) (img.zoom * 100.0)); |
| 394 | + | bar_put(r, "G%+d | ", img.gamma); |
| 395 | + | bar_put(r, "%3d%% | ", (int) (img.zoom * 100.0)); |
| 383 | 396 | | if (img.multi.cnt > 0) { |
| 384 | 397 | | for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10); |
| 385 | | − | n += snprintf(rt + n, rlen - n, "%0*d/%d | ", |
| 386 | | − | fn, img.multi.sel + 1, img.multi.cnt); |
| 398 | + | bar_put(r, "%0*d/%d | ", fn, img.multi.sel + 1, img.multi.cnt); |
| 387 | 399 | | } |
| 388 | | − | n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, fileidx + 1, filecnt); |
| 400 | + | bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt); |
| 389 | 401 | | ow_info = info.cmd == NULL; |
| 390 | 402 | | } |
| 391 | 403 | | if (ow_info) { |
| 392 | 404 | | fn = strlen(files[fileidx].name); |
| 393 | | − | if (fn < llen && |
| 405 | + | if (fn < l->size && |
| 394 | 406 | | win_textwidth(files[fileidx].name, fn, true) + |
| 395 | | − | win_textwidth(rt, n, true) < win.w) |
| 407 | + | win_textwidth(r->buf, r->p - r->buf, true) < win.w) |
| 396 | 408 | | { |
| 397 | | − | strncpy(lt, files[fileidx].name, llen); |
| 409 | + | strncpy(l->buf, files[fileidx].name, l->size); |
| 398 | 410 | | } else { |
| 399 | | − | strncpy(lt, files[fileidx].base, llen); |
| 411 | + | strncpy(l->buf, files[fileidx].base, l->size); |
| 400 | 412 | | } |
| 401 | 413 | | } |
| 402 | 414 | | } |
| @@ -469,7 +481,7 @@ void run_key_handler(const char *key, unsigned int mask) |
| 469 | 481 | | int i, j, retval, status; |
| 470 | 482 | | int fcnt = mode == MODE_THUMB && markcnt > 0 ? markcnt : 1; |
| 471 | 483 | | bool changed = false; |
| 472 | | − | char **args, kstr[32], oldbar[sizeof(win.bar.l)]; |
| 484 | + | char **args, kstr[32], oldbar[BAR_L_LEN]; |
| 473 | 485 | | struct stat *oldst, newst; |
| 474 | 486 | | struct { int fn; struct stat st; } *finfo; |
| 475 | 487 | | |
| @@ -504,8 +516,8 @@ void run_key_handler(const char *key, unsigned int mask) |
| 504 | 516 | | mask & Mod1Mask ? "M-" : "", |
| 505 | 517 | | mask & ShiftMask ? "S-" : "", key); |
| 506 | 518 | | |
| 507 | | − | memcpy(oldbar, win.bar.l, sizeof(win.bar.l)); |
| 508 | | − | strncpy(win.bar.l, "Running key handler...", sizeof(win.bar.l)); |
| 519 | + | memcpy(oldbar, win.bar.l.buf, sizeof(oldbar)); |
| 520 | + | strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size); |
| 509 | 521 | | win_draw(&win); |
| 510 | 522 | | win_set_cursor(&win, CURSOR_WATCH); |
| 511 | 523 | | |
| @@ -540,7 +552,7 @@ end: |
| 540 | 552 | | img_close(&img, true); |
| 541 | 553 | | load_image(fileidx); |
| 542 | 554 | | } else if (info.cmd != NULL) { |
| 543 | | − | memcpy(win.bar.l, oldbar, sizeof(win.bar.l)); |
| 555 | + | memcpy(win.bar.l.buf, oldbar, win.bar.l.size); |
| 544 | 556 | | } |
| 545 | 557 | | } |
| 546 | 558 | | reset_cursor(); |