Console.cpp 34 KB

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