progress.c revision e6817ec1d8ab31fc7b01906e305f848542df6413
1#include <stdlib.h>
2#include <newt.h>
3#include "../cache.h"
4#include "progress.h"
5
6struct ui_progress {
7	newtComponent form, scale;
8};
9
10struct ui_progress *ui_progress__new(const char *title, u64 total)
11{
12	struct ui_progress *self = malloc(sizeof(*self));
13
14	if (self != NULL) {
15		int cols;
16
17		if (use_browser <= 0)
18			return self;
19		newtGetScreenSize(&cols, NULL);
20		cols -= 4;
21		newtCenteredWindow(cols, 1, title);
22		self->form  = newtForm(NULL, NULL, 0);
23		if (self->form == NULL)
24			goto out_free_self;
25		self->scale = newtScale(0, 0, cols, total);
26		if (self->scale == NULL)
27			goto out_free_form;
28		newtFormAddComponent(self->form, self->scale);
29		newtRefresh();
30	}
31
32	return self;
33
34out_free_form:
35	newtFormDestroy(self->form);
36out_free_self:
37	free(self);
38	return NULL;
39}
40
41void ui_progress__update(struct ui_progress *self, u64 curr)
42{
43	/*
44	 * FIXME: We should have a per UI backend way of showing progress,
45	 * stdio will just show a percentage as NN%, etc.
46	 */
47	if (use_browser <= 0)
48		return;
49	newtScaleSet(self->scale, curr);
50	newtRefresh();
51}
52
53void ui_progress__delete(struct ui_progress *self)
54{
55	if (use_browser > 0) {
56		newtFormDestroy(self->form);
57		newtPopWindow();
58	}
59	free(self);
60}
61