diff --git a/README.md b/README.md index 8ed4f9a..e3667d7 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ download MSYS2 from https://www.msys2.org/ and install it run MSYS2, and in the terminal run: ``` pacman -Syuu -pacman -S --needed base-devel mingw-w64-ucrt-x86_64-clang mingw-w64-ucrt-x86_64-gdb mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-sfml mingw-w64-ucrt-x86_64-make +pacman -S --needed base-devel mingw-w64-ucrt-x86_64-clang mingw-w64-ucrt-x86_64-gdb mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-sfml mingw-w64-ucrt-x86_64-make mingw-w64-ucrt-x86_64-boost ``` **Step 3:** diff --git a/compile b/compile index b7df819..266a8e8 100755 --- a/compile +++ b/compile @@ -1,4 +1,4 @@ -#/bin/bash +#!/bin/bash rm -r bin diff --git a/src/ostd/OutputHandlers.cpp b/src/ostd/OutputHandlers.cpp index dc9a87e..1089875 100755 --- a/src/ostd/OutputHandlers.cpp +++ b/src/ostd/OutputHandlers.cpp @@ -165,9 +165,9 @@ namespace ostd return *this; } - IOutputHandler& ConsoleOutputHandler::pStyled(const String& styled) + IOutputHandler& ConsoleOutputHandler::pStyled(const StringEditor& styled) { - return pStyled(TextStyleParser::parse(styled)); + return pStyled(TextStyleParser::parse(styled.str())); } IOutputHandler& ConsoleOutputHandler::pStyled(const TextStyleParser::tStyledString& styled) @@ -175,6 +175,12 @@ namespace ostd std::cout << styled; return *this; } + + IOutputHandler& ConsoleOutputHandler::pStyled(const TextStyleBuilder::IRichStringBase& styled) + { + std::cout << styled.getStyledString(); + return *this; + } IOutputHandler& ConsoleOutputHandler::nl(void) { diff --git a/src/ostd/StringEditor.cpp b/src/ostd/StringEditor.cpp index 977fb1e..b6f8a32 100755 --- a/src/ostd/StringEditor.cpp +++ b/src/ostd/StringEditor.cpp @@ -1,6 +1,7 @@ #include "Utils.hpp" #include #include +#include namespace ostd { @@ -150,6 +151,22 @@ namespace ostd return *this; } + StringEditor& StringEditor::regexReplace(String regex_pattern, String replace_with, bool case_insensitive) + { + try + { + boost::regex rgx(regex_pattern, (case_insensitive ? boost::regex_constants::icase : boost::regex_constants::normal)); + m_data = boost::regex_replace(m_data, rgx, replace_with); + return *this; + } + catch(const boost::regex_error& err) + { + std::cerr << err.what() << '\n'; //TODO: Better error handling + return *this; + } + return *this; + } + StringEditor& StringEditor::put(uint32_t index, char c) { if (index < m_data.length()) diff --git a/src/ostd/TextStyleParser.cpp b/src/ostd/TextStyleParser.cpp index 2781ceb..46d7798 100644 --- a/src/ostd/TextStyleParser.cpp +++ b/src/ostd/TextStyleParser.cpp @@ -18,6 +18,16 @@ namespace ostd return os << builder.getStyledString(); } + std::ostream &operator<<(std::ostream &os, TextStyleBuilder::Regex const ®ex) + { + return os << regex.getStyledString(); + } + + std::ostream &operator<<(std::ostream &os, TextStyleBuilder::IRichStringBase const &rstr) + { + return os << rstr.getStyledString(); + } + void TextStyleParser::tColor::convertToBackground(void) { StringEditor edit(consoleColor); @@ -49,12 +59,12 @@ namespace ostd return text.length() > 0 && text.length() == backgroundColors.size() && text.length() == foregroundColors.size(); } - TextStyleParser::tStyledString TextStyleParser::parse(const String& styledString) + TextStyleParser::tStyledString TextStyleParser::parse(const StringEditor& styledString) { return parse(styledString, ConsoleColors["black"], ConsoleColors["white"]); } - TextStyleParser::tStyledString TextStyleParser::parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor) + TextStyleParser::tStyledString TextStyleParser::parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor) { tStyledString rstring; bool insideBlock = false; @@ -313,4 +323,57 @@ namespace ostd return TextStyleParser::ConsoleColors["black"]; } + + + + TextStyleBuilder::Regex& TextStyleBuilder::Regex::setRawString(const StringEditor& rawString) + { + m_rawString = rawString.str(); + return *this; + } + + TextStyleBuilder::Regex& TextStyleBuilder::Regex::fg(const StringEditor& regex, const StringEditor& foreground_color, bool case_insensitive) + { + String replace_pattern = "[@@ style foreground:" + foreground_color.str(); + replace_pattern += "]$&[@@/]"; + m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str(); + return *this; + } + + TextStyleBuilder::Regex& TextStyleBuilder::Regex::bg(const StringEditor& regex, const StringEditor& background_color, bool case_insensitive) + { + String replace_pattern = "[@@ style background:" + background_color.str(); + replace_pattern += "]$&[@@/]"; + m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str(); + return *this; + } + + TextStyleBuilder::Regex& TextStyleBuilder::Regex::col(const StringEditor& regex, const StringEditor& foreground_color, const StringEditor& background_color, bool case_insensitive) + { + String replace_pattern = "[@@ style background:" + background_color.str(); + replace_pattern += ", foreground:" + foreground_color.str(); + replace_pattern += "]$&[@@/]"; + m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str(); + return *this; + } + + TextStyleParser::tStyledString TextStyleBuilder::Regex::getStyledString(void) const + { + return TextStyleParser::parse(m_rawString); + } + + TextStyleBuilder::Regex& TextStyleBuilder::Regex::print(void) + { + ConsoleOutputHandler out; + out.pStyled(m_rawString); + return *this; + } + + TextStyleBuilder::Regex& TextStyleBuilder::Regex::print(IOutputHandler& out) + { + out.pStyled(m_rawString); + return *this; + } + + } \ No newline at end of file diff --git a/src/ostd/TextStyleParser.hpp b/src/ostd/TextStyleParser.hpp index 0acceb9..d80cac4 100644 --- a/src/ostd/TextStyleParser.hpp +++ b/src/ostd/TextStyleParser.hpp @@ -5,6 +5,7 @@ namespace ostd { + class StringEditor; class TextStyleParser { public: enum class eBlockParserReturnValue { CloseBlock = 0, ValidBlock, InvalidBlock }; @@ -29,8 +30,8 @@ namespace ostd }; public: - static tStyledString parse(const String& styledString); - static tStyledString parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor); + static tStyledString parse(const StringEditor& styledString); + static tStyledString parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor); private: static eBlockParserReturnValue parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor); @@ -65,7 +66,13 @@ namespace ostd class TextStyleBuilder { - public: class Console { + public: class IRichStringBase { + public: + virtual TextStyleParser::tStyledString getStyledString(void) const = 0; + friend std::ostream& operator<<(std::ostream&, IRichStringBase const&); + }; + + public: class Console : public IRichStringBase { public: Console(void); Console& bg(const String& consoleColor); @@ -87,7 +94,7 @@ namespace ostd Console& print(IOutputHandler& out); Console& print(void); - inline TextStyleParser::tStyledString getStyledString(void) const { return m_styledString; } + inline TextStyleParser::tStyledString getStyledString(void) const override { return m_styledString; } friend std::ostream& operator<<(std::ostream&, Console const&); @@ -100,6 +107,28 @@ namespace ostd TextStyleParser::tColor m_foregroundColor; }; + public: class Regex : public IRichStringBase { + public: + inline Regex(void) { m_rawString = ""; } + inline Regex(const StringEditor& rawString) { setRawString(rawString); } + inline String getRawString(void) const { return m_rawString; } + Regex& setRawString(const StringEditor& rawString); + + Regex& fg(const StringEditor& regex, const StringEditor& foreground_color, bool case_insensitive = false); + Regex& bg(const StringEditor& regex, const StringEditor& background_color, bool case_insensitive = false); + Regex& col(const StringEditor& regex, const StringEditor& foreground_color, const StringEditor& background_color, bool case_insensitive = false); + + TextStyleParser::tStyledString getStyledString(void) const override; + + Regex& print(void); + Regex& print(IOutputHandler& out); + + friend std::ostream& operator<<(std::ostream&, Regex const&); + + private: + String m_rawString { "" }; + }; + //TODO: Implement // public: class FullColor { @@ -107,4 +136,5 @@ namespace ostd }; typedef TextStyleBuilder::Console ConsoleRichString; + typedef TextStyleBuilder::Regex RegexRichString; } \ No newline at end of file diff --git a/src/ostd/Utils.hpp b/src/ostd/Utils.hpp index d94c239..b85a034 100755 --- a/src/ostd/Utils.hpp +++ b/src/ostd/Utils.hpp @@ -247,6 +247,7 @@ namespace ostd StringEditor& reverse(void); StringEditor& replaceAll(String what, String with); StringEditor& replaceFirst(String what, String with); + StringEditor& regexReplace(String regex_pattern, String replace_with, bool case_insensitive = false); StringEditor& put(uint32_t index, char c); @@ -341,8 +342,9 @@ namespace ostd virtual IOutputHandler& pi(int64_t i) = 0; virtual IOutputHandler& pf(float f, uint8_t precision = 0) = 0; virtual IOutputHandler& pf(double f, uint8_t precision = 0) = 0; - virtual IOutputHandler& pStyled(const String& styled) = 0; + virtual IOutputHandler& pStyled(const StringEditor& styled) = 0; virtual IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) = 0; + virtual IOutputHandler& pStyled(const TextStyleBuilder::IRichStringBase& styled) = 0; virtual IOutputHandler& nl(void) = 0; virtual IOutputHandler& flush(void) = 0; virtual IOutputHandler& reset(void) = 0; @@ -375,8 +377,9 @@ namespace ostd IOutputHandler& pi(int64_t i) override; IOutputHandler& pf(float f, uint8_t precision = 0) override; IOutputHandler& pf(double f, uint8_t precision = 0) override; - IOutputHandler& pStyled(const String& styled) override; + IOutputHandler& pStyled(const StringEditor& styled) override; IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) override; + IOutputHandler& pStyled(const TextStyleBuilder::IRichStringBase& styled) override; IOutputHandler& nl(void) override; IOutputHandler& flush(void) override; IOutputHandler& reset(void) override; @@ -403,8 +406,9 @@ namespace ostd IOutputHandler& pi(int64_t i) override; IOutputHandler& pf(float f, uint8_t precision = 0) override; IOutputHandler& pf(double f, uint8_t precision = 0) override; - IOutputHandler& pStyled(const String& styled) override { return *this; }; - IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) override { return *this; }; + IOutputHandler& pStyled(const StringEditor& styled) override { return *this; }; + inline IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) override { return *this; } + inline IOutputHandler& pStyled(const TextStyleBuilder::IRichStringBase& styled) override { return *this; }; IOutputHandler& nl(void) override; inline IOutputHandler& flush(void) override { return *this; } inline IOutputHandler& reset(void) override { return *this; } diff --git a/tools/build.nr b/tools/build.nr index 1c38c90..a41cbc3 100755 --- a/tools/build.nr +++ b/tools/build.nr @@ -1 +1 @@ -1725 +1743