-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTRIMCORE_ConsoleInfo.h
92 lines (77 loc) · 2.96 KB
/
TRIMCORE_ConsoleInfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef TRIMCORE_DLL_CONSOLEINFO_H
#define TRIMCORE_DLL_CONSOLEINFO_H
#include <cstdint>
#pragma warning (disable:26812) // hint to prefer 'enum class'
namespace TRIMCORE {
// ConsoleInfo
// - handle and possible state of console output redirection
//
struct ConsoleInfo {
enum Redirection : std::uint8_t {
None = 0, // not redirected, output displayed on console
File = 1, // redirected to disk file
Pipe = 3, // redirected to pipe (other program)
NoConsole = 0xFF // the process is not attached to any console
};
public:
HANDLE handle = NULL;
Redirection redirection = None;
bool vt = false;
bool cmdexe = false; // true if the console is shared with 'cmd.exe'
DWORD processes = 0; // number of processes that share the console
COLORREF colors [16] = { 0 };
public:
// update
// - refreshes public variables according to current process state
//
inline void update () noexcept;
// redirected
// - returns false, when console output goes to console or nowhere, otherwise true
//
inline bool redirected () const noexcept {
return this->redirection != None
&& this->redirection != NoConsole;
}
};
// Describe
// - converts 'ConsoleInfo::Redirection' to text
//
inline std::wstring Describe (ConsoleInfo::Redirection r, DescriptionFormatting * format = nullptr) {
switch (r) {
case ConsoleInfo::None: return Describe (L"-", format);
case ConsoleInfo::File: return Describe (L"file", format);
case ConsoleInfo::Pipe: return Describe (L"pipe", format);
case ConsoleInfo::NoConsole: return Describe (L"null", format);
}
return Describe ((std::uint8_t) r, format);
}
// Describe ConsoleInfo
// - describe current console info, handle value, vt support and redirection status
//
inline std::wstring Describe (const ConsoleInfo & info, DescriptionFormatting * format = nullptr) {
auto s = Describe ((const void *) info.handle, format);
if (info.redirection != ConsoleInfo::None) {
s += L" (" + Describe (info.redirection, format) + L")";
}
s += L", vt:" + Describe (info.vt, format);
return s;
}
// EnableConsoleVTMode
// - attempts to enable VT mode for RGB colors in console
// - returns true if supported and successfully enabled
//
TRIMCORE_DLL_IMPORT bool EnableConsoleVTMode ();
// UpdateConsoleInfo
// -
//
TRIMCORE_DLL_IMPORT void UpdateConsoleInfo (ConsoleInfo *) noexcept;
}
// console
// - global instance of ConsoleInfo, only one is ever required
//
TRIMCORE_DLL_IMPORT TRIMCORE::ConsoleInfo console;
// forwarding call
inline void TRIMCORE::ConsoleInfo::update () noexcept {
return TRIMCORE::UpdateConsoleInfo (this);
}
#endif