cmCursesMainForm.cxx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmCursesMainForm.h"
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include "cmCursesCacheEntryComposite.h"
  10. #include "cmCursesDummyWidget.h"
  11. #include "cmCursesForm.h"
  12. #include "cmCursesLabelWidget.h"
  13. #include "cmCursesLongMessageForm.h"
  14. #include "cmCursesStandardIncludes.h"
  15. #include "cmCursesStringWidget.h"
  16. #include "cmCursesWidget.h"
  17. #include "cmState.h"
  18. #include "cmStateTypes.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmSystemTools.h"
  21. #include "cmValue.h"
  22. #include "cmVersion.h"
  23. #include "cmake.h"
  24. inline int ctrl(int z)
  25. {
  26. return (z & 037);
  27. }
  28. cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args,
  29. int initWidth)
  30. : Args(std::move(args))
  31. , InitialWidth(initWidth)
  32. {
  33. this->HelpMessage.emplace_back(
  34. "Welcome to ccmake, curses based user interface for CMake.");
  35. this->HelpMessage.emplace_back();
  36. this->HelpMessage.emplace_back(s_ConstHelpMessage);
  37. this->CMakeInstance = cm::make_unique<cmake>(cmState::Role::Project);
  38. this->CMakeInstance->SetCMakeEditCommand(
  39. cmSystemTools::GetCMakeCursesCommand());
  40. // create the arguments for the cmake object
  41. std::string whereCMake =
  42. cmStrCat(cmSystemTools::GetProgramPath(this->Args[0]), "/cmake");
  43. this->Args[0] = whereCMake;
  44. this->CMakeInstance->SetArgs(this->Args);
  45. }
  46. cmCursesMainForm::~cmCursesMainForm()
  47. {
  48. if (this->Form) {
  49. unpost_form(this->Form);
  50. free_form(this->Form);
  51. this->Form = nullptr;
  52. }
  53. }
  54. // See if a cache entry is in the list of entries in the ui.
  55. bool cmCursesMainForm::LookForCacheEntry(std::string const& key)
  56. {
  57. return std::any_of(this->Entries.begin(), this->Entries.end(),
  58. [&key](cmCursesCacheEntryComposite const& entry) {
  59. return key == entry.Key;
  60. });
  61. }
  62. // Create new cmCursesCacheEntryComposite entries from the cache
  63. void cmCursesMainForm::InitializeUI()
  64. {
  65. // Hide the cursor by default
  66. curs_set(0);
  67. // Create a vector of cmCursesCacheEntryComposite's
  68. // which contain labels, entries and new entry markers
  69. std::vector<cmCursesCacheEntryComposite> newEntries;
  70. std::vector<std::string> cacheKeys =
  71. this->CMakeInstance->GetState()->GetCacheEntryKeys();
  72. newEntries.reserve(cacheKeys.size());
  73. // Count non-internal and non-static entries
  74. int count = 0;
  75. for (std::string const& key : cacheKeys) {
  76. cmStateEnums::CacheEntryType t =
  77. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  78. if (t != cmStateEnums::INTERNAL && t != cmStateEnums::STATIC &&
  79. t != cmStateEnums::UNINITIALIZED) {
  80. ++count;
  81. }
  82. }
  83. int entrywidth = this->InitialWidth - 35;
  84. // Add a label to display when cache is empty
  85. // dummy entry widget (does not respond to input)
  86. this->EmptyCacheEntry =
  87. cm::make_unique<cmCursesCacheEntryComposite>("EMPTY CACHE", 30, 30);
  88. this->EmptyCacheEntry->Entry =
  89. cm::make_unique<cmCursesDummyWidget>(1, 1, 1, 1);
  90. if (count > 0) {
  91. // Create the composites.
  92. // First add entries which are new
  93. for (std::string const& key : cacheKeys) {
  94. cmStateEnums::CacheEntryType t =
  95. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  96. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  97. t == cmStateEnums::UNINITIALIZED) {
  98. continue;
  99. }
  100. if (!this->LookForCacheEntry(key)) {
  101. newEntries.emplace_back(key, this->CMakeInstance->GetState(), true, 30,
  102. entrywidth);
  103. this->OkToGenerate = false;
  104. }
  105. }
  106. // then add entries which are old
  107. for (std::string const& key : cacheKeys) {
  108. cmStateEnums::CacheEntryType t =
  109. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  110. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  111. t == cmStateEnums::UNINITIALIZED) {
  112. continue;
  113. }
  114. if (this->LookForCacheEntry(key)) {
  115. newEntries.emplace_back(key, this->CMakeInstance->GetState(), false,
  116. 30, entrywidth);
  117. }
  118. }
  119. }
  120. // Replace old entries
  121. this->Entries = std::move(newEntries);
  122. // Compute fields from composites
  123. this->RePost();
  124. }
  125. void cmCursesMainForm::RePost()
  126. {
  127. // Create the fields to be passed to the form.
  128. if (this->Form) {
  129. unpost_form(this->Form);
  130. free_form(this->Form);
  131. this->Form = nullptr;
  132. }
  133. this->Fields.clear();
  134. if (this->AdvancedMode) {
  135. this->NumberOfVisibleEntries = this->Entries.size();
  136. } else {
  137. // If normal mode, count only non-advanced entries
  138. this->NumberOfVisibleEntries = 0;
  139. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  140. cmValue existingValue =
  141. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  142. bool advanced =
  143. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  144. entry.GetValue(), "ADVANCED");
  145. if (!existingValue || (!this->AdvancedMode && advanced)) {
  146. continue;
  147. }
  148. this->NumberOfVisibleEntries++;
  149. }
  150. }
  151. // there is always one even if it is the dummy one
  152. if (this->NumberOfVisibleEntries == 0) {
  153. this->NumberOfVisibleEntries = 1;
  154. }
  155. // Assign the fields: 3 for each entry: label, new entry marker
  156. // ('*' or ' ') and entry widget
  157. this->Fields.reserve(3 * this->NumberOfVisibleEntries + 1);
  158. // Assign fields
  159. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  160. cmValue existingValue =
  161. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  162. bool advanced =
  163. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  164. entry.GetValue(), "ADVANCED");
  165. if (!existingValue || (!this->AdvancedMode && advanced)) {
  166. continue;
  167. }
  168. this->Fields.push_back(entry.Label->Field);
  169. this->Fields.push_back(entry.IsNewLabel->Field);
  170. this->Fields.push_back(entry.Entry->Field);
  171. }
  172. // if no cache entries there should still be one dummy field
  173. this->IsEmpty = this->Fields.empty();
  174. if (this->IsEmpty) {
  175. this->Fields.push_back(this->EmptyCacheEntry->Label->Field);
  176. this->Fields.push_back(this->EmptyCacheEntry->IsNewLabel->Field);
  177. this->Fields.push_back(this->EmptyCacheEntry->Entry->Field);
  178. this->NumberOfVisibleEntries = 1;
  179. }
  180. // Has to be null terminated.
  181. this->Fields.push_back(nullptr);
  182. }
  183. void cmCursesMainForm::Render(int left, int top, int width, int height)
  184. {
  185. if (this->Form) {
  186. FIELD* currentField = current_field(this->Form);
  187. cmCursesWidget* cw =
  188. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  189. // If in edit mode, get out of it
  190. if (cw->GetType() == cmStateEnums::STRING ||
  191. cw->GetType() == cmStateEnums::PATH ||
  192. cw->GetType() == cmStateEnums::FILEPATH) {
  193. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  194. sw->SetInEdit(false);
  195. }
  196. // Delete the previous form
  197. unpost_form(this->Form);
  198. free_form(this->Form);
  199. this->Form = nullptr;
  200. }
  201. // Wrong window size
  202. if (width < cmCursesMainForm::MIN_WIDTH || width < this->InitialWidth ||
  203. height < cmCursesMainForm::MIN_HEIGHT) {
  204. return;
  205. }
  206. // Leave room for toolbar
  207. height -= 7;
  208. if (this->AdvancedMode) {
  209. this->NumberOfVisibleEntries = this->Entries.size();
  210. } else {
  211. // If normal, display only non-advanced entries
  212. this->NumberOfVisibleEntries = 0;
  213. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  214. cmValue existingValue =
  215. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  216. bool advanced =
  217. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  218. entry.GetValue(), "ADVANCED");
  219. if (!existingValue || (!this->AdvancedMode && advanced)) {
  220. continue;
  221. }
  222. this->NumberOfVisibleEntries++;
  223. }
  224. }
  225. // Re-adjust the fields according to their place
  226. this->NumberOfPages = 1;
  227. if (height > 0) {
  228. bool isNewPage;
  229. int i = 0;
  230. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  231. cmValue existingValue =
  232. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  233. bool advanced =
  234. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  235. entry.GetValue(), "ADVANCED");
  236. if (!existingValue || (!this->AdvancedMode && advanced)) {
  237. continue;
  238. }
  239. int row = (i % height) + 1;
  240. int page = (i / height) + 1;
  241. isNewPage = (page > 1) && (row == 1);
  242. if (isNewPage) {
  243. this->NumberOfPages++;
  244. }
  245. entry.Label->Move(left, top + row - 1, isNewPage);
  246. entry.IsNewLabel->Move(left + 30, top + row - 1, false);
  247. entry.Entry->Move(left + 33, top + row - 1, false);
  248. entry.Entry->SetPage(this->NumberOfPages);
  249. i++;
  250. }
  251. }
  252. // Post the form
  253. this->Form = new_form(this->Fields.data());
  254. post_form(this->Form);
  255. // Update toolbar
  256. this->UpdateStatusBar();
  257. this->PrintKeys();
  258. touchwin(stdscr);
  259. refresh();
  260. }
  261. void cmCursesMainForm::PrintKeys(int process /* = 0 */)
  262. {
  263. int x;
  264. int y;
  265. getmaxyx(stdscr, y, x);
  266. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  267. y < cmCursesMainForm::MIN_HEIGHT) {
  268. return;
  269. }
  270. // Give the current widget (if it exists), a chance to print keys
  271. cmCursesWidget* cw = nullptr;
  272. if (this->Form) {
  273. FIELD* currentField = current_field(this->Form);
  274. cw = reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  275. }
  276. char fmt_s[] = "%s";
  277. if (!cw || !cw->PrintKeys()) {
  278. char firstLine[512] = "";
  279. char secondLine[512] = "";
  280. char thirdLine[512] = "";
  281. if (process) {
  282. memset(firstLine, ' ', 68);
  283. memset(secondLine, ' ', 68);
  284. memset(thirdLine, ' ', 68);
  285. } else {
  286. if (this->OkToGenerate) {
  287. snprintf(firstLine, sizeof(firstLine),
  288. " [l] Show log output [c] Configure"
  289. " [g] Generate ");
  290. } else {
  291. snprintf(firstLine, sizeof(firstLine),
  292. " [l] Show log output [c] Configure"
  293. " ");
  294. }
  295. {
  296. char const* toggleKeyInstruction =
  297. " [t] Toggle advanced mode (currently %s)";
  298. snprintf(thirdLine, sizeof(thirdLine), toggleKeyInstruction,
  299. this->AdvancedMode ? "on" : "off");
  300. }
  301. snprintf(secondLine, sizeof(secondLine),
  302. " [h] Help [q] Quit without generating");
  303. }
  304. curses_move(y - 4, 0);
  305. char fmt[512] = "Keys: [enter] Edit an entry [d] Delete an entry";
  306. if (process) {
  307. memset(fmt, ' ', 57);
  308. }
  309. printw(fmt_s, fmt);
  310. curses_move(y - 3, 0);
  311. printw(fmt_s, firstLine);
  312. curses_move(y - 2, 0);
  313. printw(fmt_s, secondLine);
  314. curses_move(y - 1, 0);
  315. printw(fmt_s, thirdLine);
  316. }
  317. if (cw) {
  318. char pageLine[512] = "";
  319. snprintf(pageLine, sizeof(pageLine), "Page %d of %d", cw->GetPage(),
  320. this->NumberOfPages);
  321. curses_move(0, 65 - static_cast<unsigned int>(strlen(pageLine)) - 1);
  322. printw(fmt_s, pageLine);
  323. }
  324. pos_form_cursor(this->Form);
  325. }
  326. // Print the key of the current entry and the CMake version
  327. // on the status bar. Designed for a width of 80 chars.
  328. void cmCursesMainForm::UpdateStatusBar(cm::optional<std::string> message)
  329. {
  330. int x;
  331. int y;
  332. getmaxyx(stdscr, y, x);
  333. // If window size is too small, display error and return
  334. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  335. y < cmCursesMainForm::MIN_HEIGHT) {
  336. curses_clear();
  337. curses_move(0, 0);
  338. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  339. printw(fmt,
  340. (cmCursesMainForm::MIN_WIDTH < this->InitialWidth
  341. ? this->InitialWidth
  342. : cmCursesMainForm::MIN_WIDTH),
  343. cmCursesMainForm::MIN_HEIGHT);
  344. touchwin(stdscr);
  345. wrefresh(stdscr);
  346. return;
  347. }
  348. // Find the current label index
  349. // Field are grouped by 3, the label should be 2 less than the current index
  350. using size_type = decltype(this->Fields)::size_type;
  351. size_type currentLabelIndex = field_index(current_field(this->Form)) - 2;
  352. // Use the status message if any, otherwise join the key and help string
  353. std::string bar;
  354. if (message) {
  355. bar = *message;
  356. } else {
  357. // Get the key of the current entry
  358. cmCursesWidget* labelWidget = reinterpret_cast<cmCursesWidget*>(
  359. field_userptr(this->Fields[currentLabelIndex]));
  360. std::string labelValue = labelWidget->GetValue();
  361. bar = labelValue + ": ";
  362. // Get the help string of the current entry
  363. // and add it to the help string
  364. auto* cmakeState = this->CMakeInstance->GetState();
  365. cmValue existingValue = cmakeState->GetCacheEntryValue(labelValue);
  366. if (existingValue) {
  367. cmValue help =
  368. cmakeState->GetCacheEntryProperty(labelValue, "HELPSTRING");
  369. if (help) {
  370. bar += *help;
  371. }
  372. }
  373. }
  374. // Pad with spaces to erase any previous text,
  375. // or truncate as necessary to fit the screen
  376. bar.resize(x, ' ');
  377. curses_move(y - 5, 0);
  378. attron(A_STANDOUT);
  379. char fmt_s[] = "%s";
  380. printw(fmt_s, bar.c_str());
  381. attroff(A_STANDOUT);
  382. // Highlight the current label, reset others
  383. // Fields are grouped by 3, the first one being the label
  384. // so start at 0 and move up by 3 avoiding the last null entry
  385. for (size_type index = 0; index < this->Fields.size() - 1; index += 3) {
  386. int attr = (index == currentLabelIndex) ? A_STANDOUT : A_NORMAL;
  387. set_field_fore(this->Fields[index], attr);
  388. set_field_back(this->Fields[index], attr);
  389. set_field_fore(this->Fields[index + 1], attr);
  390. set_field_back(this->Fields[index + 1], attr);
  391. }
  392. // Display CMake version under the status bar
  393. // We want to display this on the right
  394. std::string version = "CMake Version ";
  395. version += cmVersion::GetCMakeVersion();
  396. version.resize(std::min<std::string::size_type>(x, version.size()));
  397. curses_move(y - 4, x - static_cast<int>(version.size()));
  398. printw(fmt_s, version.c_str());
  399. pos_form_cursor(this->Form);
  400. }
  401. void cmCursesMainForm::UpdateProgress(std::string const& msg, float prog)
  402. {
  403. if (prog >= 0) {
  404. constexpr int progressBarWidth = 40;
  405. int progressBarCompleted = static_cast<int>(progressBarWidth * prog);
  406. int percentCompleted = static_cast<int>(100 * prog);
  407. this->LastProgress = (percentCompleted < 100 ? " " : "");
  408. this->LastProgress += (percentCompleted < 10 ? " " : "");
  409. this->LastProgress += std::to_string(percentCompleted) + "% [";
  410. this->LastProgress.append(progressBarCompleted, '#');
  411. this->LastProgress.append(progressBarWidth - progressBarCompleted, ' ');
  412. this->LastProgress += "] " + msg + "...";
  413. this->DisplayOutputs(std::string());
  414. } else {
  415. this->Outputs.emplace_back(msg);
  416. this->DisplayOutputs(msg);
  417. }
  418. }
  419. void cmCursesMainForm::Write()
  420. {
  421. this->FillCacheManagerFromUI();
  422. this->CMakeInstance->SaveCache(
  423. this->CMakeInstance->GetHomeOutputDirectory());
  424. this->LoadCache(nullptr);
  425. }
  426. int cmCursesMainForm::Configure(int noconfigure)
  427. {
  428. this->ResetOutputs();
  429. if (noconfigure == 0) {
  430. this->UpdateProgress("Configuring", 0);
  431. this->CMakeInstance->SetProgressCallback(
  432. [this](std::string const& msg, float prog) {
  433. this->UpdateProgress(msg, prog);
  434. });
  435. }
  436. // always save the current gui values to disk
  437. this->Write();
  438. // run the generate process
  439. this->OkToGenerate = true;
  440. int retVal;
  441. if (noconfigure) {
  442. retVal = this->CMakeInstance->DoPreConfigureChecks();
  443. this->OkToGenerate = false;
  444. if (retVal > 0) {
  445. retVal = 0;
  446. }
  447. } else {
  448. retVal = this->CMakeInstance->Configure();
  449. }
  450. this->CMakeInstance->SetProgressCallback(nullptr);
  451. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  452. if (retVal != 0 || this->HasNonStatusOutputs) {
  453. // see if there was an error
  454. if (cmSystemTools::GetErrorOccurredFlag()) {
  455. this->OkToGenerate = false;
  456. }
  457. int xx;
  458. int yy;
  459. getmaxyx(stdscr, yy, xx);
  460. char const* title = "Configure produced the following output";
  461. if (cmSystemTools::GetErrorOccurredFlag()) {
  462. title = "Configure failed with the following output";
  463. }
  464. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  465. this->Outputs, title,
  466. cmCursesLongMessageForm::ScrollBehavior::ScrollDown);
  467. // reset error condition
  468. cmSystemTools::ResetErrorOccurredFlag();
  469. CurrentForm = msgs;
  470. msgs->Render(1, 1, xx, yy);
  471. msgs->HandleInput();
  472. // If they typed the wrong source directory, we report
  473. // an error and exit
  474. if (retVal == -2) {
  475. return retVal;
  476. }
  477. }
  478. this->InitializeUI();
  479. CurrentForm = this;
  480. int xi;
  481. int yi;
  482. getmaxyx(stdscr, yi, xi);
  483. this->Render(1, 1, xi, yi);
  484. return 0;
  485. }
  486. int cmCursesMainForm::Generate()
  487. {
  488. this->ResetOutputs();
  489. this->UpdateProgress("Generating", 0);
  490. this->CMakeInstance->SetProgressCallback(
  491. [this](std::string const& msg, float prog) {
  492. this->UpdateProgress(msg, prog);
  493. });
  494. // run the generate process
  495. int retVal = this->CMakeInstance->Generate();
  496. this->CMakeInstance->SetProgressCallback(nullptr);
  497. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  498. if (retVal != 0 || this->HasNonStatusOutputs) {
  499. // see if there was an error
  500. if (cmSystemTools::GetErrorOccurredFlag()) {
  501. this->OkToGenerate = false;
  502. }
  503. // reset error condition
  504. cmSystemTools::ResetErrorOccurredFlag();
  505. int xx;
  506. int yy;
  507. getmaxyx(stdscr, yy, xx);
  508. char const* title = "Generate produced the following output";
  509. if (cmSystemTools::GetErrorOccurredFlag()) {
  510. title = "Generate failed with the following output";
  511. }
  512. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  513. this->Outputs, title,
  514. cmCursesLongMessageForm::ScrollBehavior::ScrollDown);
  515. CurrentForm = msgs;
  516. msgs->Render(1, 1, xx, yy);
  517. msgs->HandleInput();
  518. // If they typed the wrong source directory, we report
  519. // an error and exit
  520. if (retVal == -2) {
  521. return retVal;
  522. }
  523. }
  524. this->InitializeUI();
  525. CurrentForm = this;
  526. int xi;
  527. int yi;
  528. getmaxyx(stdscr, yi, xi);
  529. this->Render(1, 1, xi, yi);
  530. return 0;
  531. }
  532. void cmCursesMainForm::AddError(std::string const& message,
  533. char const* /*unused*/)
  534. {
  535. this->Outputs.emplace_back(message);
  536. this->HasNonStatusOutputs = true;
  537. this->DisplayOutputs(message);
  538. }
  539. void cmCursesMainForm::RemoveEntry(char const* value)
  540. {
  541. if (!value) {
  542. return;
  543. }
  544. auto removeIt =
  545. std::find_if(this->Entries.begin(), this->Entries.end(),
  546. [value](cmCursesCacheEntryComposite& entry) -> bool {
  547. char const* val = entry.GetValue();
  548. return val && !strcmp(value, val);
  549. });
  550. if (removeIt != this->Entries.end()) {
  551. this->CMakeInstance->UnwatchUnusedCli(value);
  552. this->Entries.erase(removeIt);
  553. }
  554. }
  555. // copy from the list box to the cache manager
  556. void cmCursesMainForm::FillCacheManagerFromUI()
  557. {
  558. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  559. std::string const& cacheKey = entry.Key;
  560. cmValue existingValue =
  561. this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
  562. if (existingValue) {
  563. std::string const& oldValue = *existingValue;
  564. std::string newValue = entry.Entry->GetValue();
  565. std::string fixedOldValue;
  566. std::string fixedNewValue;
  567. cmStateEnums::CacheEntryType t =
  568. this->CMakeInstance->GetState()->GetCacheEntryType(cacheKey);
  569. this->FixValue(t, oldValue, fixedOldValue);
  570. this->FixValue(t, newValue, fixedNewValue);
  571. if (!(fixedOldValue == fixedNewValue)) {
  572. // The user has changed the value. Mark it as modified.
  573. this->CMakeInstance->GetState()->SetCacheEntryBoolProperty(
  574. cacheKey, "MODIFIED", true);
  575. this->CMakeInstance->GetState()->SetCacheEntryValue(cacheKey,
  576. fixedNewValue);
  577. }
  578. }
  579. }
  580. }
  581. void cmCursesMainForm::FixValue(cmStateEnums::CacheEntryType type,
  582. std::string const& in, std::string& out) const
  583. {
  584. out = in.substr(0, in.find_last_not_of(' ') + 1);
  585. if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
  586. cmSystemTools::ConvertToUnixSlashes(out);
  587. }
  588. if (type == cmStateEnums::BOOL) {
  589. if (cmIsOff(out)) {
  590. out = "OFF";
  591. } else {
  592. out = "ON";
  593. }
  594. }
  595. }
  596. void cmCursesMainForm::SetSearchMode(bool enable)
  597. {
  598. this->SearchMode = enable;
  599. curs_set(enable);
  600. }
  601. void cmCursesMainForm::HandleInput()
  602. {
  603. int x = 0;
  604. int y = 0;
  605. if (!this->Form) {
  606. return;
  607. }
  608. FIELD* currentField;
  609. cmCursesWidget* currentWidget;
  610. char debugMessage[128];
  611. for (;;) {
  612. this->UpdateStatusBar();
  613. this->PrintKeys();
  614. if (this->SearchMode) {
  615. std::string searchstr = "Search: " + this->SearchString;
  616. this->UpdateStatusBar(searchstr);
  617. this->PrintKeys(1);
  618. curses_move(y - 5, static_cast<unsigned int>(searchstr.size()));
  619. // curses_move(1,1);
  620. touchwin(stdscr);
  621. refresh();
  622. }
  623. int key = getch();
  624. #ifdef _WIN32
  625. if (key == KEY_RESIZE) {
  626. HandleResize();
  627. }
  628. #endif // _WIN32
  629. getmaxyx(stdscr, y, x);
  630. // If window too small, handle 'q' only
  631. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  632. // quit
  633. if (key == 'q') {
  634. break;
  635. }
  636. continue;
  637. }
  638. currentField = current_field(this->Form);
  639. currentWidget =
  640. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  641. bool widgetHandled = false;
  642. if (this->SearchMode) {
  643. if (key == 10 || key == KEY_ENTER) {
  644. this->SetSearchMode(false);
  645. if (!this->SearchString.empty()) {
  646. this->JumpToCacheEntry(this->SearchString.c_str());
  647. this->OldSearchString = this->SearchString;
  648. }
  649. this->SearchString.clear();
  650. }
  651. /*
  652. else if ( key == KEY_ESCAPE )
  653. {
  654. this->SearchMode = false;
  655. }
  656. */
  657. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  658. (key >= '0' && key <= '9') || (key == '_')) {
  659. if (this->SearchString.size() <
  660. static_cast<std::string::size_type>(x - 10)) {
  661. this->SearchString += static_cast<char>(key);
  662. }
  663. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  664. if (!this->SearchString.empty()) {
  665. this->SearchString.pop_back();
  666. }
  667. }
  668. } else if (currentWidget && !this->SearchMode) {
  669. // Ask the current widget if it wants to handle input
  670. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  671. if (widgetHandled) {
  672. this->OkToGenerate = false;
  673. this->UpdateStatusBar();
  674. this->PrintKeys();
  675. }
  676. }
  677. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  678. // If the current widget does not want to handle input,
  679. // we handle it.
  680. snprintf(debugMessage, sizeof(debugMessage),
  681. "Main form handling input, key: %d", key);
  682. cmCursesForm::LogMessage(debugMessage);
  683. // quit
  684. if (key == 'q') {
  685. break;
  686. }
  687. // if not end of page, next field otherwise next page
  688. // each entry consists of fields: label, isnew, value
  689. // therefore, the label field for the prev. entry is index-5
  690. // and the label field for the next entry is index+1
  691. // (index always corresponds to the value field)
  692. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  693. // binding)
  694. if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  695. FIELD* cur = current_field(this->Form);
  696. size_t findex = field_index(cur);
  697. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  698. continue;
  699. }
  700. if (new_page(this->Fields[findex + 1])) {
  701. form_driver(this->Form, REQ_NEXT_PAGE);
  702. } else {
  703. form_driver(this->Form, REQ_NEXT_FIELD);
  704. }
  705. }
  706. // if not beginning of page, previous field, otherwise previous page
  707. // each entry consists of fields: label, isnew, value
  708. // therefore, the label field for the prev. entry is index-5
  709. // and the label field for the next entry is index+1
  710. // (index always corresponds to the value field)
  711. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  712. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  713. FIELD* cur = current_field(this->Form);
  714. int findex = field_index(cur);
  715. if (findex == 2) {
  716. continue;
  717. }
  718. if (new_page(this->Fields[findex - 2])) {
  719. form_driver(this->Form, REQ_PREV_PAGE);
  720. set_current_field(this->Form, this->Fields[findex - 3]);
  721. } else {
  722. form_driver(this->Form, REQ_PREV_FIELD);
  723. }
  724. }
  725. // pg down
  726. else if (key == KEY_NPAGE || key == ctrl('d')) {
  727. form_driver(this->Form, REQ_NEXT_PAGE);
  728. }
  729. // pg up
  730. else if (key == KEY_PPAGE || key == ctrl('u')) {
  731. form_driver(this->Form, REQ_PREV_PAGE);
  732. }
  733. // first entry
  734. else if (key == KEY_HOME) {
  735. form_driver(this->Form, REQ_FIRST_PAGE);
  736. form_driver(this->Form, REQ_FIRST_FIELD);
  737. }
  738. // last entry
  739. else if (key == KEY_END) {
  740. form_driver(this->Form, REQ_LAST_PAGE);
  741. form_driver(this->Form, REQ_LAST_FIELD);
  742. }
  743. // write and quit
  744. else if (key == 'w') {
  745. this->Write();
  746. break;
  747. }
  748. // configure
  749. else if (key == 'c') {
  750. this->Configure();
  751. }
  752. // display help
  753. else if (key == 'h') {
  754. getmaxyx(stdscr, y, x);
  755. FIELD* cur = current_field(this->Form);
  756. int findex = field_index(cur);
  757. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  758. field_userptr(this->Fields[findex - 2]));
  759. char const* curField = lbl->GetValue();
  760. cmValue helpString = nullptr;
  761. cmValue existingValue =
  762. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  763. if (existingValue) {
  764. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  765. curField, "HELPSTRING");
  766. }
  767. if (helpString) {
  768. this->HelpMessage[1] = cmStrCat("Current option is: ", curField,
  769. "\n"
  770. "Help string for this option is: ",
  771. *helpString, '\n');
  772. } else {
  773. this->HelpMessage[1] = "";
  774. }
  775. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  776. this->HelpMessage, "Help",
  777. cmCursesLongMessageForm::ScrollBehavior::NoScroll);
  778. CurrentForm = msgs;
  779. msgs->Render(1, 1, x, y);
  780. msgs->HandleInput();
  781. CurrentForm = this;
  782. this->Render(1, 1, x, y);
  783. set_current_field(this->Form, cur);
  784. }
  785. // display last errors
  786. else if (key == 'l') {
  787. getmaxyx(stdscr, y, x);
  788. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  789. this->Outputs, "CMake produced the following output",
  790. cmCursesLongMessageForm::ScrollBehavior::NoScroll);
  791. CurrentForm = msgs;
  792. msgs->Render(1, 1, x, y);
  793. msgs->HandleInput();
  794. CurrentForm = this;
  795. this->Render(1, 1, x, y);
  796. } else if (key == '/') {
  797. this->SetSearchMode(true);
  798. this->UpdateStatusBar("Search");
  799. this->PrintKeys(1);
  800. touchwin(stdscr);
  801. refresh();
  802. } else if (key == 'n') {
  803. if (!this->OldSearchString.empty()) {
  804. this->JumpToCacheEntry(this->OldSearchString.c_str());
  805. }
  806. } else if (key == 'N') {
  807. if (!this->OldSearchString.empty()) {
  808. this->JumpToCacheEntry(this->OldSearchString.c_str(), true);
  809. }
  810. }
  811. // switch advanced on/off
  812. else if (key == 't') {
  813. this->AdvancedMode = !this->AdvancedMode;
  814. getmaxyx(stdscr, y, x);
  815. this->RePost();
  816. this->Render(1, 1, x, y);
  817. }
  818. // generate and exit
  819. else if (key == 'g') {
  820. if (this->OkToGenerate) {
  821. this->Generate();
  822. break;
  823. }
  824. }
  825. // delete cache entry
  826. else if (key == 'd' && this->NumberOfVisibleEntries && !this->IsEmpty) {
  827. this->OkToGenerate = false;
  828. FIELD* cur = current_field(this->Form);
  829. size_t findex = field_index(cur);
  830. // make the next or prev. current field after deletion
  831. // each entry consists of fields: label, isnew, value
  832. // therefore, the label field for the prev. entry is findex-5
  833. // and the label field for the next entry is findex+1
  834. // (findex always corresponds to the value field)
  835. FIELD* nextCur;
  836. if (findex == 2) {
  837. nextCur = nullptr;
  838. } else if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  839. nextCur = this->Fields[findex - 5];
  840. } else {
  841. nextCur = this->Fields[findex + 1];
  842. }
  843. // Get the label widget
  844. // each entry consists of fields: label, isnew, value
  845. // therefore, the label field for the is findex-2
  846. // (findex always corresponds to the value field)
  847. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  848. field_userptr(this->Fields[findex - 2]));
  849. if (lbl) {
  850. this->CMakeInstance->GetState()->RemoveCacheEntry(lbl->GetValue());
  851. std::string nextVal;
  852. if (nextCur) {
  853. nextVal =
  854. (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))
  855. ->GetValue());
  856. }
  857. getmaxyx(stdscr, y, x);
  858. this->RemoveEntry(lbl->GetValue());
  859. this->RePost();
  860. this->Render(1, 1, x, y);
  861. if (nextCur) {
  862. // make the next or prev. current field after deletion
  863. auto nextEntryIt = std::find_if(
  864. this->Entries.begin(), this->Entries.end(),
  865. [&nextVal](cmCursesCacheEntryComposite const& entry) {
  866. return nextVal == entry.Key;
  867. });
  868. if (nextEntryIt != this->Entries.end()) {
  869. set_current_field(this->Form, nextEntryIt->Entry->Field);
  870. }
  871. }
  872. }
  873. }
  874. }
  875. touchwin(stdscr);
  876. wrefresh(stdscr);
  877. }
  878. }
  879. int cmCursesMainForm::LoadCache(char const* /*unused*/)
  880. {
  881. int r = this->CMakeInstance->LoadCache();
  882. if (r < 0) {
  883. return r;
  884. }
  885. // Process presets before loading the cache
  886. this->CMakeInstance->ProcessPresetVariables();
  887. this->CMakeInstance->ProcessPresetEnvironment();
  888. this->CMakeInstance->SetCacheArgs(this->Args);
  889. this->CMakeInstance->PreLoadCMakeFiles();
  890. return r;
  891. }
  892. void cmCursesMainForm::JumpToCacheEntry(char const* astr)
  893. {
  894. this->JumpToCacheEntry(astr, false);
  895. }
  896. void cmCursesMainForm::JumpToCacheEntry(char const* astr, bool reverse)
  897. {
  898. std::string str;
  899. if (astr) {
  900. str = cmSystemTools::LowerCase(astr);
  901. }
  902. if (str.empty()) {
  903. return;
  904. }
  905. FIELD* cur = current_field(this->Form);
  906. int start_index = field_index(cur);
  907. int findex = start_index;
  908. for (;;) {
  909. if (!str.empty()) {
  910. cmCursesWidget* lbl = nullptr;
  911. if (findex >= 0) {
  912. lbl = reinterpret_cast<cmCursesWidget*>(
  913. field_userptr(this->Fields[findex - 2]));
  914. }
  915. if (lbl) {
  916. char const* curField = lbl->GetValue();
  917. if (curField) {
  918. std::string cfld = cmSystemTools::LowerCase(curField);
  919. if (cfld.find(str) != std::string::npos && findex != start_index) {
  920. break;
  921. }
  922. }
  923. }
  924. }
  925. if (!reverse &&
  926. static_cast<size_t>(findex) >= 3 * this->NumberOfVisibleEntries - 1) {
  927. form_driver(this->Form, REQ_FIRST_PAGE);
  928. form_driver(this->Form, REQ_FIRST_FIELD);
  929. } else if (reverse && static_cast<size_t>(findex) < 3) {
  930. form_driver(this->Form, REQ_LAST_PAGE);
  931. form_driver(this->Form, REQ_LAST_FIELD);
  932. } else if (this->Fields[findex + (reverse ? -3 : 1)]->page !=
  933. this->Fields[findex]->page) {
  934. form_driver(this->Form, reverse ? REQ_PREV_PAGE : REQ_NEXT_PAGE);
  935. if (reverse) {
  936. form_driver(this->Form, REQ_LAST_FIELD);
  937. }
  938. } else {
  939. form_driver(this->Form, reverse ? REQ_PREV_FIELD : REQ_NEXT_FIELD);
  940. }
  941. cur = current_field(this->Form);
  942. findex = field_index(cur);
  943. if (findex == start_index) {
  944. break;
  945. }
  946. }
  947. }
  948. void cmCursesMainForm::ResetOutputs()
  949. {
  950. this->LogForm.reset();
  951. this->Outputs.clear();
  952. this->HasNonStatusOutputs = false;
  953. this->LastProgress.clear();
  954. }
  955. void cmCursesMainForm::DisplayOutputs(std::string const& newOutput)
  956. {
  957. int xi;
  958. int yi;
  959. getmaxyx(stdscr, yi, xi);
  960. if (CurrentForm != this->LogForm.get()) {
  961. auto* newLogForm = new cmCursesLongMessageForm(
  962. this->Outputs, this->LastProgress.c_str(),
  963. cmCursesLongMessageForm::ScrollBehavior::ScrollDown);
  964. CurrentForm = newLogForm;
  965. this->LogForm.reset(newLogForm);
  966. this->LogForm->Render(1, 1, xi, yi);
  967. } else {
  968. this->LogForm->UpdateContent(newOutput, this->LastProgress);
  969. }
  970. }
  971. char const* cmCursesMainForm::s_ConstHelpMessage =
  972. "CMake is used to configure and generate build files for software projects. "
  973. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  974. "1. Run ccmake in the directory where you want the object and executable "
  975. "files to be placed (build directory). If the source directory is not the "
  976. "same as this build directory, you have to specify it as an argument on the "
  977. "command line.\n\n"
  978. "2. When ccmake is run, it will read the configuration files and display "
  979. "the current build options. "
  980. "If you have run CMake before and have updated the configuration files "
  981. "since then, any new entries will be displayed on top and will be marked "
  982. "with a *. "
  983. "On the other hand, the first time you run ccmake, all build options will "
  984. "be new and will be marked as such. "
  985. "At this point, you can modify any options (see keys below) you want to "
  986. "change. "
  987. "When you are satisfied with your changes, press 'c' to have CMake process "
  988. "the configuration files. "
  989. "Please note that changing some options may cause new ones to appear. These "
  990. "will be shown on top and will be marked with *. "
  991. "Repeat this procedure until you are satisfied with all the options and "
  992. "there are no new entries. "
  993. "At this point, a new command will appear: G)enerate and Exit. You can now "
  994. "hit 'g' to have CMake generate all the build files (i.e. makefiles or "
  995. "project files) and exit. "
  996. "At any point during the process, you can exit ccmake with 'q'. However, "
  997. "this will not generate/change any build files. Additionally, you can exit "
  998. "ccmake with 'w' to write changes to the cache file without generating or "
  999. "changing the build files.\n\n"
  1000. "ccmake KEYS:\n\n"
  1001. "Navigation: "
  1002. "You can use the arrow keys and page up, down to navigate the options. "
  1003. "Additionally, you can use the following keys: \n"
  1004. " C-n or j : next option\n"
  1005. " C-p or k : previous options\n"
  1006. " C-d : down one page\n"
  1007. " C-u : up one page\n"
  1008. " Home : jump to first option\n"
  1009. " End : jump to last option\n"
  1010. " n : next search result\n"
  1011. " N : previous search result\n\n"
  1012. "Editing options: "
  1013. "To change an option press enter or return. If the current options is a "
  1014. "boolean, this will toggle its value. "
  1015. "Otherwise, ccmake will enter edit mode. Alternatively, you can toggle "
  1016. "a bool variable by pressing space, and enter edit mode with i."
  1017. "In this mode you can edit an option using arrow keys and backspace. "
  1018. "Alternatively, you can use the following keys:\n"
  1019. " C-b : back one character\n"
  1020. " C-f : forward one character\n"
  1021. " C-a : go to the beginning of the field\n"
  1022. " C-e : go to the end of the field\n"
  1023. " C-d : delete previous character\n"
  1024. " C-k : kill the rest of the field\n"
  1025. " Esc : Restore field (discard last changes)\n"
  1026. " Enter : Leave edit mode\n"
  1027. "Commands:\n"
  1028. " q : quit ccmake without generating build files\n"
  1029. " h : help, shows this screen\n"
  1030. " c : process the configuration files with the current options\n"
  1031. " g : generate build files and exit, only available when there are no "
  1032. "new options and no errors have been detected during last configuration.\n"
  1033. " l : shows cmake output\n"
  1034. " d : delete an option\n"
  1035. " t : toggles advanced mode. In normal mode, only the most important "
  1036. "options are shown. In advanced mode, all options are shown. We recommend "
  1037. "using normal mode unless you are an expert.\n"
  1038. " / : search for a variable name.\n";