cmCursesMainForm.cxx 34 KB

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