From 721f0e189741787de50645fed4d365b9088b269e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 15 Jul 2021 20:07:18 -0700 Subject: [PATCH] Cleanup --- bb.c | 6 ++++++ draw.c | 15 ++++++++------- utils.h | 7 ------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bb.c b/bb.c index 8a21d98..803acb1 100644 --- a/bb.c +++ b/bb.c @@ -34,6 +34,12 @@ #define BB_VERSION "0.30.0" #define MAX_BINDINGS 1024 #define SCROLLOFF MIN(5, (winsize.ws_row-4)/2) +#define ONSCREEN (winsize.ws_row - 3) + +#define new(t) memcheck(calloc(1, sizeof(t))) +#define xcalloc(a,b) memcheck(calloc(a,b)) +#define xrealloc(a,b) memcheck(realloc(a,b)) +#define clean_err(...) do { cleanup(); err(1, __VA_ARGS__); } while (0) // Functions void bb_browse(bb_t *bb, int argc, char *argv[]); diff --git a/draw.c b/draw.c index 01ed4f3..b27722c 100644 --- a/draw.c +++ b/draw.c @@ -293,6 +293,7 @@ void render(FILE *out, bb_t *bb) struct winsize winsize; ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize); + int onscreen = winsize.ws_row - 3; bb->dirty |= (winsize.ws_row != oldsize.ws_row) || (winsize.ws_col != oldsize.ws_col); @@ -337,9 +338,9 @@ void render(FILE *out, bb_t *bb) fputs("\033[37;2m ...no files here... \033[0m\033[J", out); } else { entry_t **files = bb->files; - for (int i = bb->scroll; i < bb->scroll + ONSCREEN && i < bb->nfiles; i++) { + for (int i = bb->scroll; i < bb->scroll + onscreen && i < bb->nfiles; i++) { if (!(bb->dirty || i == bb->cursor || i == lastcursor || - i < lastscroll || i >= lastscroll + ONSCREEN)) { + i < lastscroll || i >= lastscroll + onscreen)) { continue; } @@ -350,15 +351,15 @@ void render(FILE *out, bb_t *bb) move_cursor(out, x, y); draw_row(out, bb->columns, entry, color, winsize.ws_col-1); } - move_cursor(out, 0, MIN(bb->nfiles - bb->scroll, ONSCREEN) + 2); + move_cursor(out, 0, MIN(bb->nfiles - bb->scroll, onscreen) + 2); fputs("\033[J", out); } // Scrollbar: - if (bb->nfiles > ONSCREEN) { - int height = (ONSCREEN*ONSCREEN + (bb->nfiles-1))/bb->nfiles; - int start = 2 + (bb->scroll*ONSCREEN)/bb->nfiles; - for (int i = 2; i < 2 + ONSCREEN; i++) { + if (bb->nfiles > onscreen) { + int height = (onscreen*onscreen + (bb->nfiles-1))/bb->nfiles; + int start = 2 + (bb->scroll*onscreen)/bb->nfiles; + for (int i = 2; i < 2 + onscreen; i++) { move_cursor(out, winsize.ws_col-1, i); fprintf(out, "%s\033[0m", (i >= start && i < start + height) ? SCROLLBAR_FG : SCROLLBAR_BG); diff --git a/utils.h b/utils.h index 7fe1de9..93ffc01 100644 --- a/utils.h +++ b/utils.h @@ -17,16 +17,9 @@ #define streq(a,b) (strcmp(a,b)==0) #endif -#define new(t) memcheck(calloc(1, sizeof(t))) -#define xcalloc(a,b) memcheck(calloc(a,b)) -#define xrealloc(a,b) memcheck(realloc(a,b)) -#define clean_err(...) do { cleanup(); err(1, __VA_ARGS__); } while (0) - #define MAX(a,b) ((a) < (b) ? (b) : (a)) #define MIN(a,b) ((a) > (b) ? (b) : (a)) -#define ONSCREEN (winsize.ws_row - 3) - // Platform-dependent time strucutre accessors: #ifdef __APPLE__ #define mtime(s) (s).st_mtimespec