Consolix
Loading...
Searching...
No Matches
ColorManipulator.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CONSOLIX_COLOR_MANIPULATOR_HPP_INCLUDED
3#define _CONSOLIX_COLOR_MANIPULATOR_HPP_INCLUDED
4
7
8#include <iostream>
9
10#ifndef CONSOLIX_DEFAULT_COLOR
11#define CONSOLIX_DEFAULT_COLOR consolix::TextColor::LightGray
12#endif
13
14namespace consolix {
15
22 public:
26
29 TextColor color() const { return m_color; }
30
33# if CONSOLIX_USE_LOGIT == 0
34# if defined(_WIN32) || defined(_WIN64)
35 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
36 SetConsoleTextAttribute(hConsole, to_windows_color(CONSOLIX_DEFAULT_COLOR));
37# else
38 if (isatty(STDOUT_FILENO)) {
39 std::cout << "\033[0m"; // Reset color for ANSI-compatible terminals.
40 }
41# endif
42# endif
43 }
44
45 private:
47 }; // ColorManipulator
48
55
60 inline std::ostream& operator<<(std::ostream& os, const ColorManipulator& manip) {
61 os << to_c_str(manip.color()); // Apply ANSI escape sequence or equivalent.
62 return os;
63 }
64
65} // namespace consolix
66
67#endif // _CONSOLIX_COLOR_MANIPULATOR_HPP_INCLUDED
A utility class for managing text color in streams.
TextColor color() const
Retrieves the current text color.
TextColor m_color
The current text color.
ColorManipulator(TextColor color)
Constructor.
~ColorManipulator()
Destructor that resets the text color to the default.
#define CONSOLIX_DEFAULT_COLOR
Default text color for console output.
< Utility modules and helpers.
ColorManipulator color(TextColor color)
Creates a color manipulator for use in output streams.
TextColor
Represents text colors for console output.
Definition enums.hpp:17
const char * to_c_str(const TextColor &color)
Converts a TextColor to an ANSI escape code string.
Definition enums.hpp:39
std::ostream & operator<<(std::ostream &os, const ColorManipulator &manip)
Overloads the stream operator to apply text color.