progress.c 1.9 KB

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