Consolix
Toggle main menu visibility
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
14
namespace
consolix
{
15
21
class
ColorManipulator
{
22
public
:
25
explicit
ColorManipulator
(
TextColor
color
) :
m_color
(
color
) {}
26
29
TextColor
color
()
const
{
return
m_color
; }
30
32
~ColorManipulator
() {
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
:
46
TextColor
m_color
;
47
};
// ColorManipulator
48
52
inline
ColorManipulator
color
(
TextColor
color
) {
53
return
ColorManipulator
(
color
);
54
}
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
consolix::ColorManipulator
A utility class for managing text color in streams.
Definition
ColorManipulator.hpp:21
consolix::ColorManipulator::color
TextColor color() const
Retrieves the current text color.
Definition
ColorManipulator.hpp:29
consolix::ColorManipulator::m_color
TextColor m_color
The current text color.
Definition
ColorManipulator.hpp:46
consolix::ColorManipulator::ColorManipulator
ColorManipulator(TextColor color)
Constructor.
Definition
ColorManipulator.hpp:25
consolix::ColorManipulator::~ColorManipulator
~ColorManipulator()
Destructor that resets the text color to the default.
Definition
ColorManipulator.hpp:32
CONSOLIX_DEFAULT_COLOR
#define CONSOLIX_DEFAULT_COLOR
Default text color for console output.
Definition
config_macros.hpp:127
consolix
< Utility modules and helpers.
Definition
BaseLoopComponent.hpp:13
consolix::color
ColorManipulator color(TextColor color)
Creates a color manipulator for use in output streams.
Definition
ColorManipulator.hpp:52
consolix::TextColor
TextColor
Represents text colors for console output.
Definition
enums.hpp:19
consolix::to_c_str
const char * to_c_str(const TextColor &color)
Converts a TextColor to an ANSI escape code string.
Definition
enums.hpp:41
consolix::operator<<
std::ostream & operator<<(std::ostream &os, const ColorManipulator &manip)
Overloads the stream operator to apply text color.
Definition
ColorManipulator.hpp:60
include
consolix
utils
ColorManipulator.hpp
Generated by
1.17.0