Console.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. #include "Console.h"
  2. #include <iostream>
  3. #ifndef WIN32
  4. # include <sys/ioctl.h>
  5. # include <termios.h>
  6. # include <unistd.h>
  7. #endif
  8. using namespace Framework;
  9. ConsoleCommand::ConsoleCommand(Text name)
  10. : ReferenceCounter(),
  11. name(name)
  12. {}
  13. ConsoleCommand::~ConsoleCommand() {}
  14. const Text& Framework::ConsoleCommand::getName() const
  15. {
  16. return name;
  17. }
  18. Framework::StickyConsoleContent::StickyConsoleContent()
  19. : ReferenceCounter(),
  20. length(0),
  21. content(0),
  22. color(0),
  23. backgroundColor(0),
  24. zConsoleHandler(0)
  25. {}
  26. Framework::StickyConsoleContent::~StickyConsoleContent()
  27. {
  28. delete[] content;
  29. delete[] color;
  30. delete[] backgroundColor;
  31. }
  32. int Framework::StickyConsoleContent::getLength() const
  33. {
  34. return length;
  35. }
  36. void Framework::StickyConsoleContent::setContent(
  37. int length, const char* content)
  38. {
  39. delete[] this->content;
  40. this->content = new char[length];
  41. memcpy(this->content, content, length);
  42. this->length = length;
  43. delete[] color;
  44. delete[] backgroundColor;
  45. color = 0;
  46. backgroundColor = 0;
  47. }
  48. void Framework::StickyConsoleContent::setContent(
  49. int length, const char* content, Color color)
  50. {
  51. setContent(length, content);
  52. this->color = new Color[length];
  53. for (int i = 0; i < length; i++)
  54. {
  55. this->color[i] = color;
  56. }
  57. }
  58. void Framework::StickyConsoleContent::setContent(
  59. int length, const char* content, Color color, Color backgroundColor)
  60. {
  61. setContent(length, content, color);
  62. this->backgroundColor = new Color[length];
  63. for (int i = 0; i < length; i++)
  64. {
  65. this->backgroundColor[i] = backgroundColor;
  66. }
  67. }
  68. void Framework::StickyConsoleContent::setContent(
  69. int length, const char* content, Color* color)
  70. {
  71. setContent(length, content);
  72. this->color = new Color[length];
  73. for (int i = 0; i < length; i++)
  74. {
  75. this->color[i] = color[i];
  76. }
  77. }
  78. void Framework::StickyConsoleContent::setContent(
  79. int length, const char* content, Color* color, Color* backgroundColor)
  80. {
  81. setContent(length, content, color);
  82. this->backgroundColor = new Color[length];
  83. for (int i = 0; i < length; i++)
  84. {
  85. this->backgroundColor[i] = backgroundColor[i];
  86. }
  87. }
  88. void Framework::StickyConsoleContent::repaceContent(
  89. int start, int length, int newLength, const char* newContent)
  90. {
  91. int resultLength = this->length + newLength - length;
  92. char* resultContent = new char[resultLength];
  93. memcpy(resultContent, content, start);
  94. memcpy(resultContent + start, newContent, newLength);
  95. memcpy(resultContent + start + newLength,
  96. content + start + length,
  97. this->length - start - length);
  98. delete[] content;
  99. content = resultContent;
  100. if (color)
  101. {
  102. Color* resultColor = new Color[resultLength];
  103. memcpy(resultColor, color, start);
  104. for (int i = 0; i < newLength; i++)
  105. {
  106. resultColor[start + i] = color[start];
  107. }
  108. memcpy(resultColor + start + newLength,
  109. color + start + length,
  110. sizeof(Color) * (this->length - start - length));
  111. delete[] color;
  112. color = resultColor;
  113. }
  114. if (backgroundColor)
  115. {
  116. Color* resultBgColor = new Color[resultLength];
  117. memcpy(resultBgColor, backgroundColor, start);
  118. for (int i = 0; i < newLength; i++)
  119. {
  120. resultBgColor[start + i] = backgroundColor[start];
  121. }
  122. memcpy(resultBgColor + start + newLength,
  123. backgroundColor + start + length,
  124. sizeof(Color) * (this->length - start - length));
  125. delete[] backgroundColor;
  126. backgroundColor = resultBgColor;
  127. }
  128. this->length = resultLength;
  129. }
  130. void Framework::StickyConsoleContent::repaceContent(
  131. int start, int length, int newLength, const char* newContent, Color color)
  132. {
  133. repaceContent(start, length, newLength, newContent);
  134. if (!this->color)
  135. {
  136. this->color = new Color[this->length];
  137. for (int i = 0; i < this->length; i++)
  138. {
  139. this->color[i] = (Color)-1;
  140. }
  141. }
  142. for (int i = start; i < start + newLength; i++)
  143. {
  144. this->color[i] = color;
  145. }
  146. }
  147. void Framework::StickyConsoleContent::repaceContent(int start,
  148. int length,
  149. int newLength,
  150. const char* newContent,
  151. Color color,
  152. Color backgroundColor)
  153. {
  154. repaceContent(start, length, newLength, newContent, color);
  155. if (!this->backgroundColor)
  156. {
  157. this->backgroundColor = new Color[this->length];
  158. for (int i = 0; i < this->length; i++)
  159. {
  160. this->backgroundColor[i] = (Color)-1;
  161. }
  162. }
  163. for (int i = start; i < start + newLength; i++)
  164. {
  165. this->backgroundColor[i] = backgroundColor;
  166. }
  167. }
  168. void Framework::StickyConsoleContent::repaceContent(
  169. int start, int length, int newLength, const char* newContent, Color* color)
  170. {
  171. repaceContent(start, length, newLength, newContent);
  172. if (!this->color)
  173. {
  174. this->color = new Color[this->length];
  175. for (int i = 0; i < this->length; i++)
  176. {
  177. this->color[i] = (Color)-1;
  178. }
  179. }
  180. memcpy(this->color + start, color, sizeof(Color) * newLength);
  181. }
  182. void Framework::StickyConsoleContent::repaceContent(int start,
  183. int length,
  184. int newLength,
  185. const char* newContent,
  186. Color* color,
  187. Color* backgroundColor)
  188. {
  189. repaceContent(start, length, newLength, newContent, color);
  190. if (!this->backgroundColor)
  191. {
  192. this->backgroundColor = new Color[this->length];
  193. for (int i = 0; i < this->length; i++)
  194. {
  195. this->backgroundColor[i] = (Color)-1;
  196. }
  197. }
  198. memcpy(this->backgroundColor + start,
  199. backgroundColor,
  200. sizeof(Color) * newLength);
  201. }
  202. void Framework::StickyConsoleContent::triggerUpdate()
  203. {
  204. if (zConsoleHandler != 0)
  205. {
  206. zConsoleHandler->print();
  207. }
  208. }
  209. bool Framework::StickyConsoleContent::isInput()
  210. {
  211. return false;
  212. }
  213. void Framework::StickyConsoleContent::setConsoleHandlerZ(
  214. ConsoleHandler* zConsoleHandler)
  215. {
  216. this->zConsoleHandler = zConsoleHandler;
  217. }
  218. void Framework::StickyConsoleContent::setCursorToBeginning() {}
  219. int Framework::StickyConsoleContent::print() const
  220. {
  221. int lineCount = 0;
  222. int maxWidth = zConsoleHandler->getWidth();
  223. std::cout << "\r\33[0K"; // erase current line
  224. int lineLength = 0;
  225. int lastColor = -1;
  226. int lastBgColor = -1;
  227. for (int i = 0; i < length; i++)
  228. {
  229. if (color && (int)color[i] != lastColor)
  230. {
  231. if ((int)color[i] == -1)
  232. {
  233. std::cout << "\033[0m";
  234. if (backgroundColor && (int)backgroundColor[i] != -1)
  235. {
  236. if (backgroundColor[i] < Color::LIGHT_BLACK)
  237. {
  238. std::cout << Text("\033[1;")
  239. + Text(40 + (int)backgroundColor[i])
  240. + "m";
  241. }
  242. else
  243. {
  244. std::cout
  245. << Text("\033[1;")
  246. + Text(100 + (int)backgroundColor[i] - 8)
  247. + "m";
  248. }
  249. }
  250. }
  251. else if (color[i] < Color::LIGHT_BLACK)
  252. {
  253. std::cout << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
  254. }
  255. else
  256. {
  257. std::cout << Text("\033[1;") + Text(90 + (int)color[i] - 8)
  258. + "m";
  259. }
  260. }
  261. if (backgroundColor && (int)backgroundColor[i] != lastBgColor)
  262. {
  263. if ((int)backgroundColor[i] == -1)
  264. {
  265. std::cout << "\033[0m";
  266. if (color && (int)color[i] != -1)
  267. {
  268. if (color[i] < Color::LIGHT_BLACK)
  269. {
  270. std::cout
  271. << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
  272. }
  273. else
  274. {
  275. std::cout << Text("\033[1;")
  276. + Text(90 + (int)color[i] - 8) + "m";
  277. }
  278. }
  279. }
  280. else if (backgroundColor[i] < Color::LIGHT_BLACK)
  281. {
  282. std::cout << Text("\033[1;")
  283. + Text(40 + (int)backgroundColor[i]) + "m";
  284. }
  285. else
  286. {
  287. std::cout << Text("\033[1;")
  288. + Text(100 + (int)backgroundColor[i] - 8)
  289. + "m";
  290. }
  291. }
  292. std::cout << content[i];
  293. lastColor = color ? (int)color[i] : -1;
  294. lastBgColor = backgroundColor ? (int)backgroundColor[i] : -1;
  295. if ((content[i] == '\n' || lineLength == maxWidth) && i < length - 1)
  296. {
  297. if (lineLength == maxWidth && content[i] != '\n')
  298. {
  299. std::cout << "\n";
  300. }
  301. lineLength = 0;
  302. lineCount++;
  303. }
  304. else
  305. {
  306. lineLength++;
  307. }
  308. }
  309. if (lastColor != -1 || lastBgColor != -1) std::cout << "\033[0m";
  310. if (lineCount == 0 && lineLength > 0)
  311. {
  312. lineCount++;
  313. }
  314. return lineCount;
  315. }
  316. void Framework::StickyConsoleContent::restoreCursorPos() {}
  317. ConsoleHandler* Framework::StickyConsoleContent::zConsoleHandlerRef() const
  318. {
  319. return zConsoleHandler;
  320. }
  321. Framework::ConsoleProgressBar::ConsoleProgressBar()
  322. : StickyConsoleContent(),
  323. progress(0),
  324. maxProgress(1),
  325. maxWidth(-1)
  326. {}
  327. void Framework::ConsoleProgressBar::setMaxWidth(int maxWidth)
  328. {
  329. this->maxWidth = maxWidth;
  330. }
  331. void Framework::ConsoleProgressBar::setProgress(int progress)
  332. {
  333. if (progress < 0) progress = 0;
  334. this->progress = progress;
  335. }
  336. void Framework::ConsoleProgressBar::setMaxProgress(int maxProgress)
  337. {
  338. if (maxProgress < 0) maxProgress = 0;
  339. this->maxProgress = maxProgress;
  340. }
  341. int Framework::ConsoleProgressBar::getProgress() const
  342. {
  343. return progress;
  344. }
  345. int Framework::ConsoleProgressBar::print() const
  346. {
  347. int maxWidth = zConsoleHandlerRef()->getWidth();
  348. int width = this->maxWidth == -1 ? maxWidth : this->maxWidth;
  349. if (width > maxWidth)
  350. {
  351. width = maxWidth;
  352. }
  353. if (width < 7)
  354. {
  355. return 0;
  356. }
  357. std::cout << "\r\33[0K"; // erase current line
  358. int progress = this->progress > maxProgress ? maxProgress : this->progress;
  359. int progressChars = width - 7;
  360. std::cout << "[\033[1;47m";
  361. int progressWidth = (int)(((double)progress / maxProgress) * progressChars);
  362. if (progressWidth > progressChars)
  363. {
  364. progressWidth = progressChars;
  365. }
  366. for (int i = 0; i < progressWidth; i++)
  367. {
  368. std::cout << " ";
  369. }
  370. std::cout << "\033[0m";
  371. for (int i = 0; i < progressChars - progressWidth; i++)
  372. {
  373. std::cout << " ";
  374. }
  375. std::cout << "] ";
  376. Text str((int)(((double)progress / maxProgress) * 100));
  377. for (int i = 0; i < 3 - str.getLength(); i++)
  378. {
  379. std::cout << " ";
  380. }
  381. std::cout << str.getText() << "%";
  382. return 1;
  383. }
  384. Framework::InputLine::InputLine()
  385. : StickyConsoleContent(),
  386. Thread(),
  387. cursorPos(0),
  388. input(""),
  389. suggestions(0)
  390. {}
  391. Framework::InputLine::~InputLine()
  392. {
  393. suggestions->release();
  394. }
  395. void Framework::InputLine::addPossibleCommand(ConsoleCommand* command)
  396. {
  397. commands.add(command);
  398. }
  399. bool Framework::InputLine::isInput()
  400. {
  401. return true;
  402. }
  403. void Framework::InputLine::setCursorToBeginning()
  404. {
  405. cs.lock();
  406. std::cout << "\r";
  407. }
  408. int Framework::InputLine::print() const
  409. {
  410. std::cout << "\33[0K" // clear current line
  411. << input.getText();
  412. if (suggestions)
  413. {
  414. std::cout << "\n";
  415. return 1 + dynamic_cast<StickyConsoleContent*>(suggestions)->print();
  416. }
  417. return 1;
  418. }
  419. void Framework::InputLine::restoreCursorPos()
  420. {
  421. if (cursorPos > 0)
  422. {
  423. std::cout << Text("\33[") + Text(cursorPos) + "C";
  424. }
  425. cs.unlock();
  426. }
  427. void Framework::InputLine::thread()
  428. {
  429. #ifdef WIN32
  430. INPUT_RECORD inputRecord;
  431. DWORD eventsRead;
  432. HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
  433. while (true)
  434. {
  435. if (ReadConsoleInput(handle, &inputRecord, 1, &eventsRead))
  436. {
  437. cs.lock();
  438. if (eventsRead > 0 && inputRecord.EventType == KEY_EVENT
  439. && inputRecord.Event.KeyEvent.bKeyDown)
  440. {
  441. if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
  442. {
  443. cursorPos--;
  444. if (cursorPos < 0)
  445. {
  446. cursorPos = 0;
  447. }
  448. else
  449. {
  450. std::cout << "\33[1D" << std::flush;
  451. }
  452. }
  453. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_HOME)
  454. { // Pos1 key moves cursor to beginning of line
  455. std::cout << "\r" << std::flush;
  456. cursorPos = 0;
  457. }
  458. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
  459. {
  460. cursorPos++;
  461. if (cursorPos > input.getLength())
  462. {
  463. cursorPos = input.getLength();
  464. }
  465. else
  466. {
  467. std::cout << "\33[1C" << std::flush;
  468. }
  469. }
  470. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_END)
  471. { // Pos1 key moves cursor to beginning of line
  472. cursorPos = input.getLength();
  473. std::cout << Text("\r\33[") + Text(cursorPos) + "C"
  474. << std::flush;
  475. }
  476. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_TAB)
  477. {
  478. applyAutocompletion();
  479. }
  480. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode
  481. == VK_RETURN)
  482. {
  483. std::cout << "\r\33[0K" << std::flush; // eraze line
  484. Text cmd = input;
  485. cs.unlock();
  486. if (executeCommand(cmd))
  487. {
  488. cs.lock();
  489. input = "";
  490. cursorPos = 0;
  491. cs.unlock();
  492. triggerUpdate();
  493. }
  494. continue; // skip the unlock
  495. }
  496. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_BACK)
  497. {
  498. if (cursorPos > 0)
  499. {
  500. input.remove(cursorPos - 1, cursorPos);
  501. cursorPos--;
  502. std::cout << "\r\33[0K" // eraze line
  503. << input.getText() // output current input
  504. // move cursor to current position
  505. << Text("\r\33[") + Text(cursorPos) + "C"
  506. << std::flush;
  507. }
  508. }
  509. else if (inputRecord.Event.KeyEvent.uChar.AsciiChar)
  510. {
  511. input.insert(
  512. cursorPos, inputRecord.Event.KeyEvent.uChar.AsciiChar);
  513. cursorPos++;
  514. std::cout << "\r\33[0K" // eraze line
  515. << input.getText() // output current input
  516. // move cursor to current position
  517. << Text("\r\33[") + Text(cursorPos) + "C"
  518. << std::flush;
  519. }
  520. }
  521. cs.unlock();
  522. }
  523. }
  524. #else
  525. char c;
  526. while (read(STDIN_FILENO, &c, 1) == 1)
  527. {
  528. if (c == 27)
  529. { // Check for the escape key (27 is the ASCII code for escape)
  530. char seq[2];
  531. if (read(STDIN_FILENO, &seq, 2) == 2)
  532. {
  533. cs.lock();
  534. if (seq[0] == '[')
  535. {
  536. if (seq[1] == 'D')
  537. { // left arrow key
  538. cursorPos--;
  539. if (cursorPos < 0)
  540. {
  541. cursorPos = 0;
  542. }
  543. else
  544. {
  545. std::cout << "\33[1D" << std::flush;
  546. }
  547. }
  548. else if (seq[1] == 'H')
  549. { // Pos1 key moves cursor to beginning of line
  550. std::cout << "\r" << std::flush;
  551. cursorPos = 0;
  552. }
  553. else if (seq[1] == 'C')
  554. { // right arrow key
  555. cursorPos++;
  556. if (cursorPos > input.getLength())
  557. {
  558. cursorPos = input.getLength();
  559. }
  560. else
  561. {
  562. std::cout << "\33[1C" << std::flush;
  563. }
  564. }
  565. else if (seq[1] == 'F')
  566. { // End key moves cursor to end of line
  567. cursorPos = input.getLength();
  568. std::cout << Text("\r\33[") + Text(cursorPos) + "C"
  569. << std::flush;
  570. }
  571. }
  572. cs.unlock();
  573. }
  574. }
  575. else if (c == 8)
  576. { // backspace
  577. if (cursorPos > 0)
  578. {
  579. cs.lock();
  580. input.remove(cursorPos - 1, cursorPos);
  581. cursorPos--;
  582. std::cout << "\r\33[0K" // eraze line
  583. << input.getText() // output current input
  584. // move cursor to current position
  585. << Text("\r\33[") + Text(cursorPos) + "C"
  586. << std::flush;
  587. cs.unlock();
  588. }
  589. }
  590. else if (c == 9)
  591. { // tab
  592. cs.lock();
  593. applyAutocompletion();
  594. cs.unlock();
  595. }
  596. else if (c == 13)
  597. { // enter
  598. cs.lock();
  599. std::cout << "\r\33[0K" << std::flush; // eraze line
  600. Text cmd = input;
  601. cs.unlock();
  602. if (executeCommand(cmd))
  603. {
  604. cs.lock();
  605. input = "";
  606. cursorPos = 0;
  607. cs.unlock();
  608. }
  609. }
  610. else
  611. {
  612. cs.lock();
  613. input.insert(cursorPos, c);
  614. cursorPos++;
  615. std::cout << "\r\33[0K" // eraze line
  616. << input.getText() // output current input
  617. // move cursor to current position
  618. << Text("\r\33[") + Text(cursorPos) + "C" << std::flush;
  619. cs.unlock();
  620. }
  621. }
  622. #endif
  623. }
  624. void Framework::InputLine::applyAutocompletion()
  625. {
  626. RCArray<Text> params;
  627. bool lastArgFinished = 0;
  628. Text* cmd = input.getTeilText(0, cursorPos);
  629. parseCommand(*cmd, params, lastArgFinished);
  630. cmd->release();
  631. bool paramsStarted = params.getEntryCount() > 1 || lastArgFinished;
  632. Text* name;
  633. if (params.getEntryCount() > 0)
  634. {
  635. name = params.get(0);
  636. params.remove(0);
  637. }
  638. else
  639. {
  640. name = new Text("");
  641. }
  642. RCArray<Text> suggestions;
  643. for (ConsoleCommand* command : commands)
  644. {
  645. if ((!paramsStarted && command->getName().hasAt(0, *name))
  646. || (paramsStarted && command->getName().isEqual(*name)))
  647. {
  648. if (paramsStarted)
  649. {
  650. command->addAutocompletePossibilities(
  651. params, !lastArgFinished, suggestions);
  652. break;
  653. }
  654. else
  655. {
  656. suggestions.add(new Text(command->getName()));
  657. }
  658. }
  659. }
  660. if (this->suggestions)
  661. {
  662. this->suggestions->clear();
  663. }
  664. else
  665. {
  666. this->suggestions = new ConsoleListView();
  667. this->suggestions->setConsoleHandlerZ(zConsoleHandlerRef());
  668. }
  669. for (Text* suggestion : suggestions)
  670. {
  671. this->suggestions->addItem(*suggestion);
  672. }
  673. if (this->suggestions->getItems().getEntryCount() > 0)
  674. {
  675. bool commonChar = true;
  676. for (int i
  677. = lastArgFinished
  678. ? 0
  679. : (params.getEntryCount() > 0
  680. ? params.z(params.getEntryCount() - 1)->getLength()
  681. : name->getLength());
  682. true;
  683. i++)
  684. {
  685. if (i >= this->suggestions->getItems().z(0)->getLength())
  686. {
  687. commonChar = false;
  688. break;
  689. }
  690. for (int j = 1; j < this->suggestions->getItems().getEntryCount();
  691. j++)
  692. {
  693. if (i >= this->suggestions->getItems().z(j)->getLength())
  694. {
  695. commonChar = false;
  696. break;
  697. }
  698. if (this->suggestions->getItems().z(j)->getText()[i]
  699. != this->suggestions->getItems().z(0)->getText()[i])
  700. {
  701. commonChar = false;
  702. break;
  703. }
  704. }
  705. if (!commonChar)
  706. {
  707. break;
  708. }
  709. input.insert(
  710. cursorPos, this->suggestions->getItems().z(0)->getText()[i]);
  711. cursorPos++;
  712. }
  713. }
  714. name->release();
  715. if (this->suggestions->getItems().getEntryCount() <= 1)
  716. {
  717. this->suggestions->release();
  718. this->suggestions = 0;
  719. }
  720. triggerUpdate();
  721. }
  722. bool Framework::InputLine::executeCommand(Text command)
  723. {
  724. RCArray<Text> params;
  725. bool lastArgFinished = 0;
  726. Text* cmd = input.getTeilText(0, cursorPos);
  727. if (!parseCommand(*cmd, params, lastArgFinished))
  728. {
  729. cmd->release();
  730. return false;
  731. }
  732. cmd->release();
  733. Text* name;
  734. if (params.getEntryCount() > 0)
  735. {
  736. name = params.get(0);
  737. params.remove(0);
  738. }
  739. else
  740. {
  741. name = new Text("");
  742. }
  743. RCArray<Text> suggestions;
  744. for (ConsoleCommand* command : commands)
  745. {
  746. if (command->getName().isEqual(*name))
  747. {
  748. name->release();
  749. return command->execute(params);
  750. }
  751. }
  752. name->release();
  753. return false;
  754. }
  755. bool Framework::InputLine::parseCommand(
  756. Text command, RCArray<Text>& split, bool& lastFinished)
  757. {
  758. const char* cmd = command.getText();
  759. Text str;
  760. bool insideString = false;
  761. bool insideString2 = false;
  762. bool lastEscape = false;
  763. lastFinished = false;
  764. while (*cmd)
  765. {
  766. if (*cmd == ' ' && !insideString && !insideString2 && !lastEscape)
  767. {
  768. if (str.getLength() > 0)
  769. {
  770. split.add(new Text(str));
  771. str = "";
  772. lastFinished = true;
  773. }
  774. }
  775. else if (*cmd == '"' && !insideString2 && !lastEscape)
  776. {
  777. insideString = !insideString;
  778. lastFinished = !insideString;
  779. }
  780. else if (*cmd == '\'' && !insideString && !lastEscape)
  781. {
  782. insideString2 = !insideString2;
  783. lastFinished = !insideString2;
  784. }
  785. else if (*cmd == '\\' && !lastEscape)
  786. {
  787. lastEscape = true;
  788. lastFinished = false;
  789. }
  790. else
  791. {
  792. lastEscape = false;
  793. str.append(*cmd);
  794. lastFinished = false;
  795. }
  796. cmd++;
  797. }
  798. if (str.getLength() > 0)
  799. {
  800. split.add(new Text(str));
  801. }
  802. return !insideString && !insideString2 && !lastEscape;
  803. }
  804. Framework::ConsoleListView::ConsoleListView()
  805. : StickyConsoleContent(),
  806. maxColumns(-1),
  807. maxVisibleLines(5),
  808. lineOffset(0)
  809. {}
  810. int Framework::ConsoleListView::getUsedColumns() const
  811. {
  812. if (!zConsoleHandlerRef()) return 0;
  813. int maxWidth = zConsoleHandlerRef()->getWidth();
  814. int columnSize = 0;
  815. for (Text* item : items)
  816. {
  817. if (item->getLength() > columnSize)
  818. {
  819. columnSize = item->getLength();
  820. }
  821. }
  822. columnSize += 4;
  823. if (maxWidth < columnSize)
  824. {
  825. return 0;
  826. }
  827. int columns = maxWidth / columnSize;
  828. if (maxColumns > 0 && columns > maxColumns)
  829. {
  830. columns = maxColumns;
  831. }
  832. if (columns > items.getEntryCount())
  833. {
  834. columns = items.getEntryCount();
  835. }
  836. return columns;
  837. }
  838. int Framework::ConsoleListView::getNeededLines() const
  839. {
  840. int columns = getUsedColumns();
  841. if (!columns)
  842. {
  843. return 0;
  844. }
  845. if (maxColumns > 0 && columns > maxColumns)
  846. {
  847. columns = maxColumns;
  848. }
  849. int lines = items.getEntryCount() / columns;
  850. if (items.getEntryCount() % columns != 0)
  851. {
  852. lines++;
  853. }
  854. return lines;
  855. }
  856. void Framework::ConsoleListView::setMaxVisibleLines(int maxVisibleLines)
  857. {
  858. this->maxVisibleLines = maxVisibleLines;
  859. }
  860. void Framework::ConsoleListView::setLineOffset(int lineOffset)
  861. {
  862. this->lineOffset = lineOffset;
  863. int maxLines = getNeededLines();
  864. if (this->lineOffset > maxLines - maxVisibleLines)
  865. {
  866. this->lineOffset = maxLines - maxVisibleLines;
  867. }
  868. if (this->lineOffset < 0)
  869. {
  870. this->lineOffset = 0;
  871. }
  872. }
  873. void Framework::ConsoleListView::setMaxColumns(int maxColumns)
  874. {
  875. this->maxColumns = maxColumns;
  876. }
  877. void Framework::ConsoleListView::addItem(Text item)
  878. {
  879. item.replace("\n", " ");
  880. int index = 0;
  881. for (Text* curreent : items)
  882. {
  883. int i = 0;
  884. while (i < curreent->getLength() && i < item.getLength())
  885. {
  886. if (curreent->getText()[i] != item.getText()[i])
  887. {
  888. break;
  889. }
  890. i++;
  891. }
  892. if (i == curreent->getLength() && i == item.getLength())
  893. {
  894. return; // item already exists
  895. }
  896. if (i < curreent->getLength() && i == item.getLength())
  897. {
  898. items.add(new Text(item), index);
  899. return;
  900. }
  901. if (i < curreent->getLength() && i < item.getLength()
  902. && curreent->getText()[i] > item.getText()[i])
  903. {
  904. items.add(new Text(item), index);
  905. return;
  906. }
  907. index++;
  908. }
  909. items.add(new Text(item));
  910. }
  911. void Framework::ConsoleListView::clear()
  912. {
  913. items.clear();
  914. }
  915. const RCArray<Text>& Framework::ConsoleListView::getItems() const
  916. {
  917. return items;
  918. }
  919. int Framework::ConsoleListView::print() const
  920. {
  921. int lines = lineOffset;
  922. int maxLines = getNeededLines();
  923. if (lines > maxLines - maxVisibleLines)
  924. {
  925. lines = maxLines - maxVisibleLines;
  926. }
  927. if (lines < 0)
  928. {
  929. lines = 0;
  930. }
  931. int columns = getUsedColumns();
  932. if (!columns)
  933. {
  934. return 0;
  935. }
  936. int columnSize = zConsoleHandlerRef()->getWidth() / columns;
  937. int printetLines = 0;
  938. int currentColumn = 0;
  939. std::cout << "\33[0K"; // clear current line
  940. for (int i = columns * lines; i < items.getEntryCount(); i++)
  941. {
  942. if (i >= columns * (lines + maxVisibleLines))
  943. {
  944. break;
  945. }
  946. if (currentColumn >= columns)
  947. {
  948. std::cout << "\n\r\33[0K";
  949. printetLines++;
  950. currentColumn++;
  951. }
  952. if (!printetLines)
  953. {
  954. printetLines++;
  955. }
  956. std::cout << items.z(i)->getText();
  957. for (int j = items.z(i)->getLength(); j < columnSize; j++)
  958. {
  959. std::cout << " ";
  960. }
  961. currentColumn++;
  962. }
  963. return printetLines;
  964. }
  965. Framework::ConsoleHandler::ConsoleHandler()
  966. : ReferenceCounter(),
  967. lines(0),
  968. lineCounts(0),
  969. contentCount(0)
  970. {
  971. #ifdef WIN32
  972. hConsole = GetStdHandle(STD_INPUT_HANDLE);
  973. SetConsoleMode(hConsole, ENABLE_PROCESSED_INPUT); // set raw mode
  974. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  975. #else
  976. struct termios t;
  977. tcgetattr(STDIN_FILENO, &t);
  978. t.c_lflag &= ~(ICANON | ECHO); // set raw mode
  979. tcsetattr(STDIN_FILENO, TCSANOW, &t);
  980. #endif
  981. }
  982. Framework::ConsoleHandler::~ConsoleHandler()
  983. {
  984. for (int i = 0; i < contentCount; i++)
  985. {
  986. lines[i]->release();
  987. }
  988. delete[] lines;
  989. delete[] lineCounts;
  990. }
  991. void Framework::ConsoleHandler::addContent(
  992. StickyConsoleContent* content, ConsoleContentPosition pos)
  993. {
  994. cs.lock();
  995. if (content->isInput())
  996. {
  997. for (int i = 0; i < contentCount; i++)
  998. {
  999. if (lines[i]->isInput())
  1000. {
  1001. content->release();
  1002. cs.unlock();
  1003. throw "There can only be one input line";
  1004. }
  1005. }
  1006. }
  1007. if (content->zConsoleHandler)
  1008. {
  1009. cs.unlock();
  1010. throw "A console content can only be added to one console handler "
  1011. "at "
  1012. "the same time.";
  1013. }
  1014. content->zConsoleHandler = this;
  1015. contentCount++;
  1016. int* lineCounts = new int[contentCount];
  1017. StickyConsoleContent** lines = new StickyConsoleContent*[contentCount];
  1018. if (pos == ConsoleContentPosition::Top)
  1019. {
  1020. lines[0] = content;
  1021. lineCounts[0] = 0;
  1022. for (int i = 1; i < contentCount; i++)
  1023. {
  1024. lines[i] = this->lines[i - 1];
  1025. lineCounts[i] = this->lineCounts[i - 1];
  1026. }
  1027. }
  1028. else
  1029. {
  1030. lines[contentCount - 1] = content;
  1031. lineCounts[contentCount - 1] = content->print();
  1032. for (int i = 0; i < contentCount - 1; i++)
  1033. {
  1034. lines[i] = this->lines[i];
  1035. lineCounts[i] = this->lineCounts[i];
  1036. }
  1037. }
  1038. delete[] this->lines;
  1039. delete[] this->lineCounts;
  1040. this->lines = lines;
  1041. this->lineCounts = lineCounts;
  1042. InputLine* input = dynamic_cast<InputLine*>(content);
  1043. if (input)
  1044. {
  1045. input->start();
  1046. }
  1047. cs.unlock();
  1048. }
  1049. void Framework::ConsoleHandler::removeContent(StickyConsoleContent* zContent)
  1050. {
  1051. cs.lock();
  1052. int index = -1;
  1053. for (int i = 0; i < contentCount; i++)
  1054. {
  1055. if (lines[i] == zContent)
  1056. {
  1057. index = i;
  1058. break;
  1059. }
  1060. }
  1061. if (index == -1)
  1062. {
  1063. cs.unlock();
  1064. return;
  1065. }
  1066. zContent->zConsoleHandler = 0;
  1067. contentCount--;
  1068. int* lineCounts = new int[contentCount];
  1069. StickyConsoleContent** lines = new StickyConsoleContent*[contentCount];
  1070. for (int i = 0; i < index; i++)
  1071. {
  1072. lines[i] = this->lines[i];
  1073. lineCounts[i] = this->lineCounts[i];
  1074. }
  1075. for (int i = index; i < contentCount; i++)
  1076. {
  1077. lines[i] = this->lines[i + 1];
  1078. lineCounts[i] = this->lineCounts[i + 1];
  1079. }
  1080. delete[] this->lines;
  1081. delete[] this->lineCounts;
  1082. this->lines = lines;
  1083. this->lineCounts = lineCounts;
  1084. if (zContent->isInput())
  1085. {
  1086. InputLine* input = dynamic_cast<InputLine*>(zContent);
  1087. if (input)
  1088. {
  1089. input->start();
  1090. }
  1091. }
  1092. zContent->release();
  1093. cs.unlock();
  1094. }
  1095. int Framework::ConsoleHandler::getWidth() const
  1096. {
  1097. #ifdef WIN32
  1098. CONSOLE_SCREEN_BUFFER_INFO csbi;
  1099. GetConsoleScreenBufferInfo(hConsole, &csbi);
  1100. return csbi.dwSize.X;
  1101. #else
  1102. struct winsize ws;
  1103. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  1104. {
  1105. return 0;
  1106. }
  1107. return ws.ws_col;
  1108. #endif
  1109. }
  1110. int Framework::ConsoleHandler::getHeight() const
  1111. {
  1112. #ifdef WIN32
  1113. CONSOLE_SCREEN_BUFFER_INFO csbi;
  1114. GetConsoleScreenBufferInfo(hConsole, &csbi);
  1115. return csbi.dwSize.Y;
  1116. #else
  1117. struct winsize ws;
  1118. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  1119. {
  1120. return 0;
  1121. }
  1122. return ws.ws_row;
  1123. #endif
  1124. }
  1125. void Framework::ConsoleHandler::clear()
  1126. {
  1127. cs.lock();
  1128. for (int i = 0; i < contentCount; i++)
  1129. {
  1130. lines[i]->release();
  1131. }
  1132. delete[] lines;
  1133. delete[] lineCounts;
  1134. lines = 0;
  1135. lineCounts = 0;
  1136. contentCount = 0;
  1137. cs.unlock();
  1138. }
  1139. void Framework::ConsoleHandler::print()
  1140. {
  1141. cs.lock();
  1142. int totalLines = 0;
  1143. bool adittionalLine = 0;
  1144. for (int i = 0; i < contentCount; i++)
  1145. {
  1146. if (lines[i]->isInput())
  1147. {
  1148. lines[i]->setCursorToBeginning();
  1149. break;
  1150. }
  1151. totalLines += lineCounts[i];
  1152. }
  1153. if (totalLines > 0)
  1154. {
  1155. std::cout << "\33[" << totalLines << "A";
  1156. }
  1157. totalLines = 0;
  1158. for (int i = 0; i < contentCount; i++)
  1159. {
  1160. lineCounts[i] = lines[i]->print();
  1161. if (lineCounts[i] > 0)
  1162. {
  1163. adittionalLine = 0;
  1164. if (i < contentCount - 1)
  1165. {
  1166. std::cout << "\n";
  1167. adittionalLine = 1;
  1168. }
  1169. }
  1170. totalLines += lineCounts[i];
  1171. }
  1172. if (adittionalLine)
  1173. {
  1174. totalLines++;
  1175. }
  1176. std::cout << "\33[" << (totalLines - 1) << "A\r";
  1177. for (int i = 0; i < contentCount; i++)
  1178. {
  1179. if (lines[i]->isInput())
  1180. {
  1181. lines[i]->restoreCursorPos();
  1182. break;
  1183. }
  1184. if (i < contentCount - 1 && lineCounts[i])
  1185. {
  1186. std::cout << "\33[" << lineCounts[i] << "B";
  1187. }
  1188. }
  1189. std::cout << std::flush;
  1190. cs.unlock();
  1191. }
  1192. void Framework::ConsoleHandler::print(Text str)
  1193. {
  1194. std::cout << "\n";
  1195. cs.lock();
  1196. int totalLines = 0;
  1197. bool adittionalLine = 0;
  1198. for (int i = 0; i < contentCount; i++)
  1199. {
  1200. if (lines[i]->isInput())
  1201. {
  1202. lines[i]->setCursorToBeginning();
  1203. break;
  1204. }
  1205. totalLines += lineCounts[i];
  1206. }
  1207. std::cout << "\33[" << totalLines + 1 << "A";
  1208. std::cout << "\33[0K" // clear current line
  1209. << str.getText();
  1210. if (str.getText()[str.getLength() - 1] != '\n')
  1211. {
  1212. std::cout << "\n";
  1213. }
  1214. totalLines = 0;
  1215. for (int i = 0; i < contentCount; i++)
  1216. {
  1217. lineCounts[i] = lines[i]->print();
  1218. if (lineCounts[i] > 0)
  1219. {
  1220. adittionalLine = 0;
  1221. if (i < contentCount - 1)
  1222. {
  1223. std::cout << "\n";
  1224. adittionalLine = 1;
  1225. }
  1226. }
  1227. totalLines += lineCounts[i];
  1228. }
  1229. if (adittionalLine)
  1230. {
  1231. totalLines++;
  1232. }
  1233. std::cout << "\33[" << (totalLines - 1) << "A\r";
  1234. for (int i = 0; i < contentCount; i++)
  1235. {
  1236. if (lines[i]->isInput())
  1237. {
  1238. lines[i]->restoreCursorPos();
  1239. break;
  1240. }
  1241. if (i < contentCount - 1 && lineCounts[i])
  1242. {
  1243. std::cout << "\33[" << lineCounts[i] << "B";
  1244. }
  1245. }
  1246. std::cout << std::flush;
  1247. cs.unlock();
  1248. }