-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.cpp
More file actions
66 lines (57 loc) · 1.71 KB
/
terminal.cpp
File metadata and controls
66 lines (57 loc) · 1.71 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
#include "terminal.h"
// 用于保护 std::cout 的互斥锁
std::mutex cout_mtx;
// 设置控制台颜色的函数,使用256色模式
void terminal::setColor(Color color, bool isForeground)
{
// 使用互斥锁保护 cout,防止多线程同时操作导致的问题
std::lock_guard<std::mutex> lock(cout_mtx);
// 根据是否设置为前景色,选择相应的 ANSI escape code
std::cout << "\033[" << (isForeground ? 38 : 48) << ";5;" << static_cast<int>(color) << 'm';
}
// 设置控制台样式的函数
void terminal::setStyle(Style style)
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << "\033[" << static_cast<int>(style) << 'm';
}
// 设置光标位置的函数
void terminal::setCursor(int row, int col)
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << "\033[" << row << ';' << col << 'H';
}
// 隐藏光标的函数
void terminal::hideCursor()
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << "\033[?25l";
}
// 显示光标的函数
void terminal::showCursor()
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << "\033[?25h";
}
// 清屏的函数
void terminal::clearScreen()
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << "\033[2J";
}
// 重置控制台的函数
void terminal::reset()
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << "\033[0m";
}
// 绘制计时时间
void terminal::drawTick(int64_t minutes, int64_t seconds, int64_t milliseconds)
{
std::lock_guard<std::mutex> lock(cout_mtx);
std::cout << std::setfill('0')
<< std::setw(2) << minutes << ":"
<< std::setw(2) << seconds << ":"
<< std::setw(3) << milliseconds
<< std::flush;
}