cmCursesMainForm.cxx 34 KB

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