Console.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. #include "Console.h"
  2. #include <iostream>
  3. #ifndef WIN32
  4. # include <termios.h>
  5. # include <unistd.h>
  6. #endif
  7. using namespace Framework;
  8. ConsoleCommand::ConsoleCommand(Text name)
  9. : ReferenceCounter(),
  10. name(name)
  11. {}
  12. ConsoleCommand::~ConsoleCommand() {}
  13. const Text& Framework::ConsoleCommand::getName() const
  14. {
  15. return name;
  16. }
  17. Framework::StickyConsoleContent::StickyConsoleContent()
  18. : ReferenceCounter(),
  19. length(0),
  20. content(0),
  21. color(0),
  22. backgroundColor(0),
  23. zConsoleHandler(0)
  24. {}
  25. Framework::StickyConsoleContent::~StickyConsoleContent()
  26. {
  27. delete[] content;
  28. delete[] color;
  29. delete[] backgroundColor;
  30. }
  31. int Framework::StickyConsoleContent::getLength() const
  32. {
  33. return length;
  34. }
  35. void Framework::StickyConsoleContent::setContent(
  36. int length, const char* content)
  37. {
  38. delete[] this->content;
  39. this->content = new char[length];
  40. memcpy(this->content, content, length);
  41. this->length = length;
  42. delete[] color;
  43. delete[] backgroundColor;
  44. color = 0;
  45. backgroundColor = 0;
  46. }
  47. void Framework::StickyConsoleContent::setContent(
  48. int length, const char* content, Color color)
  49. {
  50. setContent(length, content);
  51. this->color = new Color[length];
  52. for (int i = 0; i < length; i++)
  53. {
  54. this->color[i] = color;
  55. }
  56. }
  57. void Framework::StickyConsoleContent::setContent(
  58. int length, const char* content, Color color, Color backgroundColor)
  59. {
  60. setContent(length, content, color);
  61. this->backgroundColor = new Color[length];
  62. for (int i = 0; i < length; i++)
  63. {
  64. this->backgroundColor[i] = backgroundColor;
  65. }
  66. }
  67. void Framework::StickyConsoleContent::setContent(
  68. int length, const char* content, Color* color)
  69. {
  70. setContent(length, content);
  71. this->color = new Color[length];
  72. for (int i = 0; i < length; i++)
  73. {
  74. this->color[i] = color[i];
  75. }
  76. }
  77. void Framework::StickyConsoleContent::setContent(
  78. int length, const char* content, Color* color, Color* backgroundColor)
  79. {
  80. setContent(length, content, color);
  81. this->backgroundColor = new Color[length];
  82. for (int i = 0; i < length; i++)
  83. {
  84. this->backgroundColor[i] = backgroundColor[i];
  85. }
  86. }
  87. void Framework::StickyConsoleContent::repaceContent(
  88. int start, int length, int newLength, const char* newContent)
  89. {
  90. int resultLength = this->length + newLength - length;
  91. char* resultContent = new char[resultLength];
  92. memcpy(resultContent, content, start);
  93. memcpy(resultContent + start, newContent, newLength);
  94. memcpy(resultContent + start + newLength,
  95. content + start + length,
  96. this->length - start - length);
  97. delete[] content;
  98. content = resultContent;
  99. if (color)
  100. {
  101. Color* resultColor = new Color[resultLength];
  102. memcpy(resultColor, color, start);
  103. for (int i = 0; i < newLength; i++)
  104. {
  105. resultColor[start + i] = color[start];
  106. }
  107. memcpy(resultColor + start + newLength,
  108. color + start + length,
  109. sizeof(Color) * (this->length - start - length));
  110. delete[] color;
  111. color = resultColor;
  112. }
  113. if (backgroundColor)
  114. {
  115. Color* resultBgColor = new Color[resultLength];
  116. memcpy(resultBgColor, backgroundColor, start);
  117. for (int i = 0; i < newLength; i++)
  118. {
  119. resultBgColor[start + i] = backgroundColor[start];
  120. }
  121. memcpy(resultBgColor + start + newLength,
  122. backgroundColor + start + length,
  123. sizeof(Color) * (this->length - start - length));
  124. delete[] backgroundColor;
  125. backgroundColor = resultBgColor;
  126. }
  127. this->length = resultLength;
  128. }
  129. void Framework::StickyConsoleContent::repaceContent(
  130. int start, int length, int newLength, const char* newContent, Color color)
  131. {
  132. repaceContent(start, length, newLength, newContent);
  133. if (!this->color)
  134. {
  135. this->color = new Color[this->length];
  136. for (int i = 0; i < this->length; i++)
  137. {
  138. this->color[i] = (Color)-1;
  139. }
  140. }
  141. for (int i = start; i < start + newLength; i++)
  142. {
  143. this->color[i] = color;
  144. }
  145. }
  146. void Framework::StickyConsoleContent::repaceContent(int start,
  147. int length,
  148. int newLength,
  149. const char* newContent,
  150. Color color,
  151. Color backgroundColor)
  152. {
  153. repaceContent(start, length, newLength, newContent, color);
  154. if (!this->backgroundColor)
  155. {
  156. this->backgroundColor = new Color[this->length];
  157. for (int i = 0; i < this->length; i++)
  158. {
  159. this->backgroundColor[i] = (Color)-1;
  160. }
  161. }
  162. for (int i = start; i < start + newLength; i++)
  163. {
  164. this->backgroundColor[i] = backgroundColor;
  165. }
  166. }
  167. void Framework::StickyConsoleContent::repaceContent(
  168. int start, int length, int newLength, const char* newContent, Color* color)
  169. {
  170. repaceContent(start, length, newLength, newContent);
  171. if (!this->color)
  172. {
  173. this->color = new Color[this->length];
  174. for (int i = 0; i < this->length; i++)
  175. {
  176. this->color[i] = (Color)-1;
  177. }
  178. }
  179. memcpy(this->color + start, color, sizeof(Color) * newLength);
  180. }
  181. void Framework::StickyConsoleContent::repaceContent(int start,
  182. int length,
  183. int newLength,
  184. const char* newContent,
  185. Color* color,
  186. Color* backgroundColor)
  187. {
  188. repaceContent(start, length, newLength, newContent, color);
  189. if (!this->backgroundColor)
  190. {
  191. this->backgroundColor = new Color[this->length];
  192. for (int i = 0; i < this->length; i++)
  193. {
  194. this->backgroundColor[i] = (Color)-1;
  195. }
  196. }
  197. memcpy(this->backgroundColor + start,
  198. backgroundColor,
  199. sizeof(Color) * newLength);
  200. }
  201. void Framework::StickyConsoleContent::triggerUpdate()
  202. {
  203. if (zConsoleHandler != 0)
  204. {
  205. zConsoleHandler->print();
  206. }
  207. }
  208. bool Framework::StickyConsoleContent::isInput()
  209. {
  210. return false;
  211. }
  212. void Framework::StickyConsoleContent::setConsoleHandlerZ(
  213. ConsoleHandler* zConsoleHandler)
  214. {
  215. this->zConsoleHandler = zConsoleHandler;
  216. }
  217. void Framework::StickyConsoleContent::setCursorToBeginning() {}
  218. int Framework::StickyConsoleContent::print() const
  219. {
  220. int lineCount = 0;
  221. int maxWidth = zConsoleHandler->getWidth();
  222. std::cout << "\r\33[0K"; // erase current line
  223. int lineLength = 0;
  224. int lastColor = -1;
  225. int lastBgColor = -1;
  226. for (int i = 0; i < length; i++)
  227. {
  228. if (color && (int)color[i] != lastColor)
  229. {
  230. if ((int)color[i] == -1)
  231. {
  232. std::cout << "\033[0m";
  233. if (backgroundColor && (int)backgroundColor[i] != -1)
  234. {
  235. if (backgroundColor[i] < Color::LIGHT_BLACK)
  236. {
  237. std::cout << Text("\033[1;")
  238. + Text(40 + (int)backgroundColor[i])
  239. + "m";
  240. }
  241. else
  242. {
  243. std::cout
  244. << Text("\033[1;")
  245. + Text(100 + (int)backgroundColor[i] - 8)
  246. + "m";
  247. }
  248. }
  249. }
  250. else if (color[i] < Color::LIGHT_BLACK)
  251. {
  252. std::cout << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
  253. }
  254. else
  255. {
  256. std::cout << Text("\033[1;") + Text(90 + (int)color[i] - 8)
  257. + "m";
  258. }
  259. }
  260. if (backgroundColor && (int)backgroundColor[i] != lastBgColor)
  261. {
  262. if ((int)backgroundColor[i] == -1)
  263. {
  264. std::cout << "\033[0m";
  265. if (color && (int)color[i] != -1)
  266. {
  267. if (color[i] < Color::LIGHT_BLACK)
  268. {
  269. std::cout
  270. << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
  271. }
  272. else
  273. {
  274. std::cout << Text("\033[1;")
  275. + Text(90 + (int)color[i] - 8) + "m";
  276. }
  277. }
  278. }
  279. else if (backgroundColor[i] < Color::LIGHT_BLACK)
  280. {
  281. std::cout << Text("\033[1;")
  282. + Text(40 + (int)backgroundColor[i]) + "m";
  283. }
  284. else
  285. {
  286. std::cout << Text("\033[1;")
  287. + Text(100 + (int)backgroundColor[i] - 8)
  288. + "m";
  289. }
  290. }
  291. std::cout << content[i];
  292. lastColor = color ? (int)color[i] : -1;
  293. lastBgColor = backgroundColor ? (int)backgroundColor[i] : -1;
  294. if ((content[i] == '\n' || lineLength == maxWidth) && i < length - 1)
  295. {
  296. if (lineLength == maxWidth && content[i] != '\n')
  297. {
  298. std::cout << "\n";
  299. }
  300. lineLength = 0;
  301. lineCount++;
  302. }
  303. else
  304. {
  305. lineLength++;
  306. }
  307. }
  308. if (lastColor != -1 || lastBgColor != -1) std::cout << "\033[0m";
  309. if (lineCount == 0 && lineLength > 0)
  310. {
  311. lineCount++;
  312. }
  313. return lineCount;
  314. }
  315. void Framework::StickyConsoleContent::restoreCursorPos() {}
  316. ConsoleHandler* Framework::StickyConsoleContent::zConsoleHandlerRef() const
  317. {
  318. return zConsoleHandler;
  319. }
  320. Framework::ConsoleProgressBar::ConsoleProgressBar()
  321. : StickyConsoleContent(),
  322. progress(0),
  323. maxProgress(1),
  324. maxWidth(-1)
  325. {}
  326. void Framework::ConsoleProgressBar::setMaxWidth(int maxWidth)
  327. {
  328. this->maxWidth = maxWidth;
  329. }
  330. void Framework::ConsoleProgressBar::setProgress(int progress)
  331. {
  332. if (progress < 0) progress = 0;
  333. this->progress = progress;
  334. }
  335. void Framework::ConsoleProgressBar::setMaxProgress(int maxProgress)
  336. {
  337. if (maxProgress < 0) maxProgress = 0;
  338. this->maxProgress = maxProgress;
  339. }
  340. int Framework::ConsoleProgressBar::getProgress() const
  341. {
  342. return progress;
  343. }
  344. int Framework::ConsoleProgressBar::print() const
  345. {
  346. int maxWidth = zConsoleHandlerRef()->getWidth();
  347. int width = this->maxWidth == -1 ? maxWidth : this->maxWidth;
  348. if (width > maxWidth)
  349. {
  350. width = maxWidth;
  351. }
  352. if (width < 7)
  353. {
  354. return 0;
  355. }
  356. std::cout << "\r\33[0K"; // erase current line
  357. int progress = this->progress > maxProgress ? maxProgress : this->progress;
  358. int progressChars = width - 7;
  359. std::cout << "[\033[1;47m";
  360. int progressWidth = (int)(((double)progress / maxProgress) * progressChars);
  361. if (progressWidth > progressChars)
  362. {
  363. progressWidth = progressChars;
  364. }
  365. for (int i = 0; i < progressWidth; i++)
  366. {
  367. std::cout << " ";
  368. }
  369. std::cout << "\033[0m";
  370. for (int i = 0; i < progressChars - progressWidth; i++)
  371. {
  372. std::cout << " ";
  373. }
  374. std::cout << "] ";
  375. Text str((int)(((double)progress / maxProgress) * 100));
  376. for (int i = 0; i < 3 - str.getLength(); i++)
  377. {
  378. std::cout << " ";
  379. }
  380. std::cout << str.getText() << "%";
  381. return 1;
  382. }
  383. Framework::InputLine::InputLine()
  384. : StickyConsoleContent(),
  385. Thread(),
  386. input(""),
  387. cursorPos(0),
  388. suggestions(0)
  389. {}
  390. Framework::InputLine::~InputLine()
  391. {
  392. suggestions->release();
  393. }
  394. void Framework::InputLine::addPossibleCommand(ConsoleCommand* command)
  395. {
  396. commands.add(command);
  397. }
  398. bool Framework::InputLine::isInput()
  399. {
  400. return true;
  401. }
  402. void Framework::InputLine::setCursorToBeginning()
  403. {
  404. cs.lock();
  405. std::cout << "\r";
  406. }
  407. int Framework::InputLine::print() const
  408. {
  409. std::cout << "\33[0K" // clear current line
  410. << input.getText();
  411. if (suggestions)
  412. {
  413. std::cout << "\n";
  414. return 1 + dynamic_cast<StickyConsoleContent*>(suggestions)->print();
  415. }
  416. return 1;
  417. }
  418. void Framework::InputLine::restoreCursorPos()
  419. {
  420. if (cursorPos > 0)
  421. {
  422. std::cout << Text("\33[") + Text(cursorPos) + "C";
  423. }
  424. cs.unlock();
  425. }
  426. void Framework::InputLine::thread()
  427. {
  428. #ifdef WIN32
  429. INPUT_RECORD inputRecord;
  430. DWORD eventsRead;
  431. HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
  432. while (true)
  433. {
  434. if (ReadConsoleInput(handle, &inputRecord, 1, &eventsRead))
  435. {
  436. cs.lock();
  437. if (eventsRead > 0 && inputRecord.EventType == KEY_EVENT
  438. && inputRecord.Event.KeyEvent.bKeyDown)
  439. {
  440. if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
  441. {
  442. cursorPos--;
  443. if (cursorPos < 0)
  444. {
  445. cursorPos = 0;
  446. }
  447. else
  448. {
  449. std::cout << "\33[1D" << std::flush;
  450. }
  451. }
  452. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_HOME)
  453. { // Pos1 key moves cursor to beginning of line
  454. std::cout << "\r" << std::flush;
  455. cursorPos = 0;
  456. }
  457. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
  458. {
  459. cursorPos++;
  460. if (cursorPos > input.getLength())
  461. {
  462. cursorPos = input.getLength();
  463. }
  464. else
  465. {
  466. std::cout << "\33[1C" << std::flush;
  467. }
  468. }
  469. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_END)
  470. { // Pos1 key moves cursor to beginning of line
  471. cursorPos = input.getLength();
  472. std::cout << Text("\r\33[") + Text(cursorPos) + "C"
  473. << std::flush;
  474. }
  475. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_TAB)
  476. {
  477. applyAutocompletion();
  478. }
  479. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode
  480. == VK_RETURN)
  481. {
  482. std::cout << "\r\33[0K" << std::flush; // eraze line
  483. Text cmd = input;
  484. cs.unlock();
  485. if (executeCommand(cmd))
  486. {
  487. cs.lock();
  488. input = "";
  489. cursorPos = 0;
  490. cs.unlock();
  491. triggerUpdate();
  492. }
  493. continue; // skip the unlock
  494. }
  495. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_BACK)
  496. {
  497. if (cursorPos > 0)
  498. {
  499. input.remove(cursorPos - 1, cursorPos);
  500. cursorPos--;
  501. std::cout << "\r\33[0K" // eraze line
  502. << input.getText() // output current input
  503. // move cursor to current position
  504. << Text("\r\33[") + Text(cursorPos) + "C"
  505. << std::flush;
  506. }
  507. }
  508. else if (inputRecord.Event.KeyEvent.uChar.AsciiChar)
  509. {
  510. input.insert(
  511. cursorPos, inputRecord.Event.KeyEvent.uChar.AsciiChar);
  512. cursorPos++;
  513. std::cout << "\r\33[0K" // eraze line
  514. << input.getText() // output current input
  515. // move cursor to current position
  516. << Text("\r\33[") + Text(cursorPos) + "C"
  517. << std::flush;
  518. }
  519. }
  520. cs.unlock();
  521. }
  522. }
  523. #else
  524. char c;
  525. while (read(STDIN_FILENO, &c, 1) == 1)
  526. {
  527. if (c == 27)
  528. { // Check for the escape key (27 is the ASCII code for escape)
  529. char seq[2];
  530. if (read(STDIN_FILENO, &seq, 2) == 2)
  531. {
  532. cs.lock();
  533. if (seq[0] == '[')
  534. {
  535. if (seq[1] == 'D')
  536. { // left arrow key
  537. cursorPos--;
  538. if (cursorPos < 0)
  539. {
  540. cursorPos = 0;
  541. }
  542. else
  543. {
  544. std::cout << "\33[1D" << std::flush;
  545. }
  546. }
  547. else if (seq[1] == 'H')
  548. { // Pos1 key moves cursor to beginning of line
  549. std::cout << "\r" << std::flush;
  550. cursorPos = 0;
  551. }
  552. else if (seq[1] == 'C')
  553. { // right arrow key
  554. cursorPos++;
  555. if (cursorPos > input.getLength())
  556. {
  557. cursorPos = input.getLength();
  558. }
  559. else
  560. {
  561. std::cout << "\33[1C" << std::flush;
  562. }
  563. }
  564. else if (seq[1] == 'F')
  565. { // End key moves cursor to end of line
  566. cursorPos = input.getLength();
  567. std::cout << Text("\r\33[") + Text(cursorPos) + "C"
  568. << std::flush;
  569. }
  570. }
  571. cs.unlock();
  572. }
  573. }
  574. else if (c == 8)
  575. { // backspace
  576. if (cursorPos > 0)
  577. {
  578. cs.lock();
  579. input.remove(cursorPos - 1, cursorPos);
  580. cursorPos--;
  581. std::cout << "\r\33[0K" // eraze line
  582. << input.getText() // output current input
  583. // move cursor to current position
  584. << Text("\r\33[") + Text(cursorPos) + "C"
  585. << std::flush;
  586. cs.unlock();
  587. }
  588. }
  589. else if (c == 9)
  590. { // tab
  591. cs.lock();
  592. applyAutocompletion();
  593. cs.unlock();
  594. }
  595. else if (c == 13)
  596. { // enter
  597. cs.lock();
  598. std::cout << "\r\33[0K" << std::flush; // eraze line
  599. Text cmd = input;
  600. cs.unlock();
  601. if (executeCommand(cmd))
  602. {
  603. cs.lock();
  604. input = "";
  605. cursorPos = 0;
  606. cs.unlock();
  607. }
  608. }
  609. else
  610. {
  611. cs.lock();
  612. input.insert(cursorPos, c);
  613. cursorPos++;
  614. std::cout << "\r\33[0K" // eraze line
  615. << input.getText() // output current input
  616. // move cursor to current position
  617. << Text("\r\33[") + Text(cursorPos) + "C" << std::flush;
  618. cs.unlock();
  619. }
  620. }
  621. #endif
  622. }
  623. void Framework::InputLine::applyAutocompletion()
  624. {
  625. RCArray<Text> params;
  626. bool lastArgFinished = 0;
  627. Text* cmd = input.getTeilText(0, cursorPos);
  628. parseCommand(*cmd, params, lastArgFinished);
  629. cmd->release();
  630. bool paramsStarted = params.getEintragAnzahl() > 1 || lastArgFinished;
  631. Text* name;
  632. if (params.getEintragAnzahl() > 0)
  633. {
  634. name = params.get(0);
  635. params.remove(0);
  636. }
  637. else
  638. {
  639. name = new Text("");
  640. }
  641. RCArray<Text> suggestions;
  642. for (ConsoleCommand* command : commands)
  643. {
  644. if ((!paramsStarted && command->getName().hatAt(0, *name))
  645. || (paramsStarted && command->getName().istGleich(*name)))
  646. {
  647. if (paramsStarted)
  648. {
  649. command->addAutocompletePossibilities(
  650. params, !lastArgFinished, suggestions);
  651. break;
  652. }
  653. else
  654. {
  655. suggestions.add(new Text(command->getName()));
  656. }
  657. }
  658. }
  659. if (this->suggestions)
  660. {
  661. this->suggestions->clear();
  662. }
  663. else
  664. {
  665. this->suggestions = new ConsoleListView();
  666. this->suggestions->setConsoleHandlerZ(zConsoleHandlerRef());
  667. }
  668. for (Text* suggestion : suggestions)
  669. {
  670. this->suggestions->addItem(*suggestion);
  671. }
  672. if (this->suggestions->getItems().getEintragAnzahl() > 0)
  673. {
  674. bool commonChar = true;
  675. for (int i
  676. = lastArgFinished
  677. ? 0
  678. : (params.getEintragAnzahl() > 0
  679. ? params.z(params.getEintragAnzahl() - 1)->getLength()
  680. : name->getLength());
  681. true;
  682. i++)
  683. {
  684. if (i >= this->suggestions->getItems().z(0)->getLength())
  685. {
  686. commonChar = false;
  687. break;
  688. }
  689. for (int j = 1;
  690. j < this->suggestions->getItems().getEintragAnzahl();
  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().getEintragAnzahl() <= 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.getEintragAnzahl() > 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().istGleich(*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.getEintragAnzahl())
  833. {
  834. columns = items.getEintragAnzahl();
  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.getEintragAnzahl() / columns;
  850. if (items.getEintragAnzahl() % 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.ersetzen("\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.leeren();
  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.getEintragAnzahl(); 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. }