Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/dvdrip
[dvdrip] / multiprogress.h
1 #ifndef MULTIPROGRESS_H
2 #define MULTIPROGRESS_H
3
4 #include <stdio.h>
5 #include <sys/time.h>
6
7 struct progress_ttyinfo {
8 FILE *fp; /* terminal stream */
9 char *termbuf, *capbuf; /* buffers for termcap */
10 struct { /* terminal capabilities */
11 unsigned f; /* various flags */
12 #define TCF_BCE 1u /* erases to background colour */
13 const char *cr, *nw, *up, *ce, *cd; /* cursor motion */
14 const char *mr, *md, *me; /* reverse video, bold */
15 const char *af, *ab, *op; /* colour */
16 } cap;
17 unsigned defwd, defht; /* default width and height */
18 };
19 #define PROGRESS_TTYINFO_INIT { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
20
21 struct progress_state {
22 struct progress_ttyinfo tty; /* terminal state */
23 struct progress_item *items, *end_item; /* list of progress items */
24 unsigned nitems; /* number of items */
25 unsigned last_lines; /* number written last time */
26 struct timeval tv_update; /* last update time */
27 };
28 #define PROGRESS_STATE_INIT { PROGRESS_TTYINFO_INIT, 0, 0, 0, 0, { 0, 0 } }
29
30 struct progress_render_state {
31 const struct progress_ttyinfo *tty; /* terminal state */
32 unsigned width, height; /* terminal size, in characters */
33 char *linebuf; size_t linesz; /* output buffer */
34 char *tempbuf; size_t tempsz; /* scratch buffer */
35 size_t leftsz, rightsz; /* left and right cursors */
36 unsigned leftwd, rightwd; /* left and right widths */
37 char *old_bc, *old_up; /* old fixup strings */
38 };
39
40 struct progress_item {
41 struct progress_state *parent; /* controlling progress state */
42 struct progress_item *next, *prev; /* forward and backward links */
43 void (*render)(struct progress_item */*item*/, /* render function */
44 struct progress_render_state */*rs*/);
45 };
46 #define PROGRESS_ITEM_INIT { 0, 0, 0, 0 }
47
48 extern int progress_init(struct progress_state */*progress*/);
49 extern void progress_free(struct progress_state */*progress*/);
50
51 extern int progress_clear(struct progress_state */*progress*/);
52
53 extern int progress_update(struct progress_state */*progress*/);
54
55 extern int progress_additem(struct progress_state */*progress*/,
56 struct progress_item */*item*/);
57
58 extern int progress_removeitem(struct progress_state */*progress*/,
59 struct progress_item */*item*/);
60
61
62 extern int progress_vputleft(struct progress_render_state */*render*/,
63 const char */*fmt*/, va_list /*ap*/);
64
65 extern int progress_vputright(struct progress_render_state */*render*/,
66 const char */*fmt*/, va_list /*ap*/);
67
68 __attribute__((format(printf, 2, 3)))
69 extern int progress_putleft(struct progress_render_state */*render*/,
70 const char */*fmt*/, ...);
71
72 __attribute__((format(printf, 2, 3)))
73 extern int progress_putright(struct progress_render_state */*render*/,
74 const char */*fmt*/, ...);
75
76 extern int progress_showbar(struct progress_render_state */*render*/,
77 double /*frac*/);
78
79 extern int progress_shownotice(struct progress_render_state */*render*/,
80 int /*bg*/, int /*fg*/);
81
82 #endif