-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.cpp
More file actions
98 lines (84 loc) · 2.08 KB
/
Copy pathconsole.cpp
File metadata and controls
98 lines (84 loc) · 2.08 KB
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
93
94
95
96
97
98
#include "console.h"
#include <iostream>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
namespace console
{
const std::string kClearLine = "\x1b[2K";
const std::string kSaveCursor = "\x1b[s";
const std::string kRestoreCursor = "\x1b[u";
const char* kReset = "\x1b[0m";
const char* kBold = "\x1b[1m";
const char* kDim = "\x1b[2m";
const char* kRed = "\x1b[31m";
const char* kGreen = "\x1b[32m";
const char* kYellow = "\x1b[33m";
const char* kCyan = "\x1b[36m";
const char* kGray = "\x1b[90m";
void EnableAnsi()
{
#if defined(_WIN32)
// Opt the console into ANSI/VT escape handling. Without this, Windows
// conhost prints the raw escape bytes instead of acting on them.
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
if (out == INVALID_HANDLE_VALUE)
{
return;
}
DWORD mode = 0;
if (!GetConsoleMode(out, &mode))
{
return;
}
SetConsoleMode(out, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
// POSIX terminals already interpret ANSI; nothing to do.
}
void HideCursor()
{
std::cout << "\x1b[?25l" << std::flush;
}
void ShowCursor()
{
std::cout << "\x1b[?25h" << std::flush;
}
void Clear()
{
std::cout << "\x1b[2J\x1b[H" << std::flush;
}
void MoveHome()
{
std::cout << "\x1b[H" << std::flush;
}
std::string At(int row, int col)
{
return "\x1b[" + std::to_string(row) + ";" + std::to_string(col) + "H";
}
std::string Bar(double frac, int width, char fill, char empty)
{
if (frac < 0.0)
{
frac = 0.0;
}
if (frac > 1.0)
{
frac = 1.0;
}
int filled = static_cast<int>(frac * width + 0.5);
std::string s;
s.reserve(static_cast<std::size_t>(width));
for (int i = 0; i < width; ++i)
{
s.push_back(i < filled ? fill : empty);
}
return s;
}
std::string Fg(unsigned char r, unsigned char g, unsigned char b)
{
return "\x1b[38;2;" + std::to_string(static_cast<int>(r)) + ";"
+ std::to_string(static_cast<int>(g)) + ";"
+ std::to_string(static_cast<int>(b)) + "m";
}
} // namespace console