Added way to get Console size, in ostd::Utils class

This commit is contained in:
OmniaX 2023-12-04 02:54:21 +01:00
parent 9ab0cad97f
commit 09a0cec35d
5 changed files with 76 additions and 18 deletions

View file

@ -30,10 +30,20 @@ void Utils::clearConsole(void)
SetConsoleCursorPosition( hStdOut, homeCoords ); SetConsoleCursorPosition( hStdOut, homeCoords );
} }
void Utils::getConsoleSize(int32_t& outRows, int32_t& outColumns)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
outColumns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
outRows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__) #elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
#include <unistd.h> #include <unistd.h>
#include <term.h> #include <term.h>
#include <sys/ioctl.h>
void Utils::clearConsole(void) void Utils::clearConsole(void)
{ {
@ -47,5 +57,28 @@ void Utils::clearConsole(void)
putp(tigetstr( "clear" )); putp(tigetstr( "clear" ));
} }
void Utils::getConsoleSize(int32_t& outRows, int32_t& outColumns)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
outRows = w.ws_row;
outColumns = w.ws_col;
}
#endif #endif
int32_t Utils::getConsoleWidth(void)
{
int32_t rows = 0, cols = 0;
getConsoleSize(rows, cols);
return cols;
}
int32_t Utils::getConsoleHeight(void)
{
int32_t rows = 0, cols = 0;
getConsoleSize(rows, cols);
return rows;
}
} }

View file

@ -31,15 +31,21 @@ namespace ostd
void TextStyleParser::tColor::convertToBackground(void) void TextStyleParser::tColor::convertToBackground(void)
{ {
StringEditor edit(consoleColor); StringEditor edit(consoleColor);
if (edit.startsWith("o-")) return; if (edit.startsWith("o-") || edit.startsWith("ob-")) return;
consoleColor = "o-" + edit.str(); if (edit.startsWith("b-"))
consoleColor = "o" + edit.str();
else
consoleColor = "o-" + edit.str();
} }
void TextStyleParser::tColor::convertToForeground(void) void TextStyleParser::tColor::convertToForeground(void)
{ {
StringEditor edit(consoleColor); StringEditor edit(consoleColor);
if (!edit.startsWith("o-")) return; if (!edit.startsWith("o-") && !edit.startsWith("ob-")) return;
consoleColor = edit.substr(2); if (edit.startsWith("ob-"))
consoleColor = edit.substr(3);
else
consoleColor = edit.substr(2);
} }
void TextStyleParser::tStyledString::add(const String& str, tColor background, tColor foreground) void TextStyleParser::tStyledString::add(const String& str, tColor background, tColor foreground)
@ -85,21 +91,25 @@ namespace ostd
{ {
if (insideBlock) if (insideBlock)
return tStyledString(); //TODO: Error, no nested blocks allowed return tStyledString(); //TODO: Error, no nested blocks allowed
insideBlock = true; if (test_for_block(StringEditor(_styledString).substr(i)))
continue; {
insideBlock = true;
continue;
}
} }
if(c == ']') if(c == ']')
{ {
if (!insideBlock) if (insideBlock)
return tStyledString(); //TODO: Error, closing block without opeinng one {
insideBlock = false; insideBlock = false;
validBlockStart = false; validBlockStart = false;
countBlockStart = 0; countBlockStart = 0;
auto blockParseResult = parse_block(blockText, bgcol, fgcol); auto blockParseResult = parse_block(blockText, bgcol, fgcol);
if (blockParseResult == eBlockParserReturnValue::InvalidBlock) if (blockParseResult == eBlockParserReturnValue::InvalidBlock)
return tStyledString(); //TODO: Error, Invalid block return tStyledString(); //TODO: Error, Invalid block
blockText = ""; blockText = "";
continue; continue;
}
} }
if (!insideBlock) if (!insideBlock)
{ {
@ -128,6 +138,16 @@ namespace ostd
return rstring; return rstring;
} }
bool TextStyleParser::test_for_block(const String& block_part)
{
if (block_part.length() < 3) return false;
if (block_part.starts_with("[@@"))
{
return true;
}
return false;
}
TextStyleParser::eBlockParserReturnValue TextStyleParser::parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor) TextStyleParser::eBlockParserReturnValue TextStyleParser::parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor)
{ {
StringEditor blockEditor = blockString; StringEditor blockEditor = blockString;

View file

@ -34,6 +34,7 @@ namespace ostd
static tStyledString parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor); static tStyledString parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
private: private:
static bool test_for_block(const String& block_part);
static eBlockParserReturnValue parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor); static eBlockParserReturnValue parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor);
static const tColor parse_color(const String& colorStr); static const tColor parse_color(const String& colorStr);
@ -58,7 +59,7 @@ namespace ostd
{ "yellow", { { 255, 255, 0, 255 }, "yellow" } }, { "yellow", { { 255, 255, 0, 255 }, "yellow" } },
{ "brightyellow", { { 255, 255, 170, 255 }, "b-yellow" } }, { "brightyellow", { { 255, 255, 170, 255 }, "b-yellow" } },
{ "black", { { 0, 0, 0, 255 }, "gray" } }, { "black", { { 0, 0, 0, 255 }, "gray" } },
{ "Gray", { { 50, 50, 50, 255 }, "b-gray" } }, { "gray", { { 50, 50, 50, 255 }, "b-gray" } },
{ "brightgray", { { 150, 150, 150, 255 }, "lgray" } }, { "brightgray", { { 150, 150, 150, 255 }, "lgray" } },
{ "white", { { 255, 255, 255, 255 }, "white" } } { "white", { { 255, 255, 255, 255 }, "white" } }
}; };

View file

@ -170,7 +170,11 @@ namespace ostd
static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath); static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath);
static int32_t solveIntegerExpression(const String& expr); static int32_t solveIntegerExpression(const String& expr);
static void clearConsole(void); static void clearConsole(void);
static void getConsoleSize(int32_t& outRows, int32_t& outColumns);
static int32_t getConsoleWidth(void);
static int32_t getConsoleHeight(void);
private: private:
inline static uint64_t s_startTime_ms; inline static uint64_t s_startTime_ms;

View file

@ -1 +1 @@
1743 1758