Console.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. }
  492. continue; // skip the unlock
  493. }
  494. else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_BACK)
  495. {
  496. if (cursorPos > 0)
  497. {
  498. input.remove(cursorPos - 1, cursorPos);
  499. cursorPos--;
  500. std::cout << "\r\33[0K" // eraze line
  501. << input.getText() // output current input
  502. // move cursor to current position
  503. << Text("\r\33[") + Text(cursorPos) + "C"
  504. << std::flush;
  505. }
  506. }
  507. else if (inputRecord.Event.KeyEvent.uChar.AsciiChar)
  508. {
  509. input.insert(
  510. cursorPos, inputRecord.Event.KeyEvent.uChar.AsciiChar);
  511. cursorPos++;
  512. std::cout << "\r\33[0K" // eraze line
  513. << input.getText() // output current input
  514. // move cursor to current position
  515. << Text("\r\33[") + Text(cursorPos) + "C"
  516. << std::flush;
  517. }
  518. }
  519. cs.unlock();
  520. }
  521. }
  522. #else
  523. char c;
  524. while (read(STDIN_FILENO, &c, 1) == 1)
  525. {
  526. if (c == 27)
  527. { // Check for the escape key (27 is the ASCII code for escape)
  528. char seq[2];
  529. if (read(STDIN_FILENO, &seq, 2) == 2)
  530. {
  531. cs.lock();
  532. if (seq[0] == '[')
  533. {
  534. if (seq[1] == 'D')
  535. { // left arrow key
  536. cursorPos--;
  537. if (cursorPos < 0)
  538. {
  539. cursorPos = 0;
  540. }
  541. else
  542. {
  543. std::cout << "\33[1D" << std::flush;
  544. }
  545. }
  546. else if (seq[1] == 'H')
  547. { // Pos1 key moves cursor to beginning of line
  548. std::cout << "\r" << std::flush;
  549. cursorPos = 0;
  550. }
  551. else if (seq[1] == 'C')
  552. { // right arrow key
  553. cursorPos++;
  554. if (cursorPos > input.getLength())
  555. {
  556. cursorPos = input.getLength();
  557. }
  558. else
  559. {
  560. std::cout << "\33[1C" << std::flush;
  561. }
  562. }
  563. else if (seq[1] == 'F')
  564. { // End key moves cursor to end of line
  565. cursorPos = input.getLength();
  566. std::cout << Text("\r\33[") + Text(cursorPos) + "C"
  567. << std::flush;
  568. }
  569. }
  570. cs.unlock();
  571. }
  572. }
  573. else if (c == 8)
  574. { // backspace
  575. if (cursorPos > 0)
  576. {
  577. cs.lock();
  578. input.remove(cursorPos - 1, cursorPos);
  579. cursorPos--;
  580. std::cout << "\r\33[0K" // eraze line
  581. << input.getText() // output current input
  582. // move cursor to current position
  583. << Text("\r\33[") + Text(cursorPos) + "C"
  584. << std::flush;
  585. cs.unlock();
  586. }
  587. }
  588. else if (c == 9)
  589. { // tab
  590. cs.lock();
  591. applyAutocompletion();
  592. cs.unlock();
  593. }
  594. else if (c == 13)
  595. { // enter
  596. cs.lock();
  597. std::cout << "\r\33[0K" << std::flush; // eraze line
  598. Text cmd = input;
  599. cs.unlock();
  600. if (executeCommand(cmd))
  601. {
  602. cs.lock();
  603. input = "";
  604. cursorPos = 0;
  605. cs.unlock();
  606. }
  607. }
  608. else
  609. {
  610. cs.lock();
  611. input.insert(cursorPos, c);
  612. cursorPos++;
  613. std::cout << "\r\33[0K" // eraze line
  614. << input.getText() // output current input
  615. // move cursor to current position
  616. << Text("\r\33[") + Text(cursorPos) + "C" << std::flush;
  617. cs.unlock();
  618. }
  619. }
  620. #endif
  621. }
  622. void Framework::InputLine::applyAutocompletion()
  623. {
  624. RCArray<Text> params;
  625. bool lastArgFinished = 0;
  626. Text* cmd = input.getTeilText(0, cursorPos);
  627. parseCommand(*cmd, params, lastArgFinished);
  628. cmd->release();
  629. bool paramsStarted = params.getEintragAnzahl() > 1 || lastArgFinished;
  630. Text* name;
  631. if (params.getEintragAnzahl() > 0)
  632. {
  633. name = params.get(0);
  634. params.remove(0);
  635. }
  636. else
  637. {
  638. name = new Text("");
  639. }
  640. RCArray<Text> suggestions;
  641. for (ConsoleCommand* command : commands)
  642. {
  643. if ((!paramsStarted && command->getName().hatAt(0, *name))
  644. || (paramsStarted && command->getName().istGleich(*name)))
  645. {
  646. if (paramsStarted)
  647. {
  648. command->addAutocompletePossibilities(
  649. params, !lastArgFinished, suggestions);
  650. break;
  651. }
  652. else
  653. {
  654. suggestions.add(new Text(command->getName()));
  655. }
  656. }
  657. }
  658. if (this->suggestions)
  659. {
  660. this->suggestions->clear();
  661. }
  662. else
  663. {
  664. this->suggestions = new ConsoleListView();
  665. this->suggestions->setConsoleHandlerZ(zConsoleHandlerRef());
  666. }
  667. for (Text* suggestion : suggestions)
  668. {
  669. this->suggestions->addItem(*suggestion);
  670. }
  671. if (this->suggestions->getItems().getEintragAnzahl() > 0)
  672. {
  673. bool commonChar = true;
  674. for (int i
  675. = lastArgFinished
  676. ? 0
  677. : (params.getEintragAnzahl() > 0
  678. ? params.z(params.getEintragAnzahl() - 1)->getLength()
  679. : name->getLength());
  680. true;
  681. i++)
  682. {
  683. if (i >= this->suggestions->getItems().z(0)->getLength())
  684. {
  685. commonChar = false;
  686. break;
  687. }
  688. for (int j = 1;
  689. j < this->suggestions->getItems().getEintragAnzahl();
  690. j++)
  691. {
  692. if (i >= this->suggestions->getItems().z(j)->getLength())
  693. {
  694. commonChar = false;
  695. break;
  696. }
  697. if (this->suggestions->getItems().z(j)->getText()[i]
  698. != this->suggestions->getItems().z(0)->getText()[i])
  699. {
  700. commonChar = false;
  701. break;
  702. }
  703. }
  704. if (!commonChar)
  705. {
  706. break;
  707. }
  708. input.insert(
  709. cursorPos, this->suggestions->getItems().z(0)->getText()[i]);
  710. cursorPos++;
  711. }
  712. }
  713. name->release();
  714. if (this->suggestions->getItems().getEintragAnzahl() <= 1)
  715. {
  716. this->suggestions->release();
  717. this->suggestions = 0;
  718. }
  719. triggerUpdate();
  720. }
  721. bool Framework::InputLine::executeCommand(Text command)
  722. {
  723. RCArray<Text> params;
  724. bool lastArgFinished = 0;
  725. Text* cmd = input.getTeilText(0, cursorPos);
  726. if (!parseCommand(*cmd, params, lastArgFinished))
  727. {
  728. cmd->release();
  729. return false;
  730. }
  731. cmd->release();
  732. Text* name;
  733. if (params.getEintragAnzahl() > 0)
  734. {
  735. name = params.get(0);
  736. params.remove(0);
  737. }
  738. else
  739. {
  740. name = new Text("");
  741. }
  742. RCArray<Text> suggestions;
  743. for (ConsoleCommand* command : commands)
  744. {
  745. if (command->getName().istGleich(*name))
  746. {
  747. name->release();
  748. return command->execute(params);
  749. }
  750. }
  751. name->release();
  752. return false;
  753. }
  754. bool Framework::InputLine::parseCommand(
  755. Text command, RCArray<Text>& split, bool& lastFinished)
  756. {
  757. const char* cmd = command.getText();
  758. Text str;
  759. bool insideString = false;
  760. bool insideString2 = false;
  761. bool lastEscape = false;
  762. lastFinished = false;
  763. while (*cmd)
  764. {
  765. if (*cmd == ' ' && !insideString && !insideString2 && !lastEscape)
  766. {
  767. if (str.getLength() > 0)
  768. {
  769. split.add(new Text(str));
  770. str = "";
  771. lastFinished = true;
  772. }
  773. }
  774. else if (*cmd == '"' && !insideString2 && !lastEscape)
  775. {
  776. insideString = !insideString;
  777. lastFinished = !insideString;
  778. }
  779. else if (*cmd == '\'' && !insideString && !lastEscape)
  780. {
  781. insideString2 = !insideString2;
  782. lastFinished = !insideString2;
  783. }
  784. else if (*cmd == '\\' && !lastEscape)
  785. {
  786. lastEscape = true;
  787. lastFinished = false;
  788. }
  789. else
  790. {
  791. lastEscape = false;
  792. str.append(*cmd);
  793. lastFinished = false;
  794. }
  795. cmd++;
  796. }
  797. if (str.getLength() > 0)
  798. {
  799. split.add(new Text(str));
  800. }
  801. return !insideString && !insideString2 && !lastEscape;
  802. }
  803. Framework::ConsoleListView::ConsoleListView()
  804. : StickyConsoleContent(),
  805. maxColumns(-1),
  806. maxVisibleLines(5),
  807. lineOffset(0)
  808. {}
  809. int Framework::ConsoleListView::getUsedColumns() const
  810. {
  811. if (!zConsoleHandlerRef()) return 0;
  812. int maxWidth = zConsoleHandlerRef()->getWidth();
  813. int columnSize = 0;
  814. for (Text* item : items)
  815. {
  816. if (item->getLength() > columnSize)
  817. {
  818. columnSize = item->getLength();
  819. }
  820. }
  821. columnSize += 4;
  822. if (maxWidth < columnSize)
  823. {
  824. return 0;
  825. }
  826. int columns = maxWidth / columnSize;
  827. if (maxColumns > 0 && columns > maxColumns)
  828. {
  829. columns = maxColumns;
  830. }
  831. if (columns > items.getEintragAnzahl())
  832. {
  833. columns = items.getEintragAnzahl();
  834. }
  835. return columns;
  836. }
  837. int Framework::ConsoleListView::getNeededLines() const
  838. {
  839. int columns = getUsedColumns();
  840. if (!columns)
  841. {
  842. return 0;
  843. }
  844. if (maxColumns > 0 && columns > maxColumns)
  845. {
  846. columns = maxColumns;
  847. }
  848. int lines = items.getEintragAnzahl() / columns;
  849. if (items.getEintragAnzahl() % columns != 0)
  850. {
  851. lines++;
  852. }
  853. return lines;
  854. }
  855. void Framework::ConsoleListView::setMaxVisibleLines(int maxVisibleLines)
  856. {
  857. this->maxVisibleLines = maxVisibleLines;
  858. }
  859. void Framework::ConsoleListView::setLineOffset(int lineOffset)
  860. {
  861. this->lineOffset = lineOffset;
  862. int maxLines = getNeededLines();
  863. if (this->lineOffset > maxLines - maxVisibleLines)
  864. {
  865. this->lineOffset = maxLines - maxVisibleLines;
  866. }
  867. if (this->lineOffset < 0)
  868. {
  869. this->lineOffset = 0;
  870. }
  871. }
  872. void Framework::ConsoleListView::setMaxColumns(int maxColumns)
  873. {
  874. this->maxColumns = maxColumns;
  875. }
  876. void Framework::ConsoleListView::addItem(Text item)
  877. {
  878. item.ersetzen("\n", " ");
  879. int index = 0;
  880. for (Text* curreent : items)
  881. {
  882. int i = 0;
  883. while (i < curreent->getLength() && i < item.getLength())
  884. {
  885. if (curreent->getText()[i] != item.getText()[i])
  886. {
  887. break;
  888. }
  889. i++;
  890. }
  891. if (i == curreent->getLength() && i == item.getLength())
  892. {
  893. return; // item already exists
  894. }
  895. if (i < curreent->getLength() && i == item.getLength())
  896. {
  897. items.add(new Text(item), index);
  898. return;
  899. }
  900. if (i < curreent->getLength() && i < item.getLength()
  901. && curreent->getText()[i] > item.getText()[i])
  902. {
  903. items.add(new Text(item), index);
  904. return;
  905. }
  906. index++;
  907. }
  908. items.add(new Text(item));
  909. }
  910. void Framework::ConsoleListView::clear()
  911. {
  912. items.leeren();
  913. }
  914. const RCArray<Text>& Framework::ConsoleListView::getItems() const
  915. {
  916. return items;
  917. }
  918. int Framework::ConsoleListView::print() const
  919. {
  920. int lines = lineOffset;
  921. int maxLines = getNeededLines();
  922. if (lines > maxLines - maxVisibleLines)
  923. {
  924. lines = maxLines - maxVisibleLines;
  925. }
  926. if (lines < 0)
  927. {
  928. lines = 0;
  929. }
  930. int columns = getUsedColumns();
  931. if (!columns)
  932. {
  933. return 0;
  934. }
  935. int columnSize = zConsoleHandlerRef()->getWidth() / columns;
  936. int printetLines = 0;
  937. int currentColumn = 0;
  938. std::cout << "\33[0K"; // clear current line
  939. for (int i = columns * lines; i < items.getEintragAnzahl(); i++)
  940. {
  941. if (i >= columns * (lines + maxVisibleLines))
  942. {
  943. break;
  944. }
  945. if (currentColumn >= columns)
  946. {
  947. std::cout << "\n\r\33[0K";
  948. printetLines++;
  949. currentColumn++;
  950. }
  951. if (!printetLines)
  952. {
  953. printetLines++;
  954. }
  955. std::cout << items.z(i)->getText();
  956. for (int j = items.z(i)->getLength(); j < columnSize; j++)
  957. {
  958. std::cout << " ";
  959. }
  960. currentColumn++;
  961. }
  962. return printetLines;
  963. }
  964. Framework::ConsoleHandler::ConsoleHandler()
  965. : ReferenceCounter(),
  966. lines(0),
  967. lineCounts(0),
  968. contentCount(0)
  969. {
  970. #ifdef WIN32
  971. hConsole = GetStdHandle(STD_INPUT_HANDLE);
  972. SetConsoleMode(hConsole, ENABLE_PROCESSED_INPUT); // set raw mode
  973. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  974. #else
  975. struct termios t;
  976. tcgetattr(STDIN_FILENO, &t);
  977. t.c_lflag &= ~(ICANON | ECHO); // set raw mode
  978. tcsetattr(STDIN_FILENO, TCSANOW, &t);
  979. #endif
  980. }
  981. Framework::ConsoleHandler::~ConsoleHandler()
  982. {
  983. for (int i = 0; i < contentCount; i++)
  984. {
  985. lines[i]->release();
  986. }
  987. delete[] lines;
  988. delete[] lineCounts;
  989. }
  990. void Framework::ConsoleHandler::addContent(
  991. StickyConsoleContent* content, ConsoleContentPosition pos)
  992. {
  993. cs.lock();
  994. if (content->isInput())
  995. {
  996. for (int i = 0; i < contentCount; i++)
  997. {
  998. if (lines[i]->isInput())
  999. {
  1000. content->release();
  1001. cs.unlock();
  1002. throw "There can only be one input line";
  1003. }
  1004. }
  1005. }
  1006. if (content->zConsoleHandler)
  1007. {
  1008. cs.unlock();
  1009. throw "A console content can only be added to one console handler "
  1010. "at "
  1011. "the same time.";
  1012. }
  1013. content->zConsoleHandler = this;
  1014. contentCount++;
  1015. int* lineCounts = new int[contentCount];
  1016. StickyConsoleContent** lines = new StickyConsoleContent*[contentCount];
  1017. if (pos == ConsoleContentPosition::Top)
  1018. {
  1019. lines[0] = content;
  1020. lineCounts[0] = 0;
  1021. for (int i = 1; i < contentCount; i++)
  1022. {
  1023. lines[i] = this->lines[i - 1];
  1024. lineCounts[i] = this->lineCounts[i - 1];
  1025. }
  1026. }
  1027. else
  1028. {
  1029. lines[contentCount - 1] = content;
  1030. lineCounts[contentCount - 1] = content->print();
  1031. for (int i = 0; i < contentCount - 1; i++)
  1032. {
  1033. lines[i] = this->lines[i];
  1034. lineCounts[i] = this->lineCounts[i];
  1035. }
  1036. }
  1037. delete[] this->lines;
  1038. delete[] this->lineCounts;
  1039. this->lines = lines;
  1040. this->lineCounts = lineCounts;
  1041. InputLine* input = dynamic_cast<InputLine*>(content);
  1042. if (input)
  1043. {
  1044. input->start();
  1045. }
  1046. cs.unlock();
  1047. }
  1048. void Framework::ConsoleHandler::removeContent(StickyConsoleContent* zContent)
  1049. {
  1050. cs.lock();
  1051. int index = -1;
  1052. for (int i = 0; i < contentCount; i++)
  1053. {
  1054. if (lines[i] == zContent)
  1055. {
  1056. index = i;
  1057. break;
  1058. }
  1059. }
  1060. if (index == -1)
  1061. {
  1062. cs.unlock();
  1063. return;
  1064. }
  1065. zContent->zConsoleHandler = 0;
  1066. contentCount--;
  1067. int* lineCounts = new int[contentCount];
  1068. StickyConsoleContent** lines = new StickyConsoleContent*[contentCount];
  1069. for (int i = 0; i < index; i++)
  1070. {
  1071. lines[i] = this->lines[i];
  1072. lineCounts[i] = this->lineCounts[i];
  1073. }
  1074. for (int i = index; i < contentCount; i++)
  1075. {
  1076. lines[i] = this->lines[i + 1];
  1077. lineCounts[i] = this->lineCounts[i + 1];
  1078. }
  1079. delete[] this->lines;
  1080. delete[] this->lineCounts;
  1081. this->lines = lines;
  1082. this->lineCounts = lineCounts;
  1083. if (zContent->isInput())
  1084. {
  1085. InputLine* input = dynamic_cast<InputLine*>(zContent);
  1086. if (input)
  1087. {
  1088. input->start();
  1089. }
  1090. }
  1091. zContent->release();
  1092. cs.unlock();
  1093. }
  1094. int Framework::ConsoleHandler::getWidth() const
  1095. {
  1096. #ifdef WIN32
  1097. CONSOLE_SCREEN_BUFFER_INFO csbi;
  1098. GetConsoleScreenBufferInfo(hConsole, &csbi);
  1099. return csbi.dwSize.X;
  1100. #else
  1101. struct winsize ws;
  1102. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  1103. {
  1104. return 0;
  1105. }
  1106. return ws.ws_col;
  1107. #endif
  1108. }
  1109. int Framework::ConsoleHandler::getHeight() const
  1110. {
  1111. #ifdef WIN32
  1112. CONSOLE_SCREEN_BUFFER_INFO csbi;
  1113. GetConsoleScreenBufferInfo(hConsole, &csbi);
  1114. return csbi.dwSize.Y;
  1115. #else
  1116. struct winsize ws;
  1117. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  1118. {
  1119. return 0;
  1120. }
  1121. return ws.ws_row;
  1122. #endif
  1123. }
  1124. void Framework::ConsoleHandler::clear()
  1125. {
  1126. cs.lock();
  1127. for (int i = 0; i < contentCount; i++)
  1128. {
  1129. lines[i]->release();
  1130. }
  1131. delete[] lines;
  1132. delete[] lineCounts;
  1133. lines = 0;
  1134. lineCounts = 0;
  1135. contentCount = 0;
  1136. cs.unlock();
  1137. }
  1138. void Framework::ConsoleHandler::print()
  1139. {
  1140. cs.lock();
  1141. int totalLines = 0;
  1142. bool adittionalLine = 0;
  1143. for (int i = 0; i < contentCount; i++)
  1144. {
  1145. if (lines[i]->isInput())
  1146. {
  1147. lines[i]->setCursorToBeginning();
  1148. break;
  1149. }
  1150. totalLines += lineCounts[i];
  1151. }
  1152. if (totalLines > 0)
  1153. {
  1154. std::cout << "\33[" << totalLines << "A";
  1155. }
  1156. totalLines = 0;
  1157. for (int i = 0; i < contentCount; i++)
  1158. {
  1159. lineCounts[i] = lines[i]->print();
  1160. if (lineCounts[i] > 0)
  1161. {
  1162. adittionalLine = 0;
  1163. if (i < contentCount - 1)
  1164. {
  1165. std::cout << "\n";
  1166. adittionalLine = 1;
  1167. }
  1168. }
  1169. totalLines += lineCounts[i];
  1170. }
  1171. if (adittionalLine)
  1172. {
  1173. totalLines++;
  1174. }
  1175. std::cout << "\33[" << (totalLines - 1) << "A\r";
  1176. for (int i = 0; i < contentCount; i++)
  1177. {
  1178. if (lines[i]->isInput())
  1179. {
  1180. lines[i]->restoreCursorPos();
  1181. break;
  1182. }
  1183. if (i < contentCount - 1 && lineCounts[i])
  1184. {
  1185. std::cout << "\33[" << lineCounts[i] << "B";
  1186. }
  1187. }
  1188. std::cout << std::flush;
  1189. cs.unlock();
  1190. }
  1191. void Framework::ConsoleHandler::print(Text str)
  1192. {
  1193. std::cout << "\n";
  1194. cs.lock();
  1195. int totalLines = 0;
  1196. bool adittionalLine = 0;
  1197. for (int i = 0; i < contentCount; i++)
  1198. {
  1199. if (lines[i]->isInput())
  1200. {
  1201. lines[i]->setCursorToBeginning();
  1202. break;
  1203. }
  1204. totalLines += lineCounts[i];
  1205. }
  1206. std::cout << "\33[" << totalLines + 1 << "A";
  1207. std::cout << "\33[0K" // clear current line
  1208. << str.getText();
  1209. if (str.getText()[str.getLength() - 1] != '\n')
  1210. {
  1211. std::cout << "\n";
  1212. }
  1213. totalLines = 0;
  1214. for (int i = 0; i < contentCount; i++)
  1215. {
  1216. lineCounts[i] = lines[i]->print();
  1217. if (lineCounts[i] > 0)
  1218. {
  1219. adittionalLine = 0;
  1220. if (i < contentCount - 1)
  1221. {
  1222. std::cout << "\n";
  1223. adittionalLine = 1;
  1224. }
  1225. }
  1226. totalLines += lineCounts[i];
  1227. }
  1228. if (adittionalLine)
  1229. {
  1230. totalLines++;
  1231. }
  1232. std::cout << "\33[" << (totalLines - 1) << "A\r";
  1233. for (int i = 0; i < contentCount; i++)
  1234. {
  1235. if (lines[i]->isInput())
  1236. {
  1237. lines[i]->restoreCursorPos();
  1238. break;
  1239. }
  1240. if (i < contentCount - 1 && lineCounts[i])
  1241. {
  1242. std::cout << "\33[" << lineCounts[i] << "B";
  1243. }
  1244. }
  1245. std::cout << std::flush;
  1246. cs.unlock();
  1247. }