From c45831b0417db18e420cfcfc17ee5182bbfafe5e Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 25 Mar 2022 01:23:51 +0000 Subject: [PATCH] multiprogress.[ch]: Publish the terminal-sequence output functions. Otherwise externally-defined renderers don't have a good way to do this. --- multiprogress.c | 78 +++++++++++++++++++++++++++++++-------------------------- multiprogress.h | 8 ++++++ 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/multiprogress.c b/multiprogress.c index 4d4ae3e..d8da674 100644 --- a/multiprogress.c +++ b/multiprogress.c @@ -381,29 +381,29 @@ int progress_putright(struct progress_render_state *render, #if defined(USE_TERMINFO) static const struct progress_ttyinfo *curtty = 0; static int putty(int ch) { return (putc(ch, curtty->fp)); } -static void put_sequence(const struct progress_ttyinfo *tty, - const char *p, unsigned nlines) +void progress_put_sequence(const struct progress_ttyinfo *tty, + const char *p, unsigned nlines) { if (p) { curtty = tty; tputs(p, nlines, putty); } } -static void set_fgcolour(const struct progress_ttyinfo *tty, int colour) - { put_sequence(tty, tgoto(tty->cap.af, -1, colour), 1); } -static void set_bgcolour(const struct progress_ttyinfo *tty, int colour) - { put_sequence(tty, tgoto(tty->cap.ab, -1, colour), 1); } +void progress_set_fgcolour(const struct progress_ttyinfo *tty, int colour) + { progress_put_sequence(tty, tgoto(tty->cap.af, -1, colour), 1); } +void progress_set_bgcolour(const struct progress_ttyinfo *tty, int colour) + { progress_put_sequence(tty, tgoto(tty->cap.ab, -1, colour), 1); } #elif defined(USE_TERMCAP) static const struct progress_ttyinfo *curtty = 0; static int putty(int ch) { return (putc(ch, curtty->fp)); } -static void put_sequence(const struct progress_ttyinfo *tty, - const char *p, unsigned nlines) +void progress_put_sequence(const struct progress_ttyinfo *tty, + const char *p, unsigned nlines) { if (p) { curtty = tty; tputs(p, nlines, putty); } } -static void set_fgcolour(const struct progress_ttyinfo *tty, int colour) - { put_sequence(tty, tgoto(tty->cap.af, -1, colour), 1); } -static void set_bgcolour(const struct progress_ttyinfo *tty, int colour) - { put_sequence(tty, tgoto(tty->cap.ab, -1, colour), 1); } +void progress_set_fgcolour(const struct progress_ttyinfo *tty, int colour) + { progress_put_sequence(tty, tgoto(tty->cap.af, -1, colour), 1); } +void progress_set_bgcolour(const struct progress_ttyinfo *tty, int colour) + { progress_put_sequence(tty, tgoto(tty->cap.ab, -1, colour), 1); } #else -static void put_sequence(const struct progress_ttyinfo *tty, - const char *p, unsigned nlines) { ; } -static void set_fgcolour(const struct progress_ttyinfo *tty, int colour) +void progress_put_sequence(const struct progress_ttyinfo *tty, + const char *p, unsigned nlines) { ; } +void progress_set_fgcolour(const struct progress_ttyinfo *tty, int colour) { ; } -static void set_bgcolour(const struct progress_ttyinfo *tty, int colour) +void progress_set_bgcolour(const struct progress_ttyinfo *tty, int colour) { ; } #endif @@ -417,7 +417,7 @@ static int clear_progress(struct progress_state *progress, if (!tty->fp) return (-1); - put_sequence(tty, tty->cap.cr, 1); + progress_put_sequence(tty, tty->cap.cr, 1); if (progress->last_lines) { if (f&CLRF_ALL) { ndel = progress->last_lines; nleave = 0; } @@ -427,11 +427,14 @@ static int clear_progress(struct progress_state *progress, nleave = progress->last_lines - ndel; } if (!ndel) - for (i = 1; i < nleave; i++) put_sequence(tty, tty->cap.up, 1); + for (i = 1; i < nleave; i++) + progress_put_sequence(tty, tty->cap.up, 1); else { - for (i = 1; i < ndel; i++) put_sequence(tty, tty->cap.up, 1); - put_sequence(tty, tty->cap.cd, ndel); - for (i = 0; i < nleave; i++) put_sequence(tty, tty->cap.up, 1); + for (i = 1; i < ndel; i++) + progress_put_sequence(tty, tty->cap.up, 1); + progress_put_sequence(tty, tty->cap.cd, ndel); + for (i = 0; i < nleave; i++) + progress_put_sequence(tty, tty->cap.up, 1); } } progress->last_lines = 0; @@ -463,13 +466,13 @@ int progress_update(struct progress_state *progress) clear_progress(progress, &render, 0); for (item = progress->items; item; item = item->next) { - if (f&f_any) put_sequence(tty, tty->cap.nw, 1); + if (f&f_any) progress_put_sequence(tty, tty->cap.nw, 1); render.leftsz = render.rightsz = 0; render.leftwd = render.rightwd = 0; item->render(item, &render); progress->last_lines++; f |= f_any; if (progress->last_lines > render.height) break; } - if (f&f_any) put_sequence(tty, tty->cap.cr, 1); + if (f&f_any) progress_put_sequence(tty, tty->cap.cr, 1); free_render_state(&render); return (0); } @@ -495,8 +498,8 @@ static void advance_bar_state(struct bar_state *bar) while (bar->nextpos <= here) { switch (bar->state) { - case LEFT_COLOUR: set_bgcolour(tty, 3); goto right; - case LEFT_MONO: put_sequence(tty, tty->cap.me, 1); goto right; + case LEFT_COLOUR: progress_set_bgcolour(tty, 3); goto right; + case LEFT_MONO: progress_put_sequence(tty, tty->cap.me, 1); goto right; case LEFT_SIMPLE: putc('|', tty->fp); goto right; right: bar->state = RIGHT_ANY; bar->nextpos = render->width; break; case RIGHT_ANY: bar->state = STOP; bar->nextpos = UINT_MAX; break; @@ -548,12 +551,12 @@ int progress_showbar(struct progress_render_state *render, double frac) bar.render = render; bar.pos = 0; bar.nextpos = frac*render->width + 0.5; if (tty->cap.op) { - set_fgcolour(tty, 0); bar.state = LEFT_COLOUR; - if (bar.nextpos) set_bgcolour(tty, 2); + progress_set_fgcolour(tty, 0); bar.state = LEFT_COLOUR; + if (bar.nextpos) progress_set_bgcolour(tty, 2); else advance_bar_state(&bar); } else if (tty->cap.mr) { if (bar.nextpos) - { bar.state = LEFT_MONO; put_sequence(tty, tty->cap.mr, 1); } + { bar.state = LEFT_MONO; progress_put_sequence(tty, tty->cap.mr, 1); } else { bar.state = RIGHT; bar.nextpos = render->width; } } else @@ -565,8 +568,8 @@ int progress_showbar(struct progress_render_state *render, double frac) render->linebuf + render->linesz - render->rightsz, render->rightsz); - put_sequence(tty, tty->cap.me, 1); - put_sequence(tty, tty->cap.op, 1); + progress_put_sequence(tty, tty->cap.me, 1); + progress_put_sequence(tty, tty->cap.op, 1); return (0); } @@ -577,13 +580,16 @@ int progress_shownotice(struct progress_render_state *render, int bg, int fg) if (!tty->fp) return (-1); - if (tty->cap.op) { set_fgcolour(tty, fg); set_bgcolour(tty, bg); } - else if (tty->cap.mr) put_sequence(tty, tty->cap.mr, 1); - if (tty->cap.md) put_sequence(tty, tty->cap.md, 1); + if (tty->cap.op) + { progress_set_fgcolour(tty, fg); progress_set_bgcolour(tty, bg); } + else if (tty->cap.mr) + progress_put_sequence(tty, tty->cap.mr, 1); + if (tty->cap.md) + progress_put_sequence(tty, tty->cap.md, 1); put_str(tty->fp, render->linebuf, render->leftsz); if (!render->rightsz && (tty->cap.f&TCF_BCE) && tty->cap.ce) - put_sequence(tty, tty->cap.ce, 1); + progress_put_sequence(tty, tty->cap.ce, 1); else { put_spc(tty->fp, render->width - render->leftwd - render->rightwd); put_str(tty->fp, @@ -591,8 +597,8 @@ int progress_shownotice(struct progress_render_state *render, int bg, int fg) render->rightsz); } - put_sequence(tty, tty->cap.me, 1); - put_sequence(tty, tty->cap.op, 1); + progress_put_sequence(tty, tty->cap.me, 1); + progress_put_sequence(tty, tty->cap.op, 1); return (0); } diff --git a/multiprogress.h b/multiprogress.h index f5147c4..5969801 100644 --- a/multiprogress.h +++ b/multiprogress.h @@ -74,6 +74,14 @@ __attribute__((format(printf, 2, 3))) extern int progress_putright(struct progress_render_state */*render*/, const char */*fmt*/, ...); +extern void progress_put_sequence(const struct progress_ttyinfo */*tty*/, + const char */*p*/, unsigned /*nlines*/); + +extern void progress_set_fgcolour(const struct progress_ttyinfo */*tty*/, + int /*colour*/); +extern void progress_set_bgcolour(const struct progress_ttyinfo */*tty*/, + int /*colour*/); + extern int progress_showbar(struct progress_render_state */*render*/, double /*frac*/); -- 2.11.0