11 #include <sys/ioctl.h>
13 #if defined(USE_TERMINFO)
16 #elif defined(USE_TERMCAP)
20 #include "multiprogress.h"
22 static FILE *dup_stream(int fd)
27 newfd = dup(fd); if (newfd < 0) return (0);
28 fp = fdopen(newfd, "r+"); if (!fp) return (0);
32 int progress_init(struct progress_state *progress)
40 struct progress_ttyinfo *tty;
46 tty->termbuf = tty->capbuf = 0;
48 tty->cap.cr = tty->cap.up = tty->cap.ce = tty->cap.cd =
49 tty->cap.mr = tty->cap.md = tty->cap.me =
50 tty->cap.af = tty->cap.ab = tty->cap.op = 0;
52 progress->items = progress->end_item = 0;
53 progress->nitems = 0; progress->last_lines = 0;
54 progress->tv_update.tv_sec = 0; progress->tv_update.tv_usec = 0;
56 if (isatty(1)) tty->fp = dup_stream(1);
57 else if (isatty(2)) tty->fp = dup_stream(2);
58 else tty->fp = fopen("/dev/tty", "r+");
59 if (!tty->fp) return (-1);
61 #define SETDIM(dim, var, getcap, dflt) do { \
62 t = getenv(var); if (t) { n = atoi(t); if (n) { tty->dim = n; break; } } \
63 n = getcap; if (n > 0) { tty->dim = n; break; } \
67 #if defined(USE_TERMINFO)
69 if (setupterm(0, fileno(tty->fp), &err) != OK || err < 1) return (-1);
71 tty->cap.cr = tigetstr("cr");
72 tty->cap.up = tigetstr("cuu1");
73 tty->cap.ce = tigetstr("el");
74 tty->cap.cd = tigetstr("ed");
76 if (tigetnum("xmc") < 1) {
77 tty->cap.mr = tigetstr("rev");
78 tty->cap.md = tigetstr("bold");
79 tty->cap.me = tigetstr("sgr0");
81 tty->cap.af = tigetstr("setaf");
82 tty->cap.ab = tigetstr("setab");
83 tty->cap.op = tigetstr("op");
86 if (tigetflag("bce") > 0) tty->cap.f |= TCF_BCE;
88 SETDIM(defwd, "COLUMNS", tigetnum("co"), 80);
89 SETDIM(defht, "LINES", tigetnum("li"), 25);
91 #elif defined(USE_TERMCAP)
93 term = getenv("TERM"); if (!term) return (-1);
94 if (tgetent(tty->termbuf, term) < 1) return (-1);
96 tty->termbuf = malloc(4096); if (!tty->termbuf) return (-1);
97 tty->capbuf = malloc(4096); if (!tty->capbuf) return (-1);
100 tty->cap.cr = tgetstr("cr", &capcur);
101 tty->cap.up = tgetstr("up", &capcur);
102 tty->cap.ce = tgetstr("ce", &capcur);
103 tty->cap.cd = tgetstr("cd", &capcur);
105 if (tgetnum("sg") < 1) {
106 tty->cap.mr = tgetstr("mr", &capcur);
107 tty->cap.md = tgetstr("md", &capcur);
108 tty->cap.me = tgetstr("me", &capcur);
110 tty->cap.af = tgetstr("AF", &capcur);
111 tty->cap.ab = tgetstr("AB", &capcur);
112 tty->cap.op = tgetstr("op", &capcur);
115 if (tgetflag("ut") > 0) tty->cap.f |= TCF_BCE;
117 t = tgetstr("pc", &capcur); PC = t ? *t : 0;
119 SETDIM(defwd, "COLUMNS", tgetnum("co"), 80);
120 SETDIM(defht, "LINES", tgetnum("li"), 25);
124 SETDIM(defwd, "COLUMNS", -1, 80);
125 SETDIM(defht, "LINES", -1, 25);
131 if (!tty->cap.cr) tty->cap.cr = "\r";
132 if (!tty->cap.up || !tty->cap.ce || !tty->cap.cd)
133 { fclose(tty->fp); tty->fp = 0; return (-1); }
134 if (!tty->cap.af || !tty->cap.ab || !tty->cap.op) tty->cap.op = 0;
135 if (!tty->cap.me) tty->cap.mr = tty->cap.md = 0;
139 void progress_free(struct progress_state *progress)
141 struct progress_ttyinfo *tty = &progress->tty;
143 if (tty->fp) { fclose(tty->fp); tty->fp = 0; }
144 free(tty->termbuf); free(tty->capbuf); tty->termbuf = tty->capbuf = 0;
147 #if defined(USE_TERMINFO)
148 static const struct progress_ttyinfo *curtty = 0;
149 static int putty(int ch) { return (putc(ch, curtty->fp)); }
150 static void put_sequence(const struct progress_ttyinfo *tty,
151 const char *p, unsigned nlines)
152 { if (p) { curtty = tty; tputs(p, nlines, putty); } }
153 static void set_fgcolour(const struct progress_ttyinfo *tty, int colour)
154 { put_sequence(tty, tgoto(tty->cap.af, -1, colour), 1); }
155 static void set_bgcolour(const struct progress_ttyinfo *tty, int colour)
156 { put_sequence(tty, tgoto(tty->cap.ab, -1, colour), 1); }
157 #elif defined(USE_TERMCAP)
158 static const struct progress_ttyinfo *curtty = 0;
159 static int putty(int ch) { return (putc(ch, curtty->fp)); }
160 static void put_sequence(const struct progress_ttyinfo *tty,
161 const char *p, unsigned nlines)
162 { if (p) { curtty = tty; tputs(p, nlines, putty); } }
163 static void set_fgcolour(const struct progress_ttyinfo *tty, int colour)
164 { put_sequence(tty, tgoto(tty->cap.af, -1, colour), 1); }
165 static void set_bgcolour(const struct progress_ttyinfo *tty, int colour)
166 { put_sequence(tty, tgoto(tty->cap.ab, -1, colour), 1); }
168 static void put_sequence(const struct progress_ttyinfo *tty,
169 const char *p, unsigned nlines) { ; }
170 static void set_fgcolour(const struct progress_ttyinfo *tty, int colour)
172 static void set_bgcolour(const struct progress_ttyinfo *tty, int colour)
177 static int clear_progress(struct progress_state *progress,
178 struct progress_render_state *render, unsigned f)
180 const struct progress_ttyinfo *tty = &progress->tty;
181 unsigned ndel, nleave;
184 if (!tty->fp) return (-1);
186 put_sequence(tty, tty->cap.cr, 1);
187 if (progress->last_lines) {
189 { ndel = progress->last_lines; nleave = 0; }
191 if (progress->nitems >= progress->last_lines) ndel = 0;
192 else ndel = progress->last_lines - progress->nitems;
193 nleave = progress->last_lines - ndel;
196 for (i = 0; i < nleave - 1; i++) put_sequence(tty, tty->cap.up, 1);
198 for (i = 0; i < ndel - 1; i++) put_sequence(tty, tty->cap.up, 1);
199 put_sequence(tty, tty->cap.cd, ndel);
200 for (i = 0; i < nleave; i++) put_sequence(tty, tty->cap.up, 1);
203 progress->last_lines = 0;
204 if (ferror(tty->fp)) return (-1);
208 static int grow_linebuf(struct progress_render_state *render, size_t want)
210 char *newbuf; size_t newsz;
212 if (want <= render->linesz) return (0);
213 if (!render->linesz) newsz = 4*render->width + 1;
214 else newsz = render->linesz;
215 while (newsz < want) newsz *= 2;
216 newbuf = malloc(newsz + 1); if (!newbuf) return (-1);
219 memcpy(newbuf, render->linebuf, render->leftsz);
221 memcpy(newbuf + newsz - render->rightsz,
222 render->linebuf + render->linesz - render->rightsz,
224 free(render->linebuf); render->linebuf = newbuf; render->linesz = newsz;
228 static int grow_tempbuf(struct progress_render_state *render, size_t want)
230 char *newbuf; size_t newsz;
232 if (want <= render->tempsz) return (0);
233 if (!render->tempsz) newsz = 4*render->width + 1;
234 else newsz = render->tempsz;
235 while (newsz < want) newsz *= 2;
236 newbuf = malloc(newsz + 1); if (!newbuf) return (-1);
238 if (render->tempsz) memcpy(newbuf, render->tempbuf, render->tempsz);
239 free(render->tempbuf); render->tempbuf = newbuf; render->tempsz = newsz;
243 static int setup_render_state(struct progress_state *progress,
244 struct progress_render_state *render)
246 const struct progress_ttyinfo *tty = &progress->tty;
251 render->linebuf = 0; render->linesz = 0;
252 render->tempbuf = 0; render->tempsz = 0;
255 render->old_bc = BC; BC = 0;
256 render->old_up = UP; UP = 0;
259 if (!ioctl(fileno(tty->fp), TIOCGWINSZ, &wsz))
260 { render->width = wsz.ws_col; render->height = wsz.ws_row; }
262 { render->width = tty->defwd; render->height = tty->defht; rc = -1; }
264 if (render->width && !tty->cap.op && !tty->cap.mr) render->width--;
269 static void free_render_state(struct progress_render_state *render)
271 fflush(render->tty->fp);
272 free(render->linebuf); render->linebuf = 0; render->linesz = 0;
273 free(render->tempbuf); render->tempbuf = 0; render->tempsz = 0;
280 #define CONV_MORE ((size_t)-2)
281 #define CONV_BAD ((size_t)-1)
285 const char *p; size_t i, sz;
289 static void init_measure(struct measure *m, const char *p, size_t sz)
291 m->p = p; m->sz = sz; m->i = 0; m->wd = 0;
292 memset(&m->ps, 0, sizeof(m->ps));
295 static int advance_measure(struct measure *m)
301 n = mbrtowc(&wch, m->p + m->i, m->sz - m->i, &m->ps);
302 if (!n) { chwd = 0; n = m->sz - m->i; }
303 else if (n == CONV_MORE) { chwd = 2; n = m->sz - m->i; }
304 else if (n == CONV_BAD) { chwd = 2; n = 1; }
305 else chwd = wcwidth(wch);
307 m->i += n; m->wd += chwd;
308 return (m->i < m->sz);
311 static unsigned string_width(const char *p, size_t sz)
315 init_measure(&m, p, sz);
316 while (advance_measure(&m));
320 static size_t split_string(const char *p, size_t sz,
321 unsigned *wd_out, unsigned maxwd)
324 size_t lasti; unsigned lastwd;
327 init_measure(&m, p, sz);
329 lasti = m.i; lastwd = m.wd;
330 more = advance_measure(&m);
331 if (m.wd > maxwd) { *wd_out = lastwd; return (lasti); }
332 else if (!more) { *wd_out = m.wd; return (sz); }
336 enum { LEFT, RIGHT };
337 static int putstr(struct progress_render_state *render, unsigned side,
338 const char *p, size_t n)
340 unsigned newwd = string_width(p, n);
343 if (newwd >= render->width - render->leftwd - render->rightwd) return (-1);
344 want = render->leftsz + render->rightsz + n;
345 if (want > render->linesz && grow_linebuf(render, want)) return (-1);
348 memcpy(render->linebuf + render->leftsz, p, n);
349 render->leftsz += n; render->leftwd += newwd;
352 memcpy(render->linebuf + render->linesz - render->rightsz - n, p, n);
353 render->rightsz += n; render->rightwd += newwd;
361 static int vputf(struct progress_render_state *render, unsigned side,
362 const char *fmt, va_list ap)
367 if (!render->tempsz && grow_tempbuf(render, 2*strlen(fmt))) return (-1);
370 rc = vsnprintf(render->tempbuf, render->tempsz, fmt, bp);
372 if (rc < 0) return (-1);
373 if (rc <= render->tempsz) break;
374 if (grow_tempbuf(render, 2*(rc + 1))) return (-1);
376 if (putstr(render, side, render->tempbuf, rc)) return (-1);
380 int progress_vputleft(struct progress_render_state *render,
381 const char *fmt, va_list ap)
382 { return (vputf(render, LEFT, fmt, ap)); }
384 int progress_vputright(struct progress_render_state *render,
385 const char *fmt, va_list ap)
386 { return (vputf(render, RIGHT, fmt, ap)); }
388 int progress_putleft(struct progress_render_state *render,
389 const char *fmt, ...)
394 va_start(ap, fmt); rc = vputf(render, LEFT, fmt, ap); va_end(ap);
398 int progress_putright(struct progress_render_state *render,
399 const char *fmt, ...)
404 va_start(ap, fmt); rc = vputf(render, RIGHT, fmt, ap); va_end(ap);
417 const struct progress_render_state *render;
418 unsigned pos, nextpos, state;
421 static void advance_bar_state(struct bar_state *bar)
423 const struct progress_render_state *render = bar->render;
424 const struct progress_ttyinfo *tty = render->tty;
425 size_t here = bar->nextpos;
427 while (bar->nextpos == here) {
428 switch (bar->state) {
429 case LEFT_COLOUR: set_bgcolour(tty, 3); goto right;
430 case LEFT_MONO: put_sequence(tty, tty->cap.me, 1); goto right;
431 case LEFT_SIMPLE: putc('|', tty->fp); goto right;
432 right: bar->state = RIGHT_ANY; bar->nextpos = render->width; break;
433 case RIGHT_ANY: bar->state = STOP; bar->nextpos = UINT_MAX; break;
438 static void put_str(FILE *fp, const char *p, size_t sz)
439 { while (sz--) putc(*p++, fp); }
440 static void put_spc(FILE *fp, unsigned n)
441 { while (n--) putc(' ', fp); }
443 static void put_barstr(struct bar_state *bar, const char *p, size_t sz)
449 n = split_string(p, sz, &wd, bar->nextpos - bar->pos);
450 if (n == sz && wd < bar->nextpos - bar->pos) break;
451 put_str(bar->render->tty->fp, p, n); bar->pos += wd;
452 advance_bar_state(bar);
455 put_str(bar->render->tty->fp, p, sz); bar->pos += wd;
458 static void put_barspc(struct bar_state *bar, unsigned n)
463 step = bar->nextpos - bar->pos;
465 put_spc(bar->render->tty->fp, step); bar->pos += step;
466 advance_bar_state(bar);
469 put_spc(bar->render->tty->fp, n); bar->pos += n;
472 int progress_showbar(struct progress_render_state *render, double frac)
474 const struct progress_ttyinfo *tty = render->tty;
475 struct bar_state bar;
477 if (!tty->fp) return (-1);
479 bar.render = render; bar.pos = 0; bar.nextpos = frac*render->width + 0.5;
482 set_fgcolour(tty, 0); bar.state = LEFT_COLOUR;
483 if (bar.nextpos) set_bgcolour(tty, 2);
484 else advance_bar_state(&bar);
485 } else if (tty->cap.mr) {
487 { bar.state = LEFT_MONO; put_sequence(tty, tty->cap.mr, 1); }
489 { bar.state = RIGHT; bar.nextpos = render->width; }
491 bar.state = LEFT_SIMPLE;
493 put_barstr(&bar, render->linebuf, render->leftsz);
494 put_barspc(&bar, render->width - render->leftwd - render->rightwd);
496 render->linebuf + render->linesz - render->rightsz,
499 put_sequence(tty, tty->cap.me, 1);
500 put_sequence(tty, tty->cap.op, 1);
505 int progress_shownotice(struct progress_render_state *render, int bg, int fg)
507 const struct progress_ttyinfo *tty = render->tty;
509 if (!tty->fp) return (-1);
511 if (tty->cap.op) { set_fgcolour(tty, fg); set_bgcolour(tty, bg); }
512 else if (tty->cap.mr) put_sequence(tty, tty->cap.mr, 1);
513 if (tty->cap.md) put_sequence(tty, tty->cap.md, 1);
515 put_str(tty->fp, render->linebuf, render->leftsz);
516 if (!render->rightsz && (tty->cap.f&TCF_BCE) && tty->cap.ce)
517 put_sequence(tty, tty->cap.ce, 1);
519 put_spc(tty->fp, render->width - render->leftwd - render->rightwd);
521 render->linebuf + render->linesz - render->rightsz,
525 put_sequence(tty, tty->cap.me, 1);
526 put_sequence(tty, tty->cap.op, 1);
531 int progress_additem(struct progress_state *progress,
532 struct progress_item *item)
534 if (item->parent) return (-1);
535 item->prev = progress->end_item; item->next = 0;
536 if (progress->end_item) progress->end_item->next = item;
537 else progress->items = item;
538 progress->end_item = item; item->parent = progress;
544 int progress_clear(struct progress_state *progress)
546 struct progress_render_state render;
548 if (!progress->tty.fp) return (-1);
549 if (setup_render_state(progress, &render)) return (-1);
550 clear_progress(progress, &render, CLRF_ALL);
551 free_render_state(&render);
555 int progress_update(struct progress_state *progress)
557 struct progress_render_state render;
558 struct progress_item *item;
562 if (!progress->tty.fp) return (-1);
563 if (setup_render_state(progress, &render)) return (-1);
564 clear_progress(progress, &render, 0);
566 for (item = progress->items; item; item = item->next) {
567 if (f&f_any) fputs("\r\n", progress->tty.fp);
568 render.leftsz = render.rightsz = 0;
569 render.leftwd = render.rightwd = 0;
570 item->render(item, &render); progress->last_lines++; f |= f_any;
571 if (progress->last_lines > render.height) break;
573 free_render_state(&render);
577 int progress_removeitem(struct progress_state *progress,
578 struct progress_item *item)
580 if (!item->parent) return (-1);
581 if (item->next) item->next->prev = item->prev;
582 else (progress->end_item) = item->prev;
583 if (item->prev) item->prev->next = item->next;
584 else (progress->items) = item->next;
585 progress->nitems--; item->parent = 0;