Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color command #23

Merged
merged 22 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/commands/color.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <all.hpp>

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)
{
std::string color = context.get("color");
utils::setColor(color);

std::cout << "Color changed to " << color << std::endl;
return 0;
}
};
1 change: 1 addition & 0 deletions src/include/standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <optional>
#include <sstream>
#include <stack>
#include <unordered_map>

#include <pathcch.h>
#include <windows.h>
Expand Down
42 changes: 42 additions & 0 deletions src/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,46 @@ namespace utils
std::transform(result.begin(), result.end(), result.begin(), tolower);
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))
khoint5304 marked this conversation as resolved.
Show resolved Hide resolved
{
throw std::invalid_argument("Error: Invalid hex color format " + hexColor);
}

std::stringstream ss;
ss << std::hex << hex.substr(1);
unsigned hexColor;
ss >> hexColor;
r = (hexColor >> 16) & 0xFF;
g = (hexColor >> 8) & 0xFF;
b = hexColor & 0xFF;
}

/** @brief Wrapper function to set color using hex code */
void setColor(const std::string &hexColor)
{
int r, g, b;
hexToRgb(hexColor, r, g, 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[10] = RGB(r, g, b);

SetConsoleScreenBufferInfoEx(hConsole, &info);
SetConsoleTextAttribute(hConsole, 10);
}
}
2 changes: 2 additions & 0 deletions src/initialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,6 +32,7 @@ void initialize(liteshell::Client *client)
client->add_command<CatCommand>()
->add_command<CdCommand>()
->add_command<ClearCommand>()
->add_command<ColorCommand>()
->add_command<DateCommand>()
->add_command<EchoCommand>()
->add_command<EcholnCommand>()
Expand Down
Loading