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 =
  38. cm::make_unique<cmake>(cmake::RoleProject, cmState::Project);
  39. this->CMakeInstance->SetCMakeEditCommand(
  40. cmSystemTools::GetCMakeCursesCommand());
  41. // create the arguments for the cmake object
  42. std::string whereCMake =
  43. cmStrCat(cmSystemTools::GetProgramPath(this->Args[0]), "/cmake");
  44. this->Args[0] = whereCMake;
  45. this->CMakeInstance->SetArgs(this->Args);
  46. }
  47. cmCursesMainForm::~cmCursesMainForm()
  48. {
  49. if (this->Form) {
  50. unpost_form(this->Form);
  51. free_form(this->Form);
  52. this->Form = nullptr;
  53. }
  54. }
  55. // See if a cache entry is in the list of entries in the ui.
  56. bool cmCursesMainForm::LookForCacheEntry(std::string const& key)
  57. {
  58. return std::any_of(this->Entries.begin(), this->Entries.end(),
  59. [&key](cmCursesCacheEntryComposite const& entry) {
  60. return key == entry.Key;
  61. });
  62. }
  63. // Create new cmCursesCacheEntryComposite entries from the cache
  64. void cmCursesMainForm::InitializeUI()
  65. {
  66. // Hide the cursor by default
  67. curs_set(0);
  68. // Create a vector of cmCursesCacheEntryComposite's
  69. // which contain labels, entries and new entry markers
  70. std::vector<cmCursesCacheEntryComposite> newEntries;
  71. std::vector<std::string> cacheKeys =
  72. this->CMakeInstance->GetState()->GetCacheEntryKeys();
  73. newEntries.reserve(cacheKeys.size());
  74. // Count non-internal and non-static entries
  75. int count = 0;
  76. for (std::string const& key : cacheKeys) {
  77. cmStateEnums::CacheEntryType t =
  78. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  79. if (t != cmStateEnums::INTERNAL && t != cmStateEnums::STATIC &&
  80. t != cmStateEnums::UNINITIALIZED) {
  81. ++count;
  82. }
  83. }
  84. int entrywidth = this->InitialWidth - 35;
  85. // Add a label to display when cache is empty
  86. // dummy entry widget (does not respond to input)
  87. this->EmptyCacheEntry =
  88. cm::make_unique<cmCursesCacheEntryComposite>("EMPTY CACHE", 30, 30);
  89. this->EmptyCacheEntry->Entry =
  90. cm::make_unique<cmCursesDummyWidget>(1, 1, 1, 1);
  91. if (count > 0) {
  92. // Create the composites.
  93. // First add entries which are new
  94. for (std::string const& key : cacheKeys) {
  95. cmStateEnums::CacheEntryType t =
  96. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  97. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  98. t == cmStateEnums::UNINITIALIZED) {
  99. continue;
  100. }
  101. if (!this->LookForCacheEntry(key)) {
  102. newEntries.emplace_back(key, this->CMakeInstance->GetState(), true, 30,
  103. entrywidth);
  104. this->OkToGenerate = false;
  105. }
  106. }
  107. // then add entries which are old
  108. for (std::string const& key : cacheKeys) {
  109. cmStateEnums::CacheEntryType t =
  110. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  111. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  112. t == cmStateEnums::UNINITIALIZED) {
  113. continue;
  114. }
  115. if (this->LookForCacheEntry(key)) {
  116. newEntries.emplace_back(key, this->CMakeInstance->GetState(), false,
  117. 30, entrywidth);
  118. }
  119. }
  120. }
  121. // Replace old entries
  122. this->Entries = std::move(newEntries);
  123. // Compute fields from composites
  124. this->RePost();
  125. }
  126. void cmCursesMainForm::RePost()
  127. {
  128. // Create the fields to be passed to the form.
  129. if (this->Form) {
  130. unpost_form(this->Form);
  131. free_form(this->Form);
  132. this->Form = nullptr;
  133. }
  134. this->Fields.clear();
  135. if (this->AdvancedMode) {
  136. this->NumberOfVisibleEntries = this->Entries.size();
  137. } else {
  138. // If normal mode, count only non-advanced entries
  139. this->NumberOfVisibleEntries = 0;
  140. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  141. cmValue existingValue =
  142. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  143. bool advanced =
  144. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  145. entry.GetValue(), "ADVANCED");
  146. if (!existingValue || (!this->AdvancedMode && advanced)) {
  147. continue;
  148. }
  149. this->NumberOfVisibleEntries++;
  150. }
  151. }
  152. // there is always one even if it is the dummy one
  153. if (this->NumberOfVisibleEntries == 0) {
  154. this->NumberOfVisibleEntries = 1;
  155. }
  156. // Assign the fields: 3 for each entry: label, new entry marker
  157. // ('*' or ' ') and entry widget
  158. this->Fields.reserve(3 * this->NumberOfVisibleEntries + 1);
  159. // Assign fields
  160. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  161. cmValue existingValue =
  162. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  163. bool advanced =
  164. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  165. entry.GetValue(), "ADVANCED");
  166. if (!existingValue || (!this->AdvancedMode && advanced)) {
  167. continue;
  168. }
  169. this->Fields.push_back(entry.Label->Field);
  170. this->Fields.push_back(entry.IsNewLabel->Field);
  171. this->Fields.push_back(entry.Entry->Field);
  172. }
  173. // if no cache entries there should still be one dummy field
  174. this->IsEmpty = this->Fields.empty();
  175. if (this->IsEmpty) {
  176. this->Fields.push_back(this->EmptyCacheEntry->Label->Field);
  177. this->Fields.push_back(this->EmptyCacheEntry->IsNewLabel->Field);
  178. this->Fields.push_back(this->EmptyCacheEntry->Entry->Field);
  179. this->NumberOfVisibleEntries = 1;
  180. }
  181. // Has to be null terminated.
  182. this->Fields.push_back(nullptr);
  183. }
  184. void cmCursesMainForm::Render(int left, int top, int width, int height)
  185. {
  186. if (this->Form) {
  187. FIELD* currentField = current_field(this->Form);
  188. cmCursesWidget* cw =
  189. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  190. // If in edit mode, get out of it
  191. if (cw->GetType() == cmStateEnums::STRING ||
  192. cw->GetType() == cmStateEnums::PATH ||
  193. cw->GetType() == cmStateEnums::FILEPATH) {
  194. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  195. sw->SetInEdit(false);
  196. }
  197. // Delete the previous form
  198. unpost_form(this->Form);
  199. free_form(this->Form);
  200. this->Form = nullptr;
  201. }
  202. // Wrong window size
  203. if (width < cmCursesMainForm::MIN_WIDTH || width < this->InitialWidth ||
  204. height < cmCursesMainForm::MIN_HEIGHT) {
  205. return;
  206. }
  207. // Leave room for toolbar
  208. height -= 7;
  209. if (this->AdvancedMode) {
  210. this->NumberOfVisibleEntries = this->Entries.size();
  211. } else {
  212. // If normal, display only non-advanced entries
  213. this->NumberOfVisibleEntries = 0;
  214. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  215. cmValue existingValue =
  216. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  217. bool advanced =
  218. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  219. entry.GetValue(), "ADVANCED");
  220. if (!existingValue || (!this->AdvancedMode && advanced)) {
  221. continue;
  222. }
  223. this->NumberOfVisibleEntries++;
  224. }
  225. }
  226. // Re-adjust the fields according to their place
  227. this->NumberOfPages = 1;
  228. if (height > 0) {
  229. bool isNewPage;
  230. int i = 0;
  231. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  232. cmValue existingValue =
  233. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  234. bool advanced =
  235. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  236. entry.GetValue(), "ADVANCED");
  237. if (!existingValue || (!this->AdvancedMode && advanced)) {
  238. continue;
  239. }
  240. int row = (i % height) + 1;
  241. int page = (i / height) + 1;
  242. isNewPage = (page > 1) && (row == 1);
  243. if (isNewPage) {
  244. this->NumberOfPages++;
  245. }
  246. entry.Label->Move(left, top + row - 1, isNewPage);
  247. entry.IsNewLabel->Move(left + 30, top + row - 1, false);
  248. entry.Entry->Move(left + 33, top + row - 1, false);
  249. entry.Entry->SetPage(this->NumberOfPages);
  250. i++;
  251. }
  252. }
  253. // Post the form
  254. this->Form = new_form(this->Fields.data());
  255. post_form(this->Form);
  256. // Update toolbar
  257. this->UpdateStatusBar();
  258. this->PrintKeys();
  259. touchwin(stdscr);
  260. refresh();
  261. }
  262. void cmCursesMainForm::PrintKeys(int process /* = 0 */)
  263. {
  264. int x;
  265. int y;
  266. getmaxyx(stdscr, y, x);
  267. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  268. y < cmCursesMainForm::MIN_HEIGHT) {
  269. return;
  270. }
  271. // Give the current widget (if it exists), a chance to print keys
  272. cmCursesWidget* cw = nullptr;
  273. if (this->Form) {
  274. FIELD* currentField = current_field(this->Form);
  275. cw = reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  276. }
  277. char fmt_s[] = "%s";
  278. if (!cw || !cw->PrintKeys()) {
  279. char firstLine[512] = "";
  280. char secondLine[512] = "";
  281. char thirdLine[512] = "";
  282. if (process) {
  283. memset(firstLine, ' ', 68);
  284. memset(secondLine, ' ', 68);
  285. memset(thirdLine, ' ', 68);
  286. } else {
  287. if (this->OkToGenerate) {
  288. snprintf(firstLine, sizeof(firstLine),
  289. " [l] Show log output [c] Configure"
  290. " [g] Generate ");
  291. } else {
  292. snprintf(firstLine, sizeof(firstLine),
  293. " [l] Show log output [c] Configure"
  294. " ");
  295. }
  296. {
  297. char const* toggleKeyInstruction =
  298. " [t] Toggle advanced mode (currently %s)";
  299. snprintf(thirdLine, sizeof(thirdLine), toggleKeyInstruction,
  300. this->AdvancedMode ? "on" : "off");
  301. }
  302. snprintf(secondLine, sizeof(secondLine),
  303. " [h] Help [q] Quit without generating");
  304. }
  305. curses_move(y - 4, 0);
  306. char fmt[512] = "Keys: [enter] Edit an entry [d] Delete an entry";
  307. if (process) {
  308. memset(fmt, ' ', 57);
  309. }
  310. printw(fmt_s, fmt);
  311. curses_move(y - 3, 0);
  312. printw(fmt_s, firstLine);
  313. curses_move(y - 2, 0);
  314. printw(fmt_s, secondLine);
  315. curses_move(y - 1, 0);
  316. printw(fmt_s, thirdLine);
  317. }
  318. if (cw) {
  319. char pageLine[512] = "";
  320. snprintf(pageLine, sizeof(pageLine), "Page %d of %d", cw->GetPage(),
  321. this->NumberOfPages);
  322. curses_move(0, 65 - static_cast<unsigned int>(strlen(pageLine)) - 1);
  323. printw(fmt_s, pageLine);
  324. }
  325. pos_form_cursor(this->Form);
  326. }
  327. // Print the key of the current entry and the CMake version
  328. // on the status bar. Designed for a width of 80 chars.
  329. void cmCursesMainForm::UpdateStatusBar(cm::optional<std::string> message)
  330. {
  331. int x;
  332. int y;
  333. getmaxyx(stdscr, y, x);
  334. // If window size is too small, display error and return
  335. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  336. y < cmCursesMainForm::MIN_HEIGHT) {
  337. curses_clear();
  338. curses_move(0, 0);
  339. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  340. printw(fmt,
  341. (cmCursesMainForm::MIN_WIDTH < this->InitialWidth
  342. ? this->InitialWidth
  343. : cmCursesMainForm::MIN_WIDTH),
  344. cmCursesMainForm::MIN_HEIGHT);
  345. touchwin(stdscr);
  346. wrefresh(stdscr);
  347. return;
  348. }
  349. // Find the current label index
  350. // Field are grouped by 3, the label should be 2 less than the current index
  351. using size_type = decltype(this->Fields)::size_type;
  352. size_type currentLabelIndex = field_index(current_field(this->Form)) - 2;
  353. // Use the status message if any, otherwise join the key and help string
  354. std::string bar;
  355. if (message) {
  356. bar = *message;
  357. } else {
  358. // Get the key of the current entry
  359. cmCursesWidget* labelWidget = reinterpret_cast<cmCursesWidget*>(
  360. field_userptr(this->Fields[currentLabelIndex]));
  361. std::string labelValue = labelWidget->GetValue();
  362. bar = labelValue + ": ";
  363. // Get the help string of the current entry
  364. // and add it to the help string
  365. auto* cmakeState = this->CMakeInstance->GetState();
  366. cmValue existingValue = cmakeState->GetCacheEntryValue(labelValue);
  367. if (existingValue) {
  368. cmValue help =
  369. cmakeState->GetCacheEntryProperty(labelValue, "HELPSTRING");
  370. if (help) {
  371. bar += *help;
  372. }
  373. }
  374. }
  375. // Pad with spaces to erase any previous text,
  376. // or truncate as necessary to fit the screen
  377. bar.resize(x, ' ');
  378. curses_move(y - 5, 0);
  379. attron(A_STANDOUT);
  380. char fmt_s[] = "%s";
  381. printw(fmt_s, bar.c_str());
  382. attroff(A_STANDOUT);
  383. // Highlight the current label, reset others
  384. // Fields are grouped by 3, the first one being the label
  385. // so start at 0 and move up by 3 avoiding the last null entry
  386. for (size_type index = 0; index < this->Fields.size() - 1; index += 3) {
  387. int attr = (index == currentLabelIndex) ? A_STANDOUT : A_NORMAL;
  388. set_field_fore(this->Fields[index], attr);
  389. set_field_back(this->Fields[index], attr);
  390. set_field_fore(this->Fields[index + 1], attr);
  391. set_field_back(this->Fields[index + 1], attr);
  392. }
  393. // Display CMake version under the status bar
  394. // We want to display this on the right
  395. std::string version = "CMake Version ";
  396. version += cmVersion::GetCMakeVersion();
  397. version.resize(std::min<std::string::size_type>(x, version.size()));
  398. curses_move(y - 4, x - static_cast<int>(version.size()));
  399. printw(fmt_s, version.c_str());
  400. pos_form_cursor(this->Form);
  401. }
  402. void cmCursesMainForm::UpdateProgress(std::string const& msg, float prog)
  403. {
  404. if (prog >= 0) {
  405. constexpr int progressBarWidth = 40;
  406. int progressBarCompleted = static_cast<int>(progressBarWidth * prog);
  407. int percentCompleted = static_cast<int>(100 * prog);
  408. this->LastProgress = (percentCompleted < 100 ? " " : "");
  409. this->LastProgress += (percentCompleted < 10 ? " " : "");
  410. this->LastProgress += std::to_string(percentCompleted) + "% [";
  411. this->LastProgress.append(progressBarCompleted, '#');
  412. this->LastProgress.append(progressBarWidth - progressBarCompleted, ' ');
  413. this->LastProgress += "] " + msg + "...";
  414. this->DisplayOutputs(std::string());
  415. } else {
  416. this->Outputs.emplace_back(msg);
  417. this->DisplayOutputs(msg);
  418. }
  419. }
  420. void cmCursesMainForm::Write()
  421. {
  422. this->FillCacheManagerFromUI();
  423. this->CMakeInstance->SaveCache(
  424. this->CMakeInstance->GetHomeOutputDirectory());
  425. this->LoadCache(nullptr);
  426. }
  427. int cmCursesMainForm::Configure(int noconfigure)
  428. {
  429. this->ResetOutputs();
  430. if (noconfigure == 0) {
  431. this->UpdateProgress("Configuring", 0);
  432. this->CMakeInstance->SetProgressCallback(
  433. [this](std::string const& msg, float prog) {
  434. this->UpdateProgress(msg, prog);
  435. });
  436. }
  437. // always save the current gui values to disk
  438. this->Write();
  439. // run the generate process
  440. this->OkToGenerate = true;
  441. int retVal;
  442. if (noconfigure) {
  443. retVal = this->CMakeInstance->DoPreConfigureChecks();
  444. this->OkToGenerate = false;
  445. if (retVal > 0) {
  446. retVal = 0;
  447. }
  448. } else {
  449. retVal = this->CMakeInstance->Configure();
  450. }
  451. this->CMakeInstance->SetProgressCallback(nullptr);
  452. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  453. if (retVal != 0 || this->HasNonStatusOutputs) {
  454. // see if there was an error
  455. if (cmSystemTools::GetErrorOccurredFlag()) {
  456. this->OkToGenerate = false;
  457. }
  458. int xx;
  459. int yy;
  460. getmaxyx(stdscr, yy, xx);
  461. char const* title = "Configure produced the following output";
  462. if (cmSystemTools::GetErrorOccurredFlag()) {
  463. title = "Configure failed with the following output";
  464. }
  465. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  466. this->Outputs, title,
  467. cmCursesLongMessageForm::ScrollBehavior::ScrollDown);
  468. // reset error condition
  469. cmSystemTools::ResetErrorOccurredFlag();
  470. CurrentForm = msgs;
  471. msgs->Render(1, 1, xx, yy);
  472. msgs->HandleInput();
  473. // If they typed the wrong source directory, we report
  474. // an error and exit
  475. if (retVal == -2) {
  476. return retVal;
  477. }
  478. }
  479. this->InitializeUI();
  480. CurrentForm = this;
  481. int xi;
  482. int yi;
  483. getmaxyx(stdscr, yi, xi);
  484. this->Render(1, 1, xi, yi);
  485. return 0;
  486. }
  487. int cmCursesMainForm::Generate()
  488. {
  489. this->ResetOutputs();
  490. this->UpdateProgress("Generating", 0);
  491. this->CMakeInstance->SetProgressCallback(
  492. [this](std::string const& msg, float prog) {
  493. this->UpdateProgress(msg, prog);
  494. });
  495. // run the generate process
  496. int retVal = this->CMakeInstance->Generate();
  497. this->CMakeInstance->SetProgressCallback(nullptr);
  498. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  499. if (retVal != 0 || this->HasNonStatusOutputs) {
  500. // see if there was an error
  501. if (cmSystemTools::GetErrorOccurredFlag()) {
  502. this->OkToGenerate = false;
  503. }
  504. // reset error condition
  505. cmSystemTools::ResetErrorOccurredFlag();
  506. int xx;
  507. int yy;
  508. getmaxyx(stdscr, yy, xx);
  509. char const* title = "Generate produced the following output";
  510. if (cmSystemTools::GetErrorOccurredFlag()) {
  511. title = "Generate failed with the following output";
  512. }
  513. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  514. this->Outputs, title,
  515. cmCursesLongMessageForm::ScrollBehavior::ScrollDown);
  516. CurrentForm = msgs;
  517. msgs->Render(1, 1, xx, yy);
  518. msgs->HandleInput();
  519. // If they typed the wrong source directory, we report
  520. // an error and exit
  521. if (retVal == -2) {
  522. return retVal;
  523. }
  524. }
  525. this->InitializeUI();
  526. CurrentForm = this;
  527. int xi;
  528. int yi;
  529. getmaxyx(stdscr, yi, xi);
  530. this->Render(1, 1, xi, yi);
  531. return 0;
  532. }
  533. void cmCursesMainForm::AddError(std::string const& message,
  534. char const* /*unused*/)
  535. {
  536. this->Outputs.emplace_back(message);
  537. this->HasNonStatusOutputs = true;
  538. this->DisplayOutputs(message);
  539. }
  540. void cmCursesMainForm::RemoveEntry(char const* value)
  541. {
  542. if (!value) {
  543. return;
  544. }
  545. auto removeIt =
  546. std::find_if(this->Entries.begin(), this->Entries.end(),
  547. [value](cmCursesCacheEntryComposite& entry) -> bool {
  548. char const* val = entry.GetValue();
  549. return val && !strcmp(value, val);
  550. });
  551. if (removeIt != this->Entries.end()) {
  552. this->CMakeInstance->UnwatchUnusedCli(value);
  553. this->Entries.erase(removeIt);
  554. }
  555. }
  556. // copy from the list box to the cache manager
  557. void cmCursesMainForm::FillCacheManagerFromUI()
  558. {
  559. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  560. std::string const& cacheKey = entry.Key;
  561. cmValue existingValue =
  562. this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
  563. if (existingValue) {
  564. std::string const& oldValue = *existingValue;
  565. std::string newValue = entry.Entry->GetValue();
  566. std::string fixedOldValue;
  567. std::string fixedNewValue;
  568. cmStateEnums::CacheEntryType t =
  569. this->CMakeInstance->GetState()->GetCacheEntryType(cacheKey);
  570. this->FixValue(t, oldValue, fixedOldValue);
  571. this->FixValue(t, newValue, fixedNewValue);
  572. if (!(fixedOldValue == fixedNewValue)) {
  573. // The user has changed the value. Mark it as modified.
  574. this->CMakeInstance->GetState()->SetCacheEntryBoolProperty(
  575. cacheKey, "MODIFIED", true);
  576. this->CMakeInstance->GetState()->SetCacheEntryValue(cacheKey,
  577. fixedNewValue);
  578. }
  579. }
  580. }
  581. }
  582. void cmCursesMainForm::FixValue(cmStateEnums::CacheEntryType type,
  583. std::string const& in, std::string& out) const
  584. {
  585. out = in.substr(0, in.find_last_not_of(' ') + 1);
  586. if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
  587. cmSystemTools::ConvertToUnixSlashes(out);
  588. }
  589. if (type == cmStateEnums::BOOL) {
  590. if (cmIsOff(out)) {
  591. out = "OFF";
  592. } else {
  593. out = "ON";
  594. }
  595. }
  596. }
  597. void cmCursesMainForm::SetSearchMode(bool enable)
  598. {
  599. this->SearchMode = enable;
  600. curs_set(enable);
  601. }
  602. void cmCursesMainForm::HandleInput()
  603. {
  604. int x = 0;
  605. int y = 0;
  606. if (!this->Form) {
  607. return;
  608. }
  609. FIELD* currentField;
  610. cmCursesWidget* currentWidget;
  611. char debugMessage[128];
  612. for (;;) {
  613. this->UpdateStatusBar();
  614. this->PrintKeys();
  615. if (this->SearchMode) {
  616. std::string searchstr = "Search: " + this->SearchString;
  617. this->UpdateStatusBar(searchstr);
  618. this->PrintKeys(1);
  619. curses_move(y - 5, static_cast<unsigned int>(searchstr.size()));
  620. // curses_move(1,1);
  621. touchwin(stdscr);
  622. refresh();
  623. }
  624. int key = getch();
  625. #ifdef _WIN32
  626. if (key == KEY_RESIZE) {
  627. HandleResize();
  628. }
  629. #endif // _WIN32
  630. getmaxyx(stdscr, y, x);
  631. // If window too small, handle 'q' only
  632. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  633. // quit
  634. if (key == 'q') {
  635. break;
  636. }
  637. continue;
  638. }
  639. currentField = current_field(this->Form);
  640. currentWidget =
  641. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  642. bool widgetHandled = false;
  643. if (this->SearchMode) {
  644. if (key == 10 || key == KEY_ENTER) {
  645. this->SetSearchMode(false);
  646. if (!this->SearchString.empty()) {
  647. this->JumpToCacheEntry(this->SearchString.c_str());
  648. this->OldSearchString = this->SearchString;
  649. }
  650. this->SearchString.clear();
  651. }
  652. /*
  653. else if ( key == KEY_ESCAPE )
  654. {
  655. this->SearchMode = false;
  656. }
  657. */
  658. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  659. (key >= '0' && key <= '9') || (key == '_')) {
  660. if (this->SearchString.size() <
  661. static_cast<std::string::size_type>(x - 10)) {
  662. this->SearchString += static_cast<char>(key);
  663. }
  664. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  665. if (!this->SearchString.empty()) {
  666. this->SearchString.pop_back();
  667. }
  668. }
  669. } else if (currentWidget && !this->SearchMode) {
  670. // Ask the current widget if it wants to handle input
  671. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  672. if (widgetHandled) {
  673. this->OkToGenerate = false;
  674. this->UpdateStatusBar();
  675. this->PrintKeys();
  676. }
  677. }
  678. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  679. // If the current widget does not want to handle input,
  680. // we handle it.
  681. snprintf(debugMessage, sizeof(debugMessage),
  682. "Main form handling input, key: %d", key);
  683. cmCursesForm::LogMessage(debugMessage);
  684. // quit
  685. if (key == 'q') {
  686. break;
  687. }
  688. // if not end of page, next field otherwise next page
  689. // each entry consists of fields: label, isnew, value
  690. // therefore, the label field for the prev. entry is index-5
  691. // and the label field for the next entry is index+1
  692. // (index always corresponds to the value field)
  693. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  694. // binding)
  695. if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  696. FIELD* cur = current_field(this->Form);
  697. size_t findex = field_index(cur);
  698. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  699. continue;
  700. }
  701. if (new_page(this->Fields[findex + 1])) {
  702. form_driver(this->Form, REQ_NEXT_PAGE);
  703. } else {
  704. form_driver(this->Form, REQ_NEXT_FIELD);
  705. }
  706. }
  707. // if not beginning of page, previous field, otherwise previous page
  708. // each entry consists of fields: label, isnew, value
  709. // therefore, the label field for the prev. entry is index-5
  710. // and the label field for the next entry is index+1
  711. // (index always corresponds to the value field)
  712. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  713. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  714. FIELD* cur = current_field(this->Form);
  715. int findex = field_index(cur);
  716. if (findex == 2) {
  717. continue;
  718. }
  719. if (new_page(this->Fields[findex - 2])) {
  720. form_driver(this->Form, REQ_PREV_PAGE);
  721. set_current_field(this->Form, this->Fields[findex - 3]);
  722. } else {
  723. form_driver(this->Form, REQ_PREV_FIELD);
  724. }
  725. }
  726. // pg down
  727. else if (key == KEY_NPAGE || key == ctrl('d')) {
  728. form_driver(this->Form, REQ_NEXT_PAGE);
  729. }
  730. // pg up
  731. else if (key == KEY_PPAGE || key == ctrl('u')) {
  732. form_driver(this->Form, REQ_PREV_PAGE);
  733. }
  734. // first entry
  735. else if (key == KEY_HOME) {
  736. form_driver(this->Form, REQ_FIRST_PAGE);
  737. form_driver(this->Form, REQ_FIRST_FIELD);
  738. }
  739. // last entry
  740. else if (key == KEY_END) {
  741. form_driver(this->Form, REQ_LAST_PAGE);
  742. form_driver(this->Form, REQ_LAST_FIELD);
  743. }
  744. // write and quit
  745. else if (key == 'w') {
  746. this->Write();
  747. break;
  748. }
  749. // configure
  750. else if (key == 'c') {
  751. this->Configure();
  752. }
  753. // display help
  754. else if (key == 'h') {
  755. getmaxyx(stdscr, y, x);
  756. FIELD* cur = current_field(this->Form);
  757. int findex = field_index(cur);
  758. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  759. field_userptr(this->Fields[findex - 2]));
  760. char const* curField = lbl->GetValue();
  761. cmValue helpString = nullptr;
  762. cmValue existingValue =
  763. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  764. if (existingValue) {
  765. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  766. curField, "HELPSTRING");
  767. }
  768. if (helpString) {
  769. this->HelpMessage[1] =
  770. cmStrCat("Current option is: ", curField, '\n',
  771. "Help string for this option is: ", *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";