Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
CommonSupportConsole.h
1// trax track library
2// AD 2024
3//
4// "the resolution of all the fruitless searches"
5//
6// Peter Gabriel
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29#ifdef _WIN32
30# define WIN32_LEAN_AND_MEAN
31# define VC_EXTRALEAN
32# ifndef NOMINMAX
33# define NOMINMAX
34# endif
35# include <Windows.h> // for displaying colors
36#endif // Windows
37
38#include <iostream>
39#include <string>
40
41namespace common{
42
43enum class TextColor {
44 black = 0,
45 dark_blue = 1,
46 dark_green = 2,
47 light_blue = 3,
48 dark_red = 4,
49 magenta = 5,
50 orange = 6,
51 light_gray = 7,
52 gray = 8,
53 blue = 9,
54 green = 10,
55 cyan = 11,
56 red = 12,
57 pink = 13,
58 yellow = 14,
59 white = 15,
60
61 reset = light_gray,
62};
63
64enum class BackgroundColor {
65 black = 0,
66 dark_blue = 1,
67 dark_green = 2,
68 light_blue = 3,
69 dark_red = 4,
70 magenta = 5,
71 orange = 6,
72 light_gray = 7,
73 gray = 8,
74 blue = 9,
75 green = 10,
76 cyan = 11,
77 red = 12,
78 pink = 13,
79 yellow = 14,
80 white = 15,
81
82 reset = black,
83};
84
85#if defined(__linux__)
86
87inline std::string get_textcolor_code( TextColor textcolor ) {
88 switch(textcolor)
89 {
90 case TextColor::black: return "30";
91 case TextColor::dark_blue: return "34";
92 case TextColor::dark_green: return "32";
93 case TextColor::light_blue: return "36";
94 case TextColor::dark_red: return "31";
95 case TextColor::magenta: return "35";
96 case TextColor::orange: return "33";
97 case TextColor::light_gray: return "37";
98 case TextColor::gray: return "90";
99 case TextColor::blue: return "94";
100 case TextColor::green: return "92";
101 case TextColor::cyan: return "96";
102 case TextColor::red: return "91";
103 case TextColor::pink: return "95";
104 case TextColor::yellow: return "93";
105 case TextColor::white: return "97";
106 default: return "37";
107 }
108}
109
110inline std::string get_backgroundcolor_code( BackgroundColor backgroundcolor) {
111 switch( backgroundcolor )
112 {
113 case BackgroundColor::black: return "40";
114 case BackgroundColor::dark_blue: return "44";
115 case BackgroundColor::dark_green: return "42";
116 case BackgroundColor::light_blue: return "46";
117 case BackgroundColor::dark_red: return "41";
118 case BackgroundColor::magenta: return "45";
119 case BackgroundColor::orange: return "43";
120 case BackgroundColor::light_gray: return "47";
121 case BackgroundColor::gray: return "100";
122 case BackgroundColor::blue: return "104";
123 case BackgroundColor::green: return "102";
124 case BackgroundColor::cyan: return "106";
125 case BackgroundColor::red: return "101";
126 case BackgroundColor::pink: return "105";
127 case BackgroundColor::yellow: return "103";
128 case BackgroundColor::white: return "107";
129 default: return "40";
130 }
131}
132#endif
133
134
135inline std::ostream& operator<<( std::ostream& stream, TextColor color ) {
136#if defined(_WIN32)
137 ::SetConsoleTextAttribute( ::GetStdHandle(STD_OUTPUT_HANDLE), static_cast<const int>(color) );
138#elif defined(__linux__)
139 stream << "\033["+get_textcolor_code(color)+"m";
140#endif // Windows/Linux
141 return stream;
142}
143
144inline std::ostream& operator<<( std::ostream& stream, BackgroundColor color ) {
145#if defined(_WIN32)
146 ::SetConsoleTextAttribute( ::GetStdHandle(STD_OUTPUT_HANDLE), static_cast<const int>(color) << 4 );
147#elif defined(__linux__)
148 stream << "\033["+get_backgroundcolor_code(color)+"m";
149#endif // Windows/Linux
150 return stream;
151}
152
153inline std::ostream& rendl( std::ostream& stream ) {
154 stream << TextColor::reset << std::endl;
155 return stream;
156}
157
158} // namespace common
Namespace of common utility classes and methods.
Definition Helpers.h:43