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 if (use_browser <= 0) 44 return; 45 newtScaleSet(self->scale, curr); 46 newtRefresh(); 47} 48 49void ui_progress__delete(struct ui_progress *self) 50{ 51 if (use_browser > 0) { 52 newtFormDestroy(self->form); 53 newtPopWindow(); 54 } 55 free(self); 56} 57