progress.c 1.2 KB

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