1 Star 0 Fork 0

陈祎麒/mlcc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ConsoleControl.h 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
sb500 提交于 2023-04-15 15:04 . 凑合用
#pragma once
#include <cstdio>
#include <map>
#include <string>
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#undef NOMINMAX
#else
#endif
enum ConsoleColor
{
CONSOLE_COLOR_NONE = -1,
CONSOLE_COLOR_RED = 4,
CONSOLE_COLOR_LIGHT_RED = 12,
CONSOLE_COLOR_GREEN = 2,
CONSOLE_COLOR_LIGHT_GREEN = 10,
CONSOLE_COLOR_BLUE = 1,
CONSOLE_COLOR_LIGHT_BLUE = 9,
CONSOLE_COLOR_WHITE = 7,
CONSOLE_COLOR_BLACK = 0,
};
class ConsoleControl
{
private:
ConsoleControl()
{
if (color_map_.empty())
{
color_map_ = {
{ CONSOLE_COLOR_NONE, "\e[0m" },
{ CONSOLE_COLOR_RED, "\e[0;31m" },
{ CONSOLE_COLOR_LIGHT_RED, "\e[1;31m" },
{ CONSOLE_COLOR_GREEN, "\e[0;32m" },
{ CONSOLE_COLOR_LIGHT_GREEN, "\e[1;32m" },
{ CONSOLE_COLOR_BLUE, "\e[0;34m" },
{ CONSOLE_COLOR_LIGHT_BLUE, "\e[1;34m" },
{ CONSOLE_COLOR_WHITE, "\e[1;37m" },
{ CONSOLE_COLOR_BLACK, "\e[0;30m" },
};
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
old_color_ = csbiInfo.wAttributes;
#endif
}
}
unsigned short old_color_;
std::map<int, std::string> color_map_;
static ConsoleControl* getInstance()
{
static ConsoleControl console_control;
return &console_control;
}
private:
ConsoleControl(ConsoleControl&) = delete;
ConsoleControl& operator=(ConsoleControl&) = delete;
public:
static void setColor(ConsoleColor c)
{
setColor(static_cast<int>(c));
}
static void setColor(int c)
{
auto cc = getInstance();
#ifdef _MSC_VER
if (c != CONSOLE_COLOR_NONE)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), cc->old_color_);
}
#else
fprintf(stderr, "%s", cc->color_map_[c].c_str());
#endif
}
static void resetColor()
{
setColor(CONSOLE_COLOR_NONE);
}
static void moveUp(int l = 1)
{
#ifdef _MSC_VER
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
info.dwCursorPosition.Y -= l;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), info.dwCursorPosition);
#else
if (l > 0)
{
fprintf(stderr, "\e[%dA", l);
}
else if (l < 0)
{
for (int i = 0; i < -l; i++) { fprintf(stderr, "\n"); }
}
#endif
}
static void moveDown(int l = 1)
{
moveUp(-l);
}
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chen-yiqi-duke/mlcc.git
git@gitee.com:chen-yiqi-duke/mlcc.git
chen-yiqi-duke
mlcc
mlcc
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385