12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310 |
- #include "Console.h"
- #include <iostream>
- #ifndef WIN32
- # include <termios.h>
- # include <unistd.h>
- #endif
- using namespace Framework;
- ConsoleCommand::ConsoleCommand(Text name)
- : ReferenceCounter(),
- name(name)
- {}
- ConsoleCommand::~ConsoleCommand() {}
- const Text& Framework::ConsoleCommand::getName() const
- {
- return name;
- }
- Framework::StickyConsoleContent::StickyConsoleContent()
- : ReferenceCounter(),
- length(0),
- content(0),
- color(0),
- backgroundColor(0),
- zConsoleHandler(0)
- {}
- Framework::StickyConsoleContent::~StickyConsoleContent()
- {
- delete[] content;
- delete[] color;
- delete[] backgroundColor;
- }
- int Framework::StickyConsoleContent::getLength() const
- {
- return length;
- }
- void Framework::StickyConsoleContent::setContent(
- int length, const char* content)
- {
- delete[] this->content;
- this->content = new char[length];
- memcpy(this->content, content, length);
- this->length = length;
- delete[] color;
- delete[] backgroundColor;
- color = 0;
- backgroundColor = 0;
- }
- void Framework::StickyConsoleContent::setContent(
- int length, const char* content, Color color)
- {
- setContent(length, content);
- this->color = new Color[length];
- for (int i = 0; i < length; i++)
- {
- this->color[i] = color;
- }
- }
- void Framework::StickyConsoleContent::setContent(
- int length, const char* content, Color color, Color backgroundColor)
- {
- setContent(length, content, color);
- this->backgroundColor = new Color[length];
- for (int i = 0; i < length; i++)
- {
- this->backgroundColor[i] = backgroundColor;
- }
- }
- void Framework::StickyConsoleContent::setContent(
- int length, const char* content, Color* color)
- {
- setContent(length, content);
- this->color = new Color[length];
- for (int i = 0; i < length; i++)
- {
- this->color[i] = color[i];
- }
- }
- void Framework::StickyConsoleContent::setContent(
- int length, const char* content, Color* color, Color* backgroundColor)
- {
- setContent(length, content, color);
- this->backgroundColor = new Color[length];
- for (int i = 0; i < length; i++)
- {
- this->backgroundColor[i] = backgroundColor[i];
- }
- }
- void Framework::StickyConsoleContent::repaceContent(
- int start, int length, int newLength, const char* newContent)
- {
- int resultLength = this->length + newLength - length;
- char* resultContent = new char[resultLength];
- memcpy(resultContent, content, start);
- memcpy(resultContent + start, newContent, newLength);
- memcpy(resultContent + start + newLength,
- content + start + length,
- this->length - start - length);
- delete[] content;
- content = resultContent;
- if (color)
- {
- Color* resultColor = new Color[resultLength];
- memcpy(resultColor, color, start);
- for (int i = 0; i < newLength; i++)
- {
- resultColor[start + i] = color[start];
- }
- memcpy(resultColor + start + newLength,
- color + start + length,
- sizeof(Color) * (this->length - start - length));
- delete[] color;
- color = resultColor;
- }
- if (backgroundColor)
- {
- Color* resultBgColor = new Color[resultLength];
- memcpy(resultBgColor, backgroundColor, start);
- for (int i = 0; i < newLength; i++)
- {
- resultBgColor[start + i] = backgroundColor[start];
- }
- memcpy(resultBgColor + start + newLength,
- backgroundColor + start + length,
- sizeof(Color) * (this->length - start - length));
- delete[] backgroundColor;
- backgroundColor = resultBgColor;
- }
- this->length = resultLength;
- }
- void Framework::StickyConsoleContent::repaceContent(
- int start, int length, int newLength, const char* newContent, Color color)
- {
- repaceContent(start, length, newLength, newContent);
- if (!this->color)
- {
- this->color = new Color[this->length];
- for (int i = 0; i < this->length; i++)
- {
- this->color[i] = (Color)-1;
- }
- }
- for (int i = start; i < start + newLength; i++)
- {
- this->color[i] = color;
- }
- }
- void Framework::StickyConsoleContent::repaceContent(int start,
- int length,
- int newLength,
- const char* newContent,
- Color color,
- Color backgroundColor)
- {
- repaceContent(start, length, newLength, newContent, color);
- if (!this->backgroundColor)
- {
- this->backgroundColor = new Color[this->length];
- for (int i = 0; i < this->length; i++)
- {
- this->backgroundColor[i] = (Color)-1;
- }
- }
- for (int i = start; i < start + newLength; i++)
- {
- this->backgroundColor[i] = backgroundColor;
- }
- }
- void Framework::StickyConsoleContent::repaceContent(
- int start, int length, int newLength, const char* newContent, Color* color)
- {
- repaceContent(start, length, newLength, newContent);
- if (!this->color)
- {
- this->color = new Color[this->length];
- for (int i = 0; i < this->length; i++)
- {
- this->color[i] = (Color)-1;
- }
- }
- memcpy(this->color + start, color, sizeof(Color) * newLength);
- }
- void Framework::StickyConsoleContent::repaceContent(int start,
- int length,
- int newLength,
- const char* newContent,
- Color* color,
- Color* backgroundColor)
- {
- repaceContent(start, length, newLength, newContent, color);
- if (!this->backgroundColor)
- {
- this->backgroundColor = new Color[this->length];
- for (int i = 0; i < this->length; i++)
- {
- this->backgroundColor[i] = (Color)-1;
- }
- }
- memcpy(this->backgroundColor + start,
- backgroundColor,
- sizeof(Color) * newLength);
- }
- void Framework::StickyConsoleContent::triggerUpdate()
- {
- if (zConsoleHandler != 0)
- {
- zConsoleHandler->print();
- }
- }
- bool Framework::StickyConsoleContent::isInput()
- {
- return false;
- }
- void Framework::StickyConsoleContent::setConsoleHandlerZ(
- ConsoleHandler* zConsoleHandler)
- {
- this->zConsoleHandler = zConsoleHandler;
- }
- void Framework::StickyConsoleContent::setCursorToBeginning() {}
- int Framework::StickyConsoleContent::print() const
- {
- int lineCount = 0;
- int maxWidth = zConsoleHandler->getWidth();
- std::cout << "\r\33[0K"; // erase current line
- int lineLength = 0;
- int lastColor = -1;
- int lastBgColor = -1;
- for (int i = 0; i < length; i++)
- {
- if (color && (int)color[i] != lastColor)
- {
- if ((int)color[i] == -1)
- {
- std::cout << "\033[0m";
- if (backgroundColor && (int)backgroundColor[i] != -1)
- {
- if (backgroundColor[i] < Color::LIGHT_BLACK)
- {
- std::cout << Text("\033[1;")
- + Text(40 + (int)backgroundColor[i])
- + "m";
- }
- else
- {
- std::cout
- << Text("\033[1;")
- + Text(100 + (int)backgroundColor[i] - 8)
- + "m";
- }
- }
- }
- else if (color[i] < Color::LIGHT_BLACK)
- {
- std::cout << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
- }
- else
- {
- std::cout << Text("\033[1;") + Text(90 + (int)color[i] - 8)
- + "m";
- }
- }
- if (backgroundColor && (int)backgroundColor[i] != lastBgColor)
- {
- if ((int)backgroundColor[i] == -1)
- {
- std::cout << "\033[0m";
- if (color && (int)color[i] != -1)
- {
- if (color[i] < Color::LIGHT_BLACK)
- {
- std::cout
- << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
- }
- else
- {
- std::cout << Text("\033[1;")
- + Text(90 + (int)color[i] - 8) + "m";
- }
- }
- }
- else if (backgroundColor[i] < Color::LIGHT_BLACK)
- {
- std::cout << Text("\033[1;")
- + Text(40 + (int)backgroundColor[i]) + "m";
- }
- else
- {
- std::cout << Text("\033[1;")
- + Text(100 + (int)backgroundColor[i] - 8)
- + "m";
- }
- }
- std::cout << content[i];
- lastColor = color ? (int)color[i] : -1;
- lastBgColor = backgroundColor ? (int)backgroundColor[i] : -1;
- if ((content[i] == '\n' || lineLength == maxWidth) && i < length - 1)
- {
- if (lineLength == maxWidth && content[i] != '\n')
- {
- std::cout << "\n";
- }
- lineLength = 0;
- lineCount++;
- }
- else
- {
- lineLength++;
- }
- }
- if (lastColor != -1 || lastBgColor != -1) std::cout << "\033[0m";
- if (lineCount == 0 && lineLength > 0)
- {
- lineCount++;
- }
- return lineCount;
- }
- void Framework::StickyConsoleContent::restoreCursorPos() {}
- ConsoleHandler* Framework::StickyConsoleContent::zConsoleHandlerRef() const
- {
- return zConsoleHandler;
- }
- Framework::ConsoleProgressBar::ConsoleProgressBar()
- : StickyConsoleContent(),
- progress(0),
- maxProgress(1),
- maxWidth(-1)
- {}
- void Framework::ConsoleProgressBar::setMaxWidth(int maxWidth)
- {
- this->maxWidth = maxWidth;
- }
- void Framework::ConsoleProgressBar::setProgress(int progress)
- {
- if (progress < 0) progress = 0;
- this->progress = progress;
- }
- void Framework::ConsoleProgressBar::setMaxProgress(int maxProgress)
- {
- if (maxProgress < 0) maxProgress = 0;
- this->maxProgress = maxProgress;
- }
- int Framework::ConsoleProgressBar::getProgress() const
- {
- return progress;
- }
- int Framework::ConsoleProgressBar::print() const
- {
- int maxWidth = zConsoleHandlerRef()->getWidth();
- int width = this->maxWidth == -1 ? maxWidth : this->maxWidth;
- if (width > maxWidth)
- {
- width = maxWidth;
- }
- if (width < 7)
- {
- return 0;
- }
- std::cout << "\r\33[0K"; // erase current line
- int progress = this->progress > maxProgress ? maxProgress : this->progress;
- int progressChars = width - 7;
- std::cout << "[\033[1;47m";
- int progressWidth = (int)(((double)progress / maxProgress) * progressChars);
- if (progressWidth > progressChars)
- {
- progressWidth = progressChars;
- }
- for (int i = 0; i < progressWidth; i++)
- {
- std::cout << " ";
- }
- std::cout << "\033[0m";
- for (int i = 0; i < progressChars - progressWidth; i++)
- {
- std::cout << " ";
- }
- std::cout << "] ";
- Text str((int)(((double)progress / maxProgress) * 100));
- for (int i = 0; i < 3 - str.getLength(); i++)
- {
- std::cout << " ";
- }
- std::cout << str.getText() << "%";
- return 1;
- }
- Framework::InputLine::InputLine()
- : StickyConsoleContent(),
- Thread(),
- input(""),
- cursorPos(0),
- suggestions(0)
- {}
- Framework::InputLine::~InputLine()
- {
- suggestions->release();
- }
- void Framework::InputLine::addPossibleCommand(ConsoleCommand* command)
- {
- commands.add(command);
- }
- bool Framework::InputLine::isInput()
- {
- return true;
- }
- void Framework::InputLine::setCursorToBeginning()
- {
- cs.lock();
- std::cout << "\r";
- }
- int Framework::InputLine::print() const
- {
- std::cout << "\33[0K" // clear current line
- << input.getText();
- if (suggestions)
- {
- std::cout << "\n";
- return 1 + dynamic_cast<StickyConsoleContent*>(suggestions)->print();
- }
- return 1;
- }
- void Framework::InputLine::restoreCursorPos()
- {
- if (cursorPos > 0)
- {
- std::cout << Text("\33[") + Text(cursorPos) + "C";
- }
- cs.unlock();
- }
- void Framework::InputLine::thread()
- {
- #ifdef WIN32
- INPUT_RECORD inputRecord;
- DWORD eventsRead;
- HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
- while (true)
- {
- if (ReadConsoleInput(handle, &inputRecord, 1, &eventsRead))
- {
- cs.lock();
- if (eventsRead > 0 && inputRecord.EventType == KEY_EVENT
- && inputRecord.Event.KeyEvent.bKeyDown)
- {
- if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
- {
- cursorPos--;
- if (cursorPos < 0)
- {
- cursorPos = 0;
- }
- else
- {
- std::cout << "\33[1D" << std::flush;
- }
- }
- else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_HOME)
- { // Pos1 key moves cursor to beginning of line
- std::cout << "\r" << std::flush;
- cursorPos = 0;
- }
- else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
- {
- cursorPos++;
- if (cursorPos > input.getLength())
- {
- cursorPos = input.getLength();
- }
- else
- {
- std::cout << "\33[1C" << std::flush;
- }
- }
- else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_END)
- { // Pos1 key moves cursor to beginning of line
- cursorPos = input.getLength();
- std::cout << Text("\r\33[") + Text(cursorPos) + "C"
- << std::flush;
- }
- else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_TAB)
- {
- applyAutocompletion();
- }
- else if (inputRecord.Event.KeyEvent.wVirtualKeyCode
- == VK_RETURN)
- {
- std::cout << "\r\33[0K" << std::flush; // eraze line
- Text cmd = input;
- cs.unlock();
- if (executeCommand(cmd))
- {
- cs.lock();
- input = "";
- cursorPos = 0;
- cs.unlock();
- triggerUpdate();
- }
- continue; // skip the unlock
- }
- else if (inputRecord.Event.KeyEvent.wVirtualKeyCode == VK_BACK)
- {
- if (cursorPos > 0)
- {
- input.remove(cursorPos - 1, cursorPos);
- cursorPos--;
- std::cout << "\r\33[0K" // eraze line
- << input.getText() // output current input
- // move cursor to current position
- << Text("\r\33[") + Text(cursorPos) + "C"
- << std::flush;
- }
- }
- else if (inputRecord.Event.KeyEvent.uChar.AsciiChar)
- {
- input.insert(
- cursorPos, inputRecord.Event.KeyEvent.uChar.AsciiChar);
- cursorPos++;
- std::cout << "\r\33[0K" // eraze line
- << input.getText() // output current input
- // move cursor to current position
- << Text("\r\33[") + Text(cursorPos) + "C"
- << std::flush;
- }
- }
- cs.unlock();
- }
- }
- #else
- char c;
- while (read(STDIN_FILENO, &c, 1) == 1)
- {
- if (c == 27)
- { // Check for the escape key (27 is the ASCII code for escape)
- char seq[2];
- if (read(STDIN_FILENO, &seq, 2) == 2)
- {
- cs.lock();
- if (seq[0] == '[')
- {
- if (seq[1] == 'D')
- { // left arrow key
- cursorPos--;
- if (cursorPos < 0)
- {
- cursorPos = 0;
- }
- else
- {
- std::cout << "\33[1D" << std::flush;
- }
- }
- else if (seq[1] == 'H')
- { // Pos1 key moves cursor to beginning of line
- std::cout << "\r" << std::flush;
- cursorPos = 0;
- }
- else if (seq[1] == 'C')
- { // right arrow key
- cursorPos++;
- if (cursorPos > input.getLength())
- {
- cursorPos = input.getLength();
- }
- else
- {
- std::cout << "\33[1C" << std::flush;
- }
- }
- else if (seq[1] == 'F')
- { // End key moves cursor to end of line
- cursorPos = input.getLength();
- std::cout << Text("\r\33[") + Text(cursorPos) + "C"
- << std::flush;
- }
- }
- cs.unlock();
- }
- }
- else if (c == 8)
- { // backspace
- if (cursorPos > 0)
- {
- cs.lock();
- input.remove(cursorPos - 1, cursorPos);
- cursorPos--;
- std::cout << "\r\33[0K" // eraze line
- << input.getText() // output current input
- // move cursor to current position
- << Text("\r\33[") + Text(cursorPos) + "C"
- << std::flush;
- cs.unlock();
- }
- }
- else if (c == 9)
- { // tab
- cs.lock();
- applyAutocompletion();
- cs.unlock();
- }
- else if (c == 13)
- { // enter
- cs.lock();
- std::cout << "\r\33[0K" << std::flush; // eraze line
- Text cmd = input;
- cs.unlock();
- if (executeCommand(cmd))
- {
- cs.lock();
- input = "";
- cursorPos = 0;
- cs.unlock();
- }
- }
- else
- {
- cs.lock();
- input.insert(cursorPos, c);
- cursorPos++;
- std::cout << "\r\33[0K" // eraze line
- << input.getText() // output current input
- // move cursor to current position
- << Text("\r\33[") + Text(cursorPos) + "C" << std::flush;
- cs.unlock();
- }
- }
- #endif
- }
- void Framework::InputLine::applyAutocompletion()
- {
- RCArray<Text> params;
- bool lastArgFinished = 0;
- Text* cmd = input.getTeilText(0, cursorPos);
- parseCommand(*cmd, params, lastArgFinished);
- cmd->release();
- bool paramsStarted = params.getEintragAnzahl() > 1 || lastArgFinished;
- Text* name;
- if (params.getEintragAnzahl() > 0)
- {
- name = params.get(0);
- params.remove(0);
- }
- else
- {
- name = new Text("");
- }
- RCArray<Text> suggestions;
- for (ConsoleCommand* command : commands)
- {
- if ((!paramsStarted && command->getName().hatAt(0, *name))
- || (paramsStarted && command->getName().istGleich(*name)))
- {
- if (paramsStarted)
- {
- command->addAutocompletePossibilities(
- params, !lastArgFinished, suggestions);
- break;
- }
- else
- {
- suggestions.add(new Text(command->getName()));
- }
- }
- }
- if (this->suggestions)
- {
- this->suggestions->clear();
- }
- else
- {
- this->suggestions = new ConsoleListView();
- this->suggestions->setConsoleHandlerZ(zConsoleHandlerRef());
- }
- for (Text* suggestion : suggestions)
- {
- this->suggestions->addItem(*suggestion);
- }
- if (this->suggestions->getItems().getEintragAnzahl() > 0)
- {
- bool commonChar = true;
- for (int i
- = lastArgFinished
- ? 0
- : (params.getEintragAnzahl() > 0
- ? params.z(params.getEintragAnzahl() - 1)->getLength()
- : name->getLength());
- true;
- i++)
- {
- if (i >= this->suggestions->getItems().z(0)->getLength())
- {
- commonChar = false;
- break;
- }
- for (int j = 1;
- j < this->suggestions->getItems().getEintragAnzahl();
- j++)
- {
- if (i >= this->suggestions->getItems().z(j)->getLength())
- {
- commonChar = false;
- break;
- }
- if (this->suggestions->getItems().z(j)->getText()[i]
- != this->suggestions->getItems().z(0)->getText()[i])
- {
- commonChar = false;
- break;
- }
- }
- if (!commonChar)
- {
- break;
- }
- input.insert(
- cursorPos, this->suggestions->getItems().z(0)->getText()[i]);
- cursorPos++;
- }
- }
- name->release();
- if (this->suggestions->getItems().getEintragAnzahl() <= 1)
- {
- this->suggestions->release();
- this->suggestions = 0;
- }
- triggerUpdate();
- }
- bool Framework::InputLine::executeCommand(Text command)
- {
- RCArray<Text> params;
- bool lastArgFinished = 0;
- Text* cmd = input.getTeilText(0, cursorPos);
- if (!parseCommand(*cmd, params, lastArgFinished))
- {
- cmd->release();
- return false;
- }
- cmd->release();
- Text* name;
- if (params.getEintragAnzahl() > 0)
- {
- name = params.get(0);
- params.remove(0);
- }
- else
- {
- name = new Text("");
- }
- RCArray<Text> suggestions;
- for (ConsoleCommand* command : commands)
- {
- if (command->getName().istGleich(*name))
- {
- name->release();
- return command->execute(params);
- }
- }
- name->release();
- return false;
- }
- bool Framework::InputLine::parseCommand(
- Text command, RCArray<Text>& split, bool& lastFinished)
- {
- const char* cmd = command.getText();
- Text str;
- bool insideString = false;
- bool insideString2 = false;
- bool lastEscape = false;
- lastFinished = false;
- while (*cmd)
- {
- if (*cmd == ' ' && !insideString && !insideString2 && !lastEscape)
- {
- if (str.getLength() > 0)
- {
- split.add(new Text(str));
- str = "";
- lastFinished = true;
- }
- }
- else if (*cmd == '"' && !insideString2 && !lastEscape)
- {
- insideString = !insideString;
- lastFinished = !insideString;
- }
- else if (*cmd == '\'' && !insideString && !lastEscape)
- {
- insideString2 = !insideString2;
- lastFinished = !insideString2;
- }
- else if (*cmd == '\\' && !lastEscape)
- {
- lastEscape = true;
- lastFinished = false;
- }
- else
- {
- lastEscape = false;
- str.append(*cmd);
- lastFinished = false;
- }
- cmd++;
- }
- if (str.getLength() > 0)
- {
- split.add(new Text(str));
- }
- return !insideString && !insideString2 && !lastEscape;
- }
- Framework::ConsoleListView::ConsoleListView()
- : StickyConsoleContent(),
- maxColumns(-1),
- maxVisibleLines(5),
- lineOffset(0)
- {}
- int Framework::ConsoleListView::getUsedColumns() const
- {
- if (!zConsoleHandlerRef()) return 0;
- int maxWidth = zConsoleHandlerRef()->getWidth();
- int columnSize = 0;
- for (Text* item : items)
- {
- if (item->getLength() > columnSize)
- {
- columnSize = item->getLength();
- }
- }
- columnSize += 4;
- if (maxWidth < columnSize)
- {
- return 0;
- }
- int columns = maxWidth / columnSize;
- if (maxColumns > 0 && columns > maxColumns)
- {
- columns = maxColumns;
- }
- if (columns > items.getEintragAnzahl())
- {
- columns = items.getEintragAnzahl();
- }
- return columns;
- }
- int Framework::ConsoleListView::getNeededLines() const
- {
- int columns = getUsedColumns();
- if (!columns)
- {
- return 0;
- }
- if (maxColumns > 0 && columns > maxColumns)
- {
- columns = maxColumns;
- }
- int lines = items.getEintragAnzahl() / columns;
- if (items.getEintragAnzahl() % columns != 0)
- {
- lines++;
- }
- return lines;
- }
- void Framework::ConsoleListView::setMaxVisibleLines(int maxVisibleLines)
- {
- this->maxVisibleLines = maxVisibleLines;
- }
- void Framework::ConsoleListView::setLineOffset(int lineOffset)
- {
- this->lineOffset = lineOffset;
- int maxLines = getNeededLines();
- if (this->lineOffset > maxLines - maxVisibleLines)
- {
- this->lineOffset = maxLines - maxVisibleLines;
- }
- if (this->lineOffset < 0)
- {
- this->lineOffset = 0;
- }
- }
- void Framework::ConsoleListView::setMaxColumns(int maxColumns)
- {
- this->maxColumns = maxColumns;
- }
- void Framework::ConsoleListView::addItem(Text item)
- {
- item.ersetzen("\n", " ");
- int index = 0;
- for (Text* curreent : items)
- {
- int i = 0;
- while (i < curreent->getLength() && i < item.getLength())
- {
- if (curreent->getText()[i] != item.getText()[i])
- {
- break;
- }
- i++;
- }
- if (i == curreent->getLength() && i == item.getLength())
- {
- return; // item already exists
- }
- if (i < curreent->getLength() && i == item.getLength())
- {
- items.add(new Text(item), index);
- return;
- }
- if (i < curreent->getLength() && i < item.getLength()
- && curreent->getText()[i] > item.getText()[i])
- {
- items.add(new Text(item), index);
- return;
- }
- index++;
- }
- items.add(new Text(item));
- }
- void Framework::ConsoleListView::clear()
- {
- items.leeren();
- }
- const RCArray<Text>& Framework::ConsoleListView::getItems() const
- {
- return items;
- }
- int Framework::ConsoleListView::print() const
- {
- int lines = lineOffset;
- int maxLines = getNeededLines();
- if (lines > maxLines - maxVisibleLines)
- {
- lines = maxLines - maxVisibleLines;
- }
- if (lines < 0)
- {
- lines = 0;
- }
- int columns = getUsedColumns();
- if (!columns)
- {
- return 0;
- }
- int columnSize = zConsoleHandlerRef()->getWidth() / columns;
- int printetLines = 0;
- int currentColumn = 0;
- std::cout << "\33[0K"; // clear current line
- for (int i = columns * lines; i < items.getEintragAnzahl(); i++)
- {
- if (i >= columns * (lines + maxVisibleLines))
- {
- break;
- }
- if (currentColumn >= columns)
- {
- std::cout << "\n\r\33[0K";
- printetLines++;
- currentColumn++;
- }
- if (!printetLines)
- {
- printetLines++;
- }
- std::cout << items.z(i)->getText();
- for (int j = items.z(i)->getLength(); j < columnSize; j++)
- {
- std::cout << " ";
- }
- currentColumn++;
- }
- return printetLines;
- }
- Framework::ConsoleHandler::ConsoleHandler()
- : ReferenceCounter(),
- lines(0),
- lineCounts(0),
- contentCount(0)
- {
- #ifdef WIN32
- hConsole = GetStdHandle(STD_INPUT_HANDLE);
- SetConsoleMode(hConsole, ENABLE_PROCESSED_INPUT); // set raw mode
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- #else
- struct termios t;
- tcgetattr(STDIN_FILENO, &t);
- t.c_lflag &= ~(ICANON | ECHO); // set raw mode
- tcsetattr(STDIN_FILENO, TCSANOW, &t);
- #endif
- }
- Framework::ConsoleHandler::~ConsoleHandler()
- {
- for (int i = 0; i < contentCount; i++)
- {
- lines[i]->release();
- }
- delete[] lines;
- delete[] lineCounts;
- }
- void Framework::ConsoleHandler::addContent(
- StickyConsoleContent* content, ConsoleContentPosition pos)
- {
- cs.lock();
- if (content->isInput())
- {
- for (int i = 0; i < contentCount; i++)
- {
- if (lines[i]->isInput())
- {
- content->release();
- cs.unlock();
- throw "There can only be one input line";
- }
- }
- }
- if (content->zConsoleHandler)
- {
- cs.unlock();
- throw "A console content can only be added to one console handler "
- "at "
- "the same time.";
- }
- content->zConsoleHandler = this;
- contentCount++;
- int* lineCounts = new int[contentCount];
- StickyConsoleContent** lines = new StickyConsoleContent*[contentCount];
- if (pos == ConsoleContentPosition::Top)
- {
- lines[0] = content;
- lineCounts[0] = 0;
- for (int i = 1; i < contentCount; i++)
- {
- lines[i] = this->lines[i - 1];
- lineCounts[i] = this->lineCounts[i - 1];
- }
- }
- else
- {
- lines[contentCount - 1] = content;
- lineCounts[contentCount - 1] = content->print();
- for (int i = 0; i < contentCount - 1; i++)
- {
- lines[i] = this->lines[i];
- lineCounts[i] = this->lineCounts[i];
- }
- }
- delete[] this->lines;
- delete[] this->lineCounts;
- this->lines = lines;
- this->lineCounts = lineCounts;
- InputLine* input = dynamic_cast<InputLine*>(content);
- if (input)
- {
- input->start();
- }
- cs.unlock();
- }
- void Framework::ConsoleHandler::removeContent(StickyConsoleContent* zContent)
- {
- cs.lock();
- int index = -1;
- for (int i = 0; i < contentCount; i++)
- {
- if (lines[i] == zContent)
- {
- index = i;
- break;
- }
- }
- if (index == -1)
- {
- cs.unlock();
- return;
- }
- zContent->zConsoleHandler = 0;
- contentCount--;
- int* lineCounts = new int[contentCount];
- StickyConsoleContent** lines = new StickyConsoleContent*[contentCount];
- for (int i = 0; i < index; i++)
- {
- lines[i] = this->lines[i];
- lineCounts[i] = this->lineCounts[i];
- }
- for (int i = index; i < contentCount; i++)
- {
- lines[i] = this->lines[i + 1];
- lineCounts[i] = this->lineCounts[i + 1];
- }
- delete[] this->lines;
- delete[] this->lineCounts;
- this->lines = lines;
- this->lineCounts = lineCounts;
- if (zContent->isInput())
- {
- InputLine* input = dynamic_cast<InputLine*>(zContent);
- if (input)
- {
- input->start();
- }
- }
- zContent->release();
- cs.unlock();
- }
- int Framework::ConsoleHandler::getWidth() const
- {
- #ifdef WIN32
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(hConsole, &csbi);
- return csbi.dwSize.X;
- #else
- struct winsize ws;
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
- {
- return 0;
- }
- return ws.ws_col;
- #endif
- }
- int Framework::ConsoleHandler::getHeight() const
- {
- #ifdef WIN32
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(hConsole, &csbi);
- return csbi.dwSize.Y;
- #else
- struct winsize ws;
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
- {
- return 0;
- }
- return ws.ws_row;
- #endif
- }
- void Framework::ConsoleHandler::clear()
- {
- cs.lock();
- for (int i = 0; i < contentCount; i++)
- {
- lines[i]->release();
- }
- delete[] lines;
- delete[] lineCounts;
- lines = 0;
- lineCounts = 0;
- contentCount = 0;
- cs.unlock();
- }
- void Framework::ConsoleHandler::print()
- {
- cs.lock();
- int totalLines = 0;
- bool adittionalLine = 0;
- for (int i = 0; i < contentCount; i++)
- {
- if (lines[i]->isInput())
- {
- lines[i]->setCursorToBeginning();
- break;
- }
- totalLines += lineCounts[i];
- }
- if (totalLines > 0)
- {
- std::cout << "\33[" << totalLines << "A";
- }
- totalLines = 0;
- for (int i = 0; i < contentCount; i++)
- {
- lineCounts[i] = lines[i]->print();
- if (lineCounts[i] > 0)
- {
- adittionalLine = 0;
- if (i < contentCount - 1)
- {
- std::cout << "\n";
- adittionalLine = 1;
- }
- }
- totalLines += lineCounts[i];
- }
- if (adittionalLine)
- {
- totalLines++;
- }
- std::cout << "\33[" << (totalLines - 1) << "A\r";
- for (int i = 0; i < contentCount; i++)
- {
- if (lines[i]->isInput())
- {
- lines[i]->restoreCursorPos();
- break;
- }
- if (i < contentCount - 1 && lineCounts[i])
- {
- std::cout << "\33[" << lineCounts[i] << "B";
- }
- }
- std::cout << std::flush;
- cs.unlock();
- }
- void Framework::ConsoleHandler::print(Text str)
- {
- std::cout << "\n";
- cs.lock();
- int totalLines = 0;
- bool adittionalLine = 0;
- for (int i = 0; i < contentCount; i++)
- {
- if (lines[i]->isInput())
- {
- lines[i]->setCursorToBeginning();
- break;
- }
- totalLines += lineCounts[i];
- }
- std::cout << "\33[" << totalLines + 1 << "A";
- std::cout << "\33[0K" // clear current line
- << str.getText();
- if (str.getText()[str.getLength() - 1] != '\n')
- {
- std::cout << "\n";
- }
- totalLines = 0;
- for (int i = 0; i < contentCount; i++)
- {
- lineCounts[i] = lines[i]->print();
- if (lineCounts[i] > 0)
- {
- adittionalLine = 0;
- if (i < contentCount - 1)
- {
- std::cout << "\n";
- adittionalLine = 1;
- }
- }
- totalLines += lineCounts[i];
- }
- if (adittionalLine)
- {
- totalLines++;
- }
- std::cout << "\33[" << (totalLines - 1) << "A\r";
- for (int i = 0; i < contentCount; i++)
- {
- if (lines[i]->isInput())
- {
- lines[i]->restoreCursorPos();
- break;
- }
- if (i < contentCount - 1 && lineCounts[i])
- {
- std::cout << "\33[" << lineCounts[i] << "B";
- }
- }
- std::cout << std::flush;
- cs.unlock();
- }
|