InfoScreen.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. htop - InfoScreen.c
  3. (C) 2016 Hisham H. Muhammad
  4. (C) 2020,2022 htop dev team
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "InfoScreen.h"
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "CRT.h"
  14. #include "IncSet.h"
  15. #include "ListItem.h"
  16. #include "Object.h"
  17. #include "ProvideCurses.h"
  18. #include "XUtils.h"
  19. static const char* const InfoScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL};
  20. static const char* const InfoScreenKeys[] = {"F3", "F4", "F5", "Esc"};
  21. static const int InfoScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
  22. InfoScreen* InfoScreen_init(InfoScreen* this, const Process* process, FunctionBar* bar, int height, const char* panelHeader) {
  23. this->process = process;
  24. if (!bar) {
  25. bar = FunctionBar_new(InfoScreenFunctions, InfoScreenKeys, InfoScreenEvents);
  26. }
  27. this->display = Panel_new(0, 1, COLS, height, Class(ListItem), false, bar);
  28. this->inc = IncSet_new(bar);
  29. this->lines = Vector_new(Vector_type(this->display->items), true, DEFAULT_SIZE);
  30. Panel_setHeader(this->display, panelHeader);
  31. return this;
  32. }
  33. InfoScreen* InfoScreen_done(InfoScreen* this) {
  34. Panel_delete((Object*)this->display);
  35. IncSet_delete(this->inc);
  36. Vector_delete(this->lines);
  37. return this;
  38. }
  39. void InfoScreen_drawTitled(InfoScreen* this, const char* fmt, ...) {
  40. va_list ap;
  41. va_start(ap, fmt);
  42. char title[COLS + 1];
  43. int len = vsnprintf(title, sizeof(title), fmt, ap);
  44. va_end(ap);
  45. if (len > COLS) {
  46. memset(&title[COLS - 3], '.', 3);
  47. }
  48. attrset(CRT_colors[METER_TEXT]);
  49. mvhline(0, 0, ' ', COLS);
  50. mvaddstr(0, 0, title);
  51. attrset(CRT_colors[DEFAULT_COLOR]);
  52. Panel_draw(this->display, true, true, true, false);
  53. IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]);
  54. }
  55. void InfoScreen_addLine(InfoScreen* this, const char* line) {
  56. Vector_add(this->lines, (Object*) ListItem_new(line, 0));
  57. const char* incFilter = IncSet_filter(this->inc);
  58. if (!incFilter || String_contains_i(line, incFilter, true)) {
  59. Panel_add(this->display, Vector_get(this->lines, Vector_size(this->lines) - 1));
  60. }
  61. }
  62. void InfoScreen_appendLine(InfoScreen* this, const char* line) {
  63. ListItem* last = (ListItem*)Vector_get(this->lines, Vector_size(this->lines) - 1);
  64. ListItem_append(last, line);
  65. const char* incFilter = IncSet_filter(this->inc);
  66. if (incFilter && Panel_get(this->display, Panel_size(this->display) - 1) != (Object*)last && String_contains_i(line, incFilter, true)) {
  67. Panel_add(this->display, (Object*)last);
  68. }
  69. }
  70. void InfoScreen_run(InfoScreen* this) {
  71. Panel* panel = this->display;
  72. if (As_InfoScreen(this)->scan)
  73. InfoScreen_scan(this);
  74. InfoScreen_draw(this);
  75. bool looping = true;
  76. while (looping) {
  77. Panel_draw(panel, false, true, true, false);
  78. IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]);
  79. int ch = Panel_getCh(panel);
  80. if (ch == ERR) {
  81. if (As_InfoScreen(this)->onErr) {
  82. InfoScreen_onErr(this);
  83. continue;
  84. }
  85. }
  86. #ifdef HAVE_GETMOUSE
  87. if (ch == KEY_MOUSE) {
  88. MEVENT mevent;
  89. int ok = getmouse(&mevent);
  90. if (ok == OK) {
  91. if (mevent.bstate & BUTTON1_RELEASED) {
  92. if (mevent.y >= panel->y && mevent.y < LINES - 1) {
  93. Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1);
  94. ch = 0;
  95. } else if (mevent.y == LINES - 1) {
  96. ch = IncSet_synthesizeEvent(this->inc, mevent.x);
  97. }
  98. }
  99. #if NCURSES_MOUSE_VERSION > 1
  100. else if (mevent.bstate & BUTTON4_PRESSED) {
  101. ch = KEY_WHEELUP;
  102. } else if (mevent.bstate & BUTTON5_PRESSED) {
  103. ch = KEY_WHEELDOWN;
  104. }
  105. #endif
  106. }
  107. }
  108. #endif
  109. if (this->inc->active) {
  110. IncSet_handleKey(this->inc, ch, panel, IncSet_getListItemValue, this->lines);
  111. continue;
  112. }
  113. switch (ch) {
  114. case ERR:
  115. continue;
  116. case KEY_F(3):
  117. case '/':
  118. IncSet_activate(this->inc, INC_SEARCH, panel);
  119. break;
  120. case KEY_F(4):
  121. case '\\':
  122. IncSet_activate(this->inc, INC_FILTER, panel);
  123. break;
  124. case KEY_F(5):
  125. clear();
  126. if (As_InfoScreen(this)->scan) {
  127. Vector_prune(this->lines);
  128. InfoScreen_scan(this);
  129. }
  130. InfoScreen_draw(this);
  131. break;
  132. case '\014': // Ctrl+L
  133. clear();
  134. InfoScreen_draw(this);
  135. break;
  136. case 27:
  137. case 'q':
  138. case KEY_F(10):
  139. looping = false;
  140. break;
  141. case KEY_RESIZE:
  142. Panel_resize(panel, COLS, LINES - 2);
  143. if (As_InfoScreen(this)->scan) {
  144. Vector_prune(this->lines);
  145. InfoScreen_scan(this);
  146. }
  147. InfoScreen_draw(this);
  148. break;
  149. default:
  150. if (As_InfoScreen(this)->onKey && InfoScreen_onKey(this, ch)) {
  151. continue;
  152. }
  153. Panel_onKey(panel, ch);
  154. }
  155. }
  156. }