diff --git a/components/Rect.h b/components/Rect.h index 34fdd47..ff3ec65 100644 --- a/components/Rect.h +++ b/components/Rect.h @@ -29,28 +29,33 @@ public: SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(color_)) | FrontColorToWinColor(getFrontColor(color_))); + std::string outputTarget = "|"; for(int i = 1; i < height - 1 && top + i < csbi.dwSize.Y - 1; i++) { if(left >= 0) { SetConsoleCursorPosition(hConsole, {static_cast(left - 1), static_cast(top + i - 1)}); - putchar('|'); + WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL); } if(left + width - 1 < csbi.dwSize.X) { SetConsoleCursorPosition(hConsole, {static_cast(left + width - 2), static_cast(top + i - 1)}); - putchar('|'); + WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL); } } + outputTarget = "-"; if(top >= 0) { for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) { if(i > 1 && i < strlen(title_.plainText().c_str()) + 2) continue; SetConsoleCursorPosition(hConsole, {static_cast(left + i - 1), static_cast(top - 1)}); - putchar('-'); + WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL); } } if(top + height - 1 < csbi.dwSize.Y) { + outputTarget = ""; for(int i = 0; i < width && left + i < csbi.dwSize.X; i++) { - SetConsoleCursorPosition(hConsole, {static_cast(left + i - 1), static_cast(top + height - 2)}); - putchar('-'); + outputTarget += "-"; + } + SetConsoleCursorPosition(hConsole, {static_cast(left - 1), static_cast(top + height - 2)}); + WriteConsoleA(hConsole, outputTarget.c_str(), outputTarget.length(), NULL, NULL); } //绘制标题 diff --git a/components/Text b/components/Text new file mode 100644 index 0000000..a115d1a Binary files /dev/null and b/components/Text differ diff --git a/components/Text.h b/components/Text.h index 8808e01..4ad62ae 100644 --- a/components/Text.h +++ b/components/Text.h @@ -6,6 +6,7 @@ #include "../mystl/my_vector.h" #include #include "../utils/RichText.h" +#include class Text : public BaseComponent { private: @@ -31,15 +32,19 @@ public: lines_.clear(); RichText line; maxLineWidth_ = 0; + // std::ofstream out("log.txt"); for(auto part : text.getParts()) { if(part.text.find('\n') == std::string::npos) { line += part; // printf("Add: %s\n", part.text.c_str()); } else { size_t begin = 0; - while((begin = part.text.find('\n', begin)) != std::string::npos) { + while((begin = part.text.find('\n', 0)) != std::string::npos) { line += StringPart(part.text.substr(0, begin), part.color); // printf("Add: %s\n", part.text.substr(0, begin).c_str()); + if(line.plainText() == "\n" || line.plainText() == "\r\n" || line.plainText() == "\r"){ + line = ""; + } lines_.push_back(line); if(maxLineWidth_ < line.length()) { maxLineWidth_ = line.length(); @@ -53,10 +58,16 @@ public: // printf("Add: %s\n", part.text.c_str()); } } + if(line.plainText() == "\n") { + line = ""; + } lines_.push_back(line); if(maxLineWidth_ < line.length()) { maxLineWidth_ = line.length(); } + // for(int i = 0; i < lines_.size(); i++) { + // out << lines_[i].plainText() << std::endl << "-------------------------------------------" << std::endl; + // } } RichText getText() { @@ -71,17 +82,34 @@ public: for(int i = viewTop_; i < viewTop_ + height && i < lines_.size(); i++) { RichText drawTarget = lines_[i].substr(viewLeft_, std::min(width, csbi.dwSize.X - left)); - SetConsoleCursorPosition(hConsole, {static_cast(left - 1), static_cast(top + i - viewTop_ - 1)}); + int currentLength = 0; + CHAR_INFO buffer[width * 1]; for(auto part : drawTarget.getParts()) { - SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color))); - printf("%s", part.text.c_str()); + // SetConsoleTextAttribute(hConsole, BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color))); + // printf("%s", part.text.c_str()); + // SetConsoleCursorPosition(hConsole, {static_cast(left + currentLength - 1), static_cast(top + i - viewTop_ - 1)}); + // WriteConsoleA(hConsole, part.text.c_str(), part.text.length(), NULL, NULL); + for(int j = 0; j < part.text.length(); j++) { + buffer[currentLength + j].Char.UnicodeChar = part.text[j]; + buffer[currentLength + j].Attributes = BackgroundColorToWinColor(getBackColor(part.color)) | FrontColorToWinColor(getFrontColor(part.color)); + } + currentLength += part.text.length(); } CONSOLE_SCREEN_BUFFER_INFO currentCSBI; GetConsoleScreenBufferInfo(hConsole, ¤tCSBI); - for(int cx = currentCSBI.dwCursorPosition.X; cx < left + width - 1; cx++) { - SetConsoleCursorPosition(hConsole, {static_cast(cx), static_cast(currentCSBI.dwCursorPosition.Y)}); - printf(" "); + // std::string space = ""; + for(int j = 0; j < width - drawTarget.plainText().length(); j++) { + // space += " "; + buffer[currentLength + j].Char.UnicodeChar = ' '; + buffer[currentLength + j].Attributes = currentCSBI.wAttributes; } + // SetConsoleCursorPosition(hConsole, {static_cast(left + drawTarget.plainText().length() - 1), static_cast(top + i - viewTop_ - 1)}); + // WriteConsoleA(hConsole, space.c_str(), space.length(), NULL, NULL); + SetConsoleCursorPosition(hConsole, {static_cast(left), static_cast(top + i - viewTop_)}); + COORD bufferSize = {static_cast(width), 1}; + COORD bufferCoord = {0, 0}; + SMALL_RECT rect = {static_cast(left - 1), static_cast(top + i - viewTop_ - 1), static_cast(left + width - 1), static_cast(top + i - viewTop_ + 1)}; + WriteConsoleOutput(hConsole, buffer, bufferSize, bufferCoord, &rect); } } diff --git a/components/TextArea_test.cpp b/components/TextArea_test.cpp index ed75877..4a44e85 100644 --- a/components/TextArea_test.cpp +++ b/components/TextArea_test.cpp @@ -9,7 +9,7 @@ int main() { richText += StringPart(" with colors!\n", COLOR_RED); richText += "And more text.\n"; richText += StringPart("And more more more text.", COLOR_GREEN); - richText += " And more more more more more text.\n"; + richText += " And more more more more more text.\n\n111\n\n"; richText += StringPart("And more more more more more more text.", COLOR_BLUE); richText += " And more more more more more more more text.\n"; richText += StringPart(" With some spaces before And more more more more more more more more text.\n", COLOR_YELLOW); @@ -73,7 +73,6 @@ int main() { // // 切换到后台缓冲区,显示绘制的内容 // SetConsoleActiveScreenBuffer(hConsole); } - Sleep(1); } return 0; diff --git a/components/log.txt b/components/log.txt new file mode 100644 index 0000000..e91ab0b --- /dev/null +++ b/components/log.txt @@ -0,0 +1,22 @@ +Hello, World with colors! +------------------------------------------- +And more text. +------------------------------------------- +And more more more text. And more more more more more text. +------------------------------------------- + +------------------------------------------- +111 +------------------------------------------- + +------------------------------------------- +And more more more more more more text. And more more more more more more more text. +------------------------------------------- + With some spaces before And more more more more more more more more text. +------------------------------------------- +And more more more more more more more more more text. +------------------------------------------- +And more more more more more more more more more more text. +------------------------------------------- +Last Key: 113 +------------------------------------------- diff --git a/utils/SyntaxHighlighter_test.cpp b/utils/SyntaxHighlighter_test.cpp index b74bfff..68bb4b6 100644 --- a/utils/SyntaxHighlighter_test.cpp +++ b/utils/SyntaxHighlighter_test.cpp @@ -3,7 +3,7 @@ int main() { SyntaxHighlighter highlighter = SyntaxHighlighter("cpp"); - TextArea textArea = TextArea(1, 1, 100, 28); + TextArea textArea = TextArea(1, 1, 100, 50); std::string rawText; // read in SyntaxHighlighter_test.cpp std::ifstream file("SyntaxHighlighter_test.cpp"); @@ -33,6 +33,7 @@ int main() { textArea.setTitle(RichText("SyntaxHighlighter Test", COLOR_LIGHTRED)); textArea.setText(richText); + // textArea.setText(RichText(rawText, COLOR_WHITE)); textArea.draw(); while(true) { diff --git a/utils/log.txt b/utils/log.txt new file mode 100644 index 0000000..ec843ac --- /dev/null +++ b/utils/log.txt @@ -0,0 +1,78 @@ +#include "SyntaxHighlighter.h" +#include "../components/TextArea.h" + +int main() { + SyntaxHighlighter highlighter = SyntaxHighlighter("cpp"); + TextArea textArea = TextArea(1, 1, 100, 50); + std::string rawText; + // read in SyntaxHighlighter_test.cpp + std::ifstream file("SyntaxHighlighter_test.cpp"); + std::string line; + while (std::getline(file, line)) { + rawText += line + "\n"; + } + file.close(); + + RichText richText = highlighter.highlight(rawText); + + // // ̨ + // HANDLE hBackBuffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL); + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + + // // պ̨ + // CONSOLE_SCREEN_BUFFER_INFO csbi; + // GetConsoleScreenBufferInfo(hConsole, &csbi); + // DWORD dwBytesWritten; + // FillConsoleOutputCharacter(hBackBuffer, ' ', csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten); + // FillConsoleOutputAttribute(hBackBuffer, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten); + CONSOLE_CURSOR_INFO cci; + cci.bVisible = false; + cci.dwSize = 1; + //SetConsoleCursorInfo(hBackBuffer, &cci); + SetConsoleCursorInfo(hConsole, &cci); + + textArea.setTitle(RichText("SyntaxHighlighter Test", COLOR_LIGHTRED)); + textArea.setText(richText); + // textArea.setText(RichText(rawText, COLOR_WHITE)); + textArea.draw(); + + while(true) { + if (_kbhit()) { + // SetConsoleActiveScreenBuffer(hBackBuffer); + char opt = _getch(); + + switch(opt) { + case 72: + textArea.moveUp(); + break; + case 80: + textArea.moveDown(); + break; + case 75: + textArea.moveLeft(); + break; + case 77: + textArea.moveRight(); + break; + case 'q': + // SetConsoleActiveScreenBuffer(hConsole); + // CloseHandle(hBackBuffer); + return 0; + } + + // // պ̨ + // FillConsoleOutputCharacter(hBackBuffer, ' ', csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten); + // FillConsoleOutputAttribute(hBackBuffer, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, {0, 0}, &dwBytesWritten); + + // // ں̨л + textArea.draw(); + + // // л̨ʾƵ + // SetConsoleActiveScreenBuffer(hConsole); + } + Sleep(1); + } + + return 0; +} +