Skip to content

Commit

Permalink
code(stylizer)!: refactored the whole file to apply the consistent id…
Browse files Browse the repository at this point in the history
…eas determined for our codebase. The stylizer function has been refactored to be more memory efficient, saving a lot of potential std::string allocations on the concatenations
  • Loading branch information
TheRustifyer committed Nov 12, 2023
1 parent 1859ab1 commit 557a7fe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
53 changes: 38 additions & 15 deletions zero/ifc/text/stylizer.cppm
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
/**
* Provides functions and enums for string styling
*/
/// @file stylizer.hpp
/// Provides functions and enums for string styling.

export module stylizer;

import std;

/// @namespace zero::fmt
/// Namespace containing functions and enums for text styling.
export namespace zero::fmt {

// ANSI escape codes for text styling
constexpr char foreground_color_begin[] = "\033[38;5;"; ///< Start of foreground color
constexpr char modifier_begin[] = "\033["; ///< Start of modifier
constexpr char reset_code[] = "\033[0m"; ///< Reset text styling

/// @enum Color
/// Enumerates ANSI color codes for text styling.
enum class Color {
DEFAULT, // Default console color
DEFAULT, ///< Default console color
RED = 196,
ORANGE = 202,
YELLOW = 226,
Expand All @@ -22,6 +30,8 @@ export namespace zero::fmt {
BLACK = 16
};

/// @enum Modifier
/// Enumerates ANSI modifier codes for text styling.
enum class Modifier {
BOLD = 1,
FAINT = 2,
Expand All @@ -32,19 +42,32 @@ export namespace zero::fmt {
HIDDEN = 8
};

std::string stylize(const std::string& text, Color color, const std::vector<Modifier>& modifiers) {
std::string colorCode = color == Color::DEFAULT ? "" : "\033[38;5;" + std::to_string(static_cast<int>(color)) + "m";
std::string modifierCodes = "";
/// @brief Apply text styling to a given string.
/// @tparam Modifiers Variadic template representing text styling modifiers.
/// @param text The input string to be stylized.
/// @param color The foreground color.
/// @param modifiers Variadic parameters representing text styling modifiers.
/// @return The stylized string.
template <typename... Modifiers>
constexpr std::string stylize(const std::string& text,
const Color color = Color::DEFAULT,
const Modifiers... modifiers)
{
std::ostringstream result;

// Add foreground color
result << foreground_color_begin << static_cast<int>(color) << "m";

for (const auto& modifier : modifiers) {
modifierCodes += "\033[" + std::to_string(static_cast<int>(modifier)) + "m";
}
// Apply modifiers
((result << modifier_begin << static_cast<int>(modifiers) << "m"), ...);

std::string resetCodes = "";
for (size_t i = 0; i < modifiers.size(); ++i) {
resetCodes += "\033[0m";
}
// Append the text
result << text;

return modifierCodes + colorCode + text + resetCodes + "\033[0m";
// Reset text styling
result << reset_code;

return result.str();
}

}
29 changes: 16 additions & 13 deletions zero/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,18 @@ void run_formatter_and_stylize_examples() {
std::cout << "\n\n#######Check stylize########\n\n";


std::string boldText = stylize("[WARNING] This is bold text", Color::YELLOW, {Modifier::BOLD});
std::string faintText = stylize("[INFO] This is faint text", Color::GREEN, {Modifier::FAINT});
std::string italicText = stylize("[DEBUG] This is italic text", Color::BLUE, {Modifier::ITALIC});
std::string underlinedText = stylize("[ERROR] This is underlined text", Color::RED, {Modifier::UNDERLINE});
std::string blinkingText = stylize("[CRITICAL] This is blinking text", Color::PURPLE, {Modifier::BLINK});
std::string reversedText = stylize("[NOTICE] This is reversed text", Color::CYAN, {Modifier::REVERSE});
std::string hiddenText = stylize("[SECRET] This is hidden text", Color::BLACK, {Modifier::HIDDEN});

std::cout << boldText << "\n";
std::string default_text = stylize("[WARNING] This is bold text without color");
std::string bold_text = stylize("[WARNING] This is bold text", Color::YELLOW, Modifier::BOLD);
std::string bold_default_text = stylize("[WARNING] This is bold text without color", Color::DEFAULT, Modifier::BOLD);
std::string faintText = stylize("[INFO] This is faint text", Color::GREEN, Modifier::FAINT);
std::string italicText = stylize("[DEBUG] This is italic text", Color::BLUE, Modifier::ITALIC);
std::string underlinedText = stylize("[ERROR] This is underlined text", Color::RED, Modifier::UNDERLINE);
std::string blinkingText = stylize("[CRITICAL] This is blinking text", Color::PURPLE, Modifier::BLINK);
std::string reversedText = stylize("[NOTICE] This is reversed text", Color::CYAN, Modifier::REVERSE);
std::string hiddenText = stylize("[SECRET] This is hidden text", Color::BLACK, Modifier::HIDDEN);

std::cout << bold_text << "\n";
std::cout << bold_default_text << "\n";
std::cout << faintText << "\n";
std::cout << italicText << "\n";
std::cout << underlinedText << "\n";
Expand All @@ -225,23 +228,23 @@ void run_formatter_and_stylize_examples() {
std::cout << "\n\n#######Check combination full text########\n\n";
std::string format_str1 = "[WARNING] {} is deprecated. Please use {} instead.";
std::string warning_msg = formatter(format_str1, "methodA", "methodB");
std::string stylized_warning_msg = stylize(warning_msg, Color::YELLOW, {Modifier::BOLD});
std::string stylized_warning_msg = stylize(warning_msg, Color::YELLOW, Modifier::BOLD);
std::cout << stylized_warning_msg << std::endl;

std::string format_str2 = "[ERROR] Failed to open file: {}";
std::string error_msg = formatter(format_str2, "/path/to/file");
std::string stylized_error_msg = stylize(error_msg, Color::RED, {Modifier::BOLD, Modifier::UNDERLINE});
std::string stylized_error_msg = stylize(error_msg, Color::RED, Modifier::BOLD, Modifier::UNDERLINE);
std::cout << stylized_error_msg << std::endl;

std::cout << "\n\n#######Check combination partial text########\n\n";
std::string format_str3 = "{} Successfully connected to server: {}";
std::string stylized_info_msg = stylize("[INFO]", Color::GREEN, {Modifier::FAINT});
std::string stylized_info_msg = stylize("[INFO]", Color::GREEN, Modifier::FAINT);
std::string info_msg = formatter(format_str3,stylized_info_msg, "192.168.1.1");
std::cout << info_msg << std::endl;

std::cout << "\n\n#######Check unicode symbols########\n";
std::string format_str4 = "{} Triple integral symbol: {}";
std::string stylized_info_msg_2 = stylize("[INFO]", Color::GREEN, {Modifier::FAINT});
std::string stylized_info_msg_2 = stylize("[INFO]", Color::GREEN, Modifier::FAINT);
std::string info_msg_2 = formatter(format_str4, stylized_info_msg_2, "");
std::cout << info_msg_2 << "\n\n";
}
Expand Down

0 comments on commit 557a7fe

Please sign in to comment.