cmCursesMainForm.cxx 34 KB

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