From d91ec5ef5b91b2940ec32b69a1cadf099bf0e0ac Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Sat, 8 Jun 2024 22:17:31 +0700 Subject: [PATCH 01/18] Update sort.ff --- tests/sort.ff | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/sort.ff b/tests/sort.ff index 31a0efc..a79c257 100644 --- a/tests/sort.ff +++ b/tests/sort.ff @@ -1,3 +1,4 @@ +@OFF eval "Enter array seperated by spaces: " -ps arr eval -s i 0 From 4afe00a544ce7a11c2befd63d34b57a25678e3c3 Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Sun, 9 Jun 2024 13:08:24 +0700 Subject: [PATCH 02/18] Fix rm command --- src/commands/rm.hpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/commands/rm.hpp b/src/commands/rm.hpp index 1a3cc98..630c6ad 100644 --- a/src/commands/rm.hpp +++ b/src/commands/rm.hpp @@ -17,11 +17,23 @@ class RmCommand : public liteshell::BaseCommand for (auto &target : context.values.at("targets")) { auto targets = utils::list_files(target); + if (targets.empty()) + { + auto message = utils::format("Error: Target \"%s\" does not exist or is empty", target.c_str()); + std::cerr << message << std::endl; + continue; + } + for (auto &target : targets) { if (target.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - utils::remove_directory(utils::utf_convert(target.cFileName), true); + bool result = utils::remove_directory(utils::utf_convert(target.cFileName), true); + if (!result) + { + auto message = utils::last_error(utils::format("Error deleting directory \"%s\"", utils::utf_convert(target.cFileName).c_str())); + std::cerr << message << std::endl; + } } else if (DeleteFileW(target.cFileName)) { From 595481e7207ef61b9c2a9a3b69fefb579e19c85a Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Sun, 9 Jun 2024 17:24:48 +0700 Subject: [PATCH 03/18] Fix rm.hpp again --- src/commands/rm.hpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/commands/rm.hpp b/src/commands/rm.hpp index 630c6ad..5a994af 100644 --- a/src/commands/rm.hpp +++ b/src/commands/rm.hpp @@ -19,7 +19,7 @@ class RmCommand : public liteshell::BaseCommand auto targets = utils::list_files(target); if (targets.empty()) { - auto message = utils::format("Error: Target \"%s\" does not exist or is empty", target.c_str()); + auto message = utils::format("Warning: Target \"%s\" does not exist", target.c_str()); std::cerr << message << std::endl; continue; } @@ -28,12 +28,7 @@ class RmCommand : public liteshell::BaseCommand { if (target.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - bool result = utils::remove_directory(utils::utf_convert(target.cFileName), true); - if (!result) - { - auto message = utils::last_error(utils::format("Error deleting directory \"%s\"", utils::utf_convert(target.cFileName).c_str())); - std::cerr << message << std::endl; - } + utils::remove_directory(utils::utf_convert(target.cFileName), true); } else if (DeleteFileW(target.cFileName)) { From 91fa8649d9a7a6c9440cde3ed291411b06a19723 Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Mon, 10 Jun 2024 20:32:58 +0700 Subject: [PATCH 04/18] Add color command --- src/commands/color.hpp | 33 ++++++++++++++++++++++++++++++++ src/include/client.hpp | 2 +- src/include/utils.hpp | 43 ++++++++++++++++++++++++++++++++++++++++++ src/initialize.hpp | 2 ++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/commands/color.hpp diff --git a/src/commands/color.hpp b/src/commands/color.hpp new file mode 100644 index 0000000..6405de9 --- /dev/null +++ b/src/commands/color.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +class ColorCommand : public liteshell::BaseCommand +{ +public: + ColorCommand() + : liteshell::BaseCommand( + "color", + "Change the text color in the shell", + "Specify a color in hex format to change the text color", + liteshell::CommandConstraint("color", "The color to set", true, false)) {} + + DWORD run(const liteshell::Context &context) + { + if (context.values.find("color") == context.values.end()) + { + std::cerr << "Error: No color specified" << std::endl; + return 1; + } + + std::string color = context.values.at("color").front(); + if (!utils::setColor(color)) + { + std::cerr << "Error: Unable to set color \"" << color << "\"" << std::endl; + return 1; + } + + std::cout << "Color changed to " << color << std::endl; + return 0; + } +}; \ No newline at end of file diff --git a/src/include/client.hpp b/src/include/client.hpp index 3d85fd3..8520d96 100644 --- a/src/include/client.hpp +++ b/src/include/client.hpp @@ -400,7 +400,7 @@ namespace liteshell SYSTEMTIME time; GetLocalTime(&time); std::cout << utils::format("\n[%d:%d:%d]", time.wHour, time.wMinute, time.wSecond); - utils::style_print("liteshell~", FOREGROUND_BLUE | FOREGROUND_INTENSITY); + utils::style_print("liteshell~", FOREGROUND_RED | FOREGROUND_INTENSITY); std::cout << utils::get_working_directory() << ">"; }, 0)); diff --git a/src/include/utils.hpp b/src/include/utils.hpp index 9e0f931..2f56f1b 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -1,5 +1,7 @@ #pragma once +#include +#include #include "converter.hpp" #include "join.hpp" @@ -428,4 +430,45 @@ namespace utils std::transform(result.begin(), result.end(), result.begin(), tolower); return result; } + + void hexToRgb(const std::string &hex, int &r, int &g, int &b) + { + std::stringstream ss; + ss << std::hex << hex.substr(1); + unsigned int hexColor; + ss >> hexColor; + r = (hexColor >> 16) & 0xFF; + g = (hexColor >> 8) & 0xFF; + b = hexColor & 0xFF; + } + + // Set console text color using RGB values + void setColor(int r, int g, int b) + { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFOEX info; + info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX); + GetConsoleScreenBufferInfoEx(hConsole, &info); + + // Modify color table at index 1 + info.ColorTable[1] = RGB(r, g, b); + + SetConsoleScreenBufferInfoEx(hConsole, &info); + SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | 1); + } + + // Wrapper function to set color using hex code + bool setColor(const std::string &hexColor) + { + if (hexColor.size() != 7 || hexColor[0] != '#') + { + std::cerr << "Error: Invalid hex color format" << std::endl; + return false; + } + + int r, g, b; + hexToRgb(hexColor, r, g, b); + setColor(r, g, b); + return true; + } } diff --git a/src/initialize.hpp b/src/initialize.hpp index 7b5dd61..08e0b06 100644 --- a/src/initialize.hpp +++ b/src/initialize.hpp @@ -5,6 +5,7 @@ #include "commands/cat.hpp" #include "commands/cd.hpp" #include "commands/clear.hpp" +#include "commands/color.hpp" #include "commands/date.hpp" #include "commands/echo.hpp" #include "commands/echoln.hpp" @@ -31,6 +32,7 @@ void initialize(liteshell::Client *client) client->add_command() ->add_command() ->add_command() + ->add_command() ->add_command() ->add_command() ->add_command() From 2c1dc51f2b2cc7f3d2f9f3e26b46e22151487c40 Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Mon, 10 Jun 2024 21:30:29 +0700 Subject: [PATCH 05/18] Fix color command --- src/commands/color.hpp | 8 +------- src/include/standard.hpp | 2 ++ src/include/utils.hpp | 2 -- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/commands/color.hpp b/src/commands/color.hpp index 6405de9..a820929 100644 --- a/src/commands/color.hpp +++ b/src/commands/color.hpp @@ -14,17 +14,11 @@ class ColorCommand : public liteshell::BaseCommand DWORD run(const liteshell::Context &context) { - if (context.values.find("color") == context.values.end()) - { - std::cerr << "Error: No color specified" << std::endl; - return 1; - } std::string color = context.values.at("color").front(); if (!utils::setColor(color)) { - std::cerr << "Error: Unable to set color \"" << color << "\"" << std::endl; - return 1; + throw std::invalid_argument("Error: Unable to set color \"" + color + "\""); } std::cout << "Color changed to " << color << std::endl; diff --git a/src/include/standard.hpp b/src/include/standard.hpp index 9fda2a4..3100dd4 100644 --- a/src/include/standard.hpp +++ b/src/include/standard.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include diff --git a/src/include/utils.hpp b/src/include/utils.hpp index 2f56f1b..a511c70 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -1,7 +1,5 @@ #pragma once -#include -#include #include "converter.hpp" #include "join.hpp" From 774aa5a7f7ee91e7d7900ac3b4ac140973fd0633 Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Mon, 10 Jun 2024 21:43:09 +0700 Subject: [PATCH 06/18] Fix color command --- src/include/utils.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index a511c70..a0031d8 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -449,10 +449,10 @@ namespace utils GetConsoleScreenBufferInfoEx(hConsole, &info); // Modify color table at index 1 - info.ColorTable[1] = RGB(r, g, b); + info.ColorTable[10] = RGB(r, g, b); SetConsoleScreenBufferInfoEx(hConsole, &info); - SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | 1); + SetConsoleTextAttribute(hConsole, 10); } // Wrapper function to set color using hex code From d82899b12cba2e26ef7317e59a05e5cca9809978 Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Mon, 10 Jun 2024 22:19:27 +0700 Subject: [PATCH 07/18] Mini fix --- src/commands/color.hpp | 3 +-- src/include/standard.hpp | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/commands/color.hpp b/src/commands/color.hpp index a820929..f9e82d9 100644 --- a/src/commands/color.hpp +++ b/src/commands/color.hpp @@ -14,8 +14,7 @@ class ColorCommand : public liteshell::BaseCommand DWORD run(const liteshell::Context &context) { - - std::string color = context.values.at("color").front(); + std::string color = context.get("color"); if (!utils::setColor(color)) { throw std::invalid_argument("Error: Unable to set color \"" + color + "\""); diff --git a/src/include/standard.hpp b/src/include/standard.hpp index 3100dd4..0c57b55 100644 --- a/src/include/standard.hpp +++ b/src/include/standard.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include From 1b35fd649050194ee0d650546796c6a18a3c78ef Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Tue, 11 Jun 2024 09:43:24 +0700 Subject: [PATCH 08/18] Minor change --- src/include/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index a0031d8..f67f565 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -460,7 +460,7 @@ namespace utils { if (hexColor.size() != 7 || hexColor[0] != '#') { - std::cerr << "Error: Invalid hex color format" << std::endl; + throw std::invalid_argument("Error: Invalid hex color format" + hexColor); return false; } From 2b49df295c1b54b020cbd1fef8c1548ce8d317d5 Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 09:49:17 +0700 Subject: [PATCH 09/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/commands/color.hpp | 2 +- src/include/client.hpp | 2 +- src/include/utils.hpp | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/commands/color.hpp b/src/commands/color.hpp index f9e82d9..63cbd23 100644 --- a/src/commands/color.hpp +++ b/src/commands/color.hpp @@ -23,4 +23,4 @@ class ColorCommand : public liteshell::BaseCommand std::cout << "Color changed to " << color << std::endl; return 0; } -}; \ No newline at end of file +}; diff --git a/src/include/client.hpp b/src/include/client.hpp index e374d40..6134f16 100644 --- a/src/include/client.hpp +++ b/src/include/client.hpp @@ -400,7 +400,7 @@ namespace liteshell SYSTEMTIME time; GetLocalTime(&time); std::cout << utils::format("\n[%d:%d:%d]", time.wHour, time.wMinute, time.wSecond); - utils::style_print("liteshell~", FOREGROUND_RED | FOREGROUND_INTENSITY); + utils::style_print("liteshell~", FOREGROUND_BLUE | FOREGROUND_INTENSITY); std::cout << utils::get_working_directory() << ">"; }, 0)); diff --git a/src/include/utils.hpp b/src/include/utils.hpp index f67f565..9ff506e 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -461,7 +461,6 @@ namespace utils if (hexColor.size() != 7 || hexColor[0] != '#') { throw std::invalid_argument("Error: Invalid hex color format" + hexColor); - return false; } int r, g, b; From 0c86035c98dd1406b15feac5dc78aa26ac269e92 Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:00:14 +0700 Subject: [PATCH 10/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index 9ff506e..ca00347 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -460,7 +460,7 @@ namespace utils { if (hexColor.size() != 7 || hexColor[0] != '#') { - throw std::invalid_argument("Error: Invalid hex color format" + hexColor); + throw std::invalid_argument("Error: Invalid hex color format " + hexColor); } int r, g, b; From 90cb732bfb2746cc83279653aac855462a9b1347 Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:07:09 +0700 Subject: [PATCH 11/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/commands/color.hpp | 5 +---- src/include/utils.hpp | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/commands/color.hpp b/src/commands/color.hpp index 63cbd23..96c72bf 100644 --- a/src/commands/color.hpp +++ b/src/commands/color.hpp @@ -15,10 +15,7 @@ class ColorCommand : public liteshell::BaseCommand DWORD run(const liteshell::Context &context) { std::string color = context.get("color"); - if (!utils::setColor(color)) - { - throw std::invalid_argument("Error: Unable to set color \"" + color + "\""); - } + utils::setColor(color); std::cout << "Color changed to " << color << std::endl; return 0; diff --git a/src/include/utils.hpp b/src/include/utils.hpp index ca00347..d12810c 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -456,7 +456,7 @@ namespace utils } // Wrapper function to set color using hex code - bool setColor(const std::string &hexColor) + void setColor(const std::string &hexColor) { if (hexColor.size() != 7 || hexColor[0] != '#') { @@ -466,6 +466,5 @@ namespace utils int r, g, b; hexToRgb(hexColor, r, g, b); setColor(r, g, b); - return true; } } From bca5ff25ebfdf0de319f291cd8fc76175d7e120f Mon Sep 17 00:00:00 2001 From: khoint5304 Date: Tue, 11 Jun 2024 10:46:36 +0700 Subject: [PATCH 12/18] Fix bug in color command --- src/include/standard.hpp | 1 + src/include/utils.hpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/include/standard.hpp b/src/include/standard.hpp index 0c57b55..8a0277f 100644 --- a/src/include/standard.hpp +++ b/src/include/standard.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include diff --git a/src/include/utils.hpp b/src/include/utils.hpp index d12810c..fd00230 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -440,6 +440,18 @@ namespace utils b = hexColor & 0xFF; } + // Validate hex color code + bool isValidHexColor(const std::string &hexColor) + { + if (hexColor.size() != 7 || hexColor[0] != '#') + { + return false; + } + // Regular expression to match valid hex color code + std::regex hexPattern("#[0-9a-fA-F]{6}"); + return std::regex_match(hexColor, hexPattern); + } + // Set console text color using RGB values void setColor(int r, int g, int b) { @@ -456,15 +468,17 @@ namespace utils } // Wrapper function to set color using hex code - void setColor(const std::string &hexColor) + bool setColor(const std::string &hexColor) { - if (hexColor.size() != 7 || hexColor[0] != '#') + if (!isValidHexColor(hexColor)) { throw std::invalid_argument("Error: Invalid hex color format " + hexColor); + return false; } int r, g, b; hexToRgb(hexColor, r, g, b); setColor(r, g, b); + return true; } } From 97722b65798ef492645af9fa2988f24d72cf2116 Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:52:16 +0700 Subject: [PATCH 13/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/standard.hpp | 1 - src/include/utils.hpp | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/include/standard.hpp b/src/include/standard.hpp index 8a0277f..0c57b55 100644 --- a/src/include/standard.hpp +++ b/src/include/standard.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include diff --git a/src/include/utils.hpp b/src/include/utils.hpp index fd00230..0b5e1b0 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -443,12 +443,8 @@ namespace utils // Validate hex color code bool isValidHexColor(const std::string &hexColor) { - if (hexColor.size() != 7 || hexColor[0] != '#') - { - return false; - } // Regular expression to match valid hex color code - std::regex hexPattern("#[0-9a-fA-F]{6}"); + boost::regex hexPattern("#[0-9a-fA-F]{6}"); return std::regex_match(hexColor, hexPattern); } From 0f391eaf3e1ee6276c04ed8adbed12c846a74cf4 Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:54:58 +0700 Subject: [PATCH 14/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/utils.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index 0b5e1b0..8287c2f 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -469,7 +469,6 @@ namespace utils if (!isValidHexColor(hexColor)) { throw std::invalid_argument("Error: Invalid hex color format " + hexColor); - return false; } int r, g, b; From 6c8721950bf7659fc9e55053d3427603331a713c Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:58:55 +0700 Subject: [PATCH 15/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/utils.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index 8287c2f..a840bb9 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -464,7 +464,7 @@ namespace utils } // Wrapper function to set color using hex code - bool setColor(const std::string &hexColor) + void setColor(const std::string &hexColor) { if (!isValidHexColor(hexColor)) { @@ -474,6 +474,5 @@ namespace utils int r, g, b; hexToRgb(hexColor, r, g, b); setColor(r, g, b); - return true; } } From d64c5cb4e1e95d249d1b2678fc96887e51beaa02 Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:05:16 +0700 Subject: [PATCH 16/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index a840bb9..c71af62 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -445,7 +445,7 @@ namespace utils { // Regular expression to match valid hex color code boost::regex hexPattern("#[0-9a-fA-F]{6}"); - return std::regex_match(hexColor, hexPattern); + return boost::regex_match(hexColor, hexPattern); } // Set console text color using RGB values From 5ea15930c43c27eb65b98338d459a015fcc8b45c Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:14:58 +0700 Subject: [PATCH 17/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/utils.hpp | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index c71af62..91f0187 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -429,28 +429,36 @@ namespace utils return result; } + const boost::regex _hex_pattern("#[0-9a-fA-F]{6}"); + + /** @brief Check if a string is a valid hex representation */ + bool isValidHexColor(const std::string &hexColor) + { + return boost::regex_match(hexColor, _hex_pattern); + } + void hexToRgb(const std::string &hex, int &r, int &g, int &b) { + if (!isValidHexColor(hexColor)) + { + throw std::invalid_argument("Error: Invalid hex color format " + hexColor); + } + std::stringstream ss; ss << std::hex << hex.substr(1); - unsigned int hexColor; + unsigned hexColor; ss >> hexColor; r = (hexColor >> 16) & 0xFF; g = (hexColor >> 8) & 0xFF; b = hexColor & 0xFF; } - // Validate hex color code - bool isValidHexColor(const std::string &hexColor) + /** @brief Wrapper function to set color using hex code */ + void setColor(const std::string &hexColor) { - // Regular expression to match valid hex color code - boost::regex hexPattern("#[0-9a-fA-F]{6}"); - return boost::regex_match(hexColor, hexPattern); - } + int r, g, b; + hexToRgb(hexColor, r, g, b); - // Set console text color using RGB values - void setColor(int r, int g, int b) - { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFOEX info; info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX); @@ -462,17 +470,4 @@ namespace utils SetConsoleScreenBufferInfoEx(hConsole, &info); SetConsoleTextAttribute(hConsole, 10); } - - // Wrapper function to set color using hex code - void setColor(const std::string &hexColor) - { - if (!isValidHexColor(hexColor)) - { - throw std::invalid_argument("Error: Invalid hex color format " + hexColor); - } - - int r, g, b; - hexToRgb(hexColor, r, g, b); - setColor(r, g, b); - } } From 19990269a825772184e0716a35789064e905db2e Mon Sep 17 00:00:00 2001 From: khoint5304 <130224578+khoint5304@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:18:13 +0700 Subject: [PATCH 18/18] Apply suggestions from code review Co-authored-by: Serious-senpai <57554044+Serious-senpai@users.noreply.github.com> --- src/include/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/utils.hpp b/src/include/utils.hpp index 91f0187..34a3947 100644 --- a/src/include/utils.hpp +++ b/src/include/utils.hpp @@ -439,7 +439,7 @@ namespace utils void hexToRgb(const std::string &hex, int &r, int &g, int &b) { - if (!isValidHexColor(hexColor)) + if (!isValidHexColor(hex)) { throw std::invalid_argument("Error: Invalid hex color format " + hexColor); }