cmCursesLongMessageForm.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCursesLongMessageForm.h"
  4. #include <cstdio>
  5. #include <cstring>
  6. #include "cmCursesForm.h"
  7. #include "cmCursesMainForm.h"
  8. #include "cmCursesStandardIncludes.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmVersion.h"
  11. inline int ctrl(int z)
  12. {
  13. return (z & 037);
  14. }
  15. cmCursesLongMessageForm::cmCursesLongMessageForm(
  16. std::vector<std::string> const& messages, const char* title,
  17. ScrollBehavior scrollBehavior)
  18. : Scrolling(scrollBehavior)
  19. {
  20. // Append all messages into on big string
  21. this->Messages = cmJoin(messages, "\n");
  22. this->Title = title;
  23. this->Fields[0] = nullptr;
  24. this->Fields[1] = nullptr;
  25. }
  26. cmCursesLongMessageForm::~cmCursesLongMessageForm()
  27. {
  28. if (this->Fields[0]) {
  29. free_field(this->Fields[0]);
  30. }
  31. }
  32. void cmCursesLongMessageForm::UpdateContent(std::string const& output,
  33. std::string const& title)
  34. {
  35. this->Title = title;
  36. if (!output.empty() && this->Messages.size() < MAX_CONTENT_SIZE) {
  37. this->Messages.push_back('\n');
  38. this->Messages.append(output);
  39. form_driver(this->Form, REQ_NEXT_LINE);
  40. form_driver(this->Form, REQ_BEG_LINE);
  41. this->DrawMessage(output.c_str());
  42. }
  43. this->UpdateStatusBar();
  44. touchwin(stdscr);
  45. refresh();
  46. }
  47. void cmCursesLongMessageForm::UpdateStatusBar()
  48. {
  49. int x;
  50. int y;
  51. getmaxyx(stdscr, y, x);
  52. char bar[cmCursesMainForm::MAX_WIDTH];
  53. size_t size = this->Title.size();
  54. if (size >= cmCursesMainForm::MAX_WIDTH) {
  55. size = cmCursesMainForm::MAX_WIDTH - 1;
  56. }
  57. strncpy(bar, this->Title.c_str(), size);
  58. for (size_t i = size; i < cmCursesMainForm::MAX_WIDTH; i++) {
  59. bar[i] = ' ';
  60. }
  61. int width;
  62. if (x >= 0 && x < cmCursesMainForm::MAX_WIDTH) {
  63. width = x;
  64. } else {
  65. width = cmCursesMainForm::MAX_WIDTH - 1;
  66. }
  67. bar[width] = '\0';
  68. char version[cmCursesMainForm::MAX_WIDTH];
  69. char vertmp[128];
  70. snprintf(vertmp, sizeof(vertmp), "CMake Version %s",
  71. cmVersion::GetCMakeVersion());
  72. size_t sideSpace = (width - strlen(vertmp));
  73. for (size_t i = 0; i < sideSpace; i++) {
  74. version[i] = ' ';
  75. }
  76. sprintf(version + sideSpace, "%s", vertmp);
  77. version[width] = '\0';
  78. char fmt_s[] = "%s";
  79. curses_move(y - 4, 0);
  80. attron(A_STANDOUT);
  81. printw(fmt_s, bar);
  82. attroff(A_STANDOUT);
  83. curses_move(y - 3, 0);
  84. printw(fmt_s, version);
  85. pos_form_cursor(this->Form);
  86. }
  87. void cmCursesLongMessageForm::PrintKeys()
  88. {
  89. int x;
  90. int y;
  91. getmaxyx(stdscr, y, x);
  92. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  93. return;
  94. }
  95. char firstLine[512];
  96. snprintf(firstLine, sizeof(firstLine), "Press [e] to exit screen");
  97. char fmt_s[] = "%s";
  98. curses_move(y - 2, 0);
  99. printw(fmt_s, firstLine);
  100. pos_form_cursor(this->Form);
  101. }
  102. void cmCursesLongMessageForm::Render(int /*left*/, int /*top*/, int /*width*/,
  103. int /*height*/)
  104. {
  105. int x;
  106. int y;
  107. getmaxyx(stdscr, y, x);
  108. if (this->Form) {
  109. unpost_form(this->Form);
  110. free_form(this->Form);
  111. this->Form = nullptr;
  112. }
  113. if (this->Fields[0]) {
  114. free_field(this->Fields[0]);
  115. this->Fields[0] = nullptr;
  116. }
  117. this->Fields[0] = new_field(y - 6, x - 2, 1, 1, 0, 0);
  118. field_opts_off(this->Fields[0], O_STATIC);
  119. this->Form = new_form(this->Fields);
  120. post_form(this->Form);
  121. form_driver(this->Form, REQ_BEG_FIELD);
  122. this->DrawMessage(this->Messages.c_str());
  123. this->UpdateStatusBar();
  124. touchwin(stdscr);
  125. refresh();
  126. }
  127. void cmCursesLongMessageForm::DrawMessage(const char* msg) const
  128. {
  129. int i = 0;
  130. while (msg[i] != '\0' && i < MAX_CONTENT_SIZE) {
  131. if (msg[i] == '\n' && msg[i + 1] != '\0') {
  132. form_driver(this->Form, REQ_NEXT_LINE);
  133. form_driver(this->Form, REQ_BEG_LINE);
  134. } else {
  135. form_driver(this->Form, msg[i]);
  136. }
  137. i++;
  138. }
  139. if (this->Scrolling == ScrollBehavior::ScrollDown) {
  140. form_driver(this->Form, REQ_END_FIELD);
  141. } else {
  142. form_driver(this->Form, REQ_BEG_FIELD);
  143. }
  144. }
  145. void cmCursesLongMessageForm::HandleInput()
  146. {
  147. if (!this->Form) {
  148. return;
  149. }
  150. char debugMessage[128];
  151. for (;;) {
  152. this->PrintKeys();
  153. int key = getch();
  154. #ifdef _WIN32
  155. if (key == KEY_RESIZE) {
  156. HandleResize();
  157. }
  158. #endif // _WIN32
  159. snprintf(debugMessage, sizeof(debugMessage),
  160. "Message widget handling input, key: %d", key);
  161. cmCursesForm::LogMessage(debugMessage);
  162. // quit
  163. if (key == 'o' || key == 'e') {
  164. break;
  165. }
  166. if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  167. form_driver(this->Form, REQ_SCR_FLINE);
  168. } else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  169. form_driver(this->Form, REQ_SCR_BLINE);
  170. } else if (key == KEY_NPAGE || key == ctrl('d')) {
  171. form_driver(this->Form, REQ_SCR_FPAGE);
  172. } else if (key == KEY_PPAGE || key == ctrl('u')) {
  173. form_driver(this->Form, REQ_SCR_BPAGE);
  174. }
  175. this->UpdateStatusBar();
  176. touchwin(stdscr);
  177. wrefresh(stdscr);
  178. }
  179. }