progress.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include "../cache.h"
  4. #include "../progress.h"
  5. #include "../libslang.h"
  6. #include "../ui.h"
  7. #include "tui.h"
  8. #include "units.h"
  9. #include "../browser.h"
  10. static void __tui_progress__init(struct ui_progress *p)
  11. {
  12. p->next = p->step = p->total / (SLtt_Screen_Cols - 2) ?: 1;
  13. }
  14. static int get_title(struct ui_progress *p, char *buf, size_t size)
  15. {
  16. char buf_cur[20];
  17. char buf_tot[20];
  18. int ret;
  19. ret = unit_number__scnprintf(buf_cur, sizeof(buf_cur), p->curr);
  20. ret += unit_number__scnprintf(buf_tot, sizeof(buf_tot), p->total);
  21. return ret + scnprintf(buf, size, "%s [%s/%s]",
  22. p->title, buf_cur, buf_tot);
  23. }
  24. static void tui_progress__update(struct ui_progress *p)
  25. {
  26. char buf[100], *title = (char *) p->title;
  27. int bar, y;
  28. /*
  29. * FIXME: We should have a per UI backend way of showing progress,
  30. * stdio will just show a percentage as NN%, etc.
  31. */
  32. if (use_browser <= 0)
  33. return;
  34. if (p->total == 0)
  35. return;
  36. if (p->size) {
  37. get_title(p, buf, sizeof(buf));
  38. title = buf;
  39. }
  40. ui__refresh_dimensions(false);
  41. pthread_mutex_lock(&ui__lock);
  42. y = SLtt_Screen_Rows / 2 - 2;
  43. SLsmg_set_color(0);
  44. SLsmg_draw_box(y, 0, 3, SLtt_Screen_Cols);
  45. SLsmg_gotorc(y++, 1);
  46. SLsmg_write_string(title);
  47. SLsmg_fill_region(y, 1, 1, SLtt_Screen_Cols - 2, ' ');
  48. SLsmg_set_color(HE_COLORSET_SELECTED);
  49. bar = ((SLtt_Screen_Cols - 2) * p->curr) / p->total;
  50. SLsmg_fill_region(y, 1, 1, bar, ' ');
  51. SLsmg_refresh();
  52. pthread_mutex_unlock(&ui__lock);
  53. }
  54. static void tui_progress__finish(void)
  55. {
  56. int y;
  57. if (use_browser <= 0)
  58. return;
  59. ui__refresh_dimensions(false);
  60. pthread_mutex_lock(&ui__lock);
  61. y = SLtt_Screen_Rows / 2 - 2;
  62. SLsmg_set_color(0);
  63. SLsmg_fill_region(y, 0, 3, SLtt_Screen_Cols, ' ');
  64. SLsmg_refresh();
  65. pthread_mutex_unlock(&ui__lock);
  66. }
  67. static struct ui_progress_ops tui_progress__ops = {
  68. .init = __tui_progress__init,
  69. .update = tui_progress__update,
  70. .finish = tui_progress__finish,
  71. };
  72. void tui_progress__init(void)
  73. {
  74. ui_progress__ops = &tui_progress__ops;
  75. }