目录
terminal.hpp
定义终端程序相关信息,包括终端尺寸、终端颜色模式的定义。
struct Dimensions { // 定义终端的外观尺寸
int dimx; // 宽度(从左到右)
int dimy; // 高度(从上到下)
};
namespace Terminal {
Dimensions Size(); // 获取当前终端程序的外观尺寸
void SetFallbackSize(const Dimensions& fallbackSize);//修改外观尺寸的静态存储内容
enum Color { // 定义终端支持的色彩显示模式
Palette1, // 黑白色
Palette16, // 16色
Palette256,// 256色
TrueColor, // 24位彩色
};
Color ColorSupport(); // 获取当前终端程序的色彩模式
void SetColorSupport(Color color); //修改色彩模式的静态存储内容
} // namespace Terminal
color.hpp
Color类
用于存储颜色信息,可以将黑白色模式、16色模式、256色模式、真色彩模式下的颜色统一转换为RGB存储。
class Color {
public:
enum Palette1 : uint8_t;
enum Palette16 : uint8_t;
enum Palette256 : uint8_t;
Color(); // Transparent.
Color(Palette1 index); // 黑白模式参数构造Color对象
Color(Palette16 index); // 16色模式参数构造Color对象
Color(Palette256 index); // 256色模式参数构造Color对象
Color(uint8_t red, uint8_t green, uint8_t blue);//RGB真彩色构造Color对象
static Color RGB(uint8_t red,
uint8_t green,
uint8_t blue);//通过RGB构造Color对象
static Color HSV(uint8_t hue,
uint8_t saturation,
uint8_t value);//通过HSV值构造Color对象
static Color Interpolate(float t,
const Color& a,
const Color& b);//通过两个Color颜色插值构造Color对象
// 黑白模式颜色定义
enum Palette1 : uint8_t{
Default, // Transparent
};
// 16色模式颜色定义
enum Palette16 : uint8_t {
Black = 0,
Red = 1,
...
White = 15,
};
// 256色模式颜色定义
enum Palette256 : uint8_t {
Aquamarine1 = 122,
Aquamarine1Bis = 86,
...
Yellow4Bis = 106,
};
// --- Operators ------
bool operator==(const Color& rhs) const;
bool operator!=(const Color& rhs) const;
std::string Print(bool is_background_color) const;//将颜色信息打印为字符串形式
private:
enum class ColorType : uint8_t {
Palette1,
Palette16,
Palette256,
TrueColor,
};
ColorType type_ = ColorType::Palette1;
uint8_t red_ = 0;
uint8_t green_ = 0;
uint8_t blue_ = 0;
};
inline namespace literals {
// 将 0x808000_rgb 形式的函数调用转为Color对象
// 例如: Color color = 0xff00ff_rgb;
Color operator""_rgb(unsigned long long int combined);
} // namespace literals
color_info.hpp
ColorInfo类
用于存储256色、16色模式下颜色对应的真彩色。
struct ColorInfo { // 定义颜色描述信息
const char* name; // 颜色名称
uint8_t index_256; // 256色模式的颜色索引
uint8_t index_16; // 16色模式的颜色索引
uint8_t red; // RGB的R分量
uint8_t green; // RGB的G分量
uint8_t blue; // RGB的B分量
uint8_t hue; // HSV的H值
uint8_t saturation;// HSV的S值
uint8_t value; // HSV的V值
};
ColorInfo GetColorInfo(Color::Palette256 index);//根据256色模式的颜色索引获取颜色描述信息
ColorInfo GetColorInfo(Color::Palette16 index);//根据16色模式的颜色索引获取颜色描述信息
screen.hpp
Pixel类
表示一个终端界面中的最小显示单元,每个最小显示单元的内容为字符串。
struct Pixel {
bool operator==(const Pixel& other) const;
std::string character = " "; // 用于存储要显示的字符信息。
Color background_color = Color::Default; // 用于存储显示的背景色信息。
Color foreground_color = Color::Default; // 用于存储显示的前景色信息。
bool blink : 1; // 表示是否闪烁显示。(已废弃)
bool bold : 1; // 表示是否加粗显示。
bool dim : 1; // 表示是否暗淡显示。
bool inverted : 1; // 表示是否前景、背景颜色翻转显示。
bool underlined : 1; // 表示是否下划线显示。
bool automerge : 1; // 表示是否自动合并多个字符显示。
Pixel()
: blink(false),
bold(false),
dim(false),
inverted(false),
underlined(false),
automerge(false) {}
};
Screen类
表示一个屏幕绘图区域,其内部维护一个存储Pixel类型的二维数组,用于存储屏幕的所有显示单元,内部使用Cursor类存储光标在屏幕绘图区的位置信息,使用Box类存储屏幕绘图区的边界。
class Screen {
public:
// 构造一个屏幕绘制区域(x值表示从左到右的坐标,y值表示从上到下的坐标)
Screen(int dimx, int dimy);
// 提供静态方法用来创建Screen对象
static Screen Create(Dimensions dimension);
static Screen Create(Dimensions width, Dimensions height);
std::string& at(int x, int y); // 获取(x,y)坐标的字符存储信息
Pixel& PixelAt(int x, int y); // 获取(x,y)坐标的像素单元信息
std::string ToString(); // 将屏幕绘制区域的所有信息转为可打印的字符串
void Print(); // 将屏幕绘制区域的所有信息打印出来
// Get screen dimensions.
int dimx() const { return dimx_; } // 返回屏幕绘制区域的x坐标长度
int dimy() const { return dimy_; } // 返回屏幕绘制区域的y坐标长度
// Move the terminal cursor n-lines up with n = dimy().
std::string ResetPosition(bool clear = false) const;//重置光标到第n行(clear是否清屏)
void Clear(); // 清空显示区域(使用空格填充显示区域)
void ApplyShader();
struct Cursor { // 光标信息结构体定义
int x = 0; // 光标所在x坐标位置
int y = 0; // 光标所在y坐标位置
enum Shape { // 定义光标的形状
Hidden = 0, // 隐藏
BlockBlinking = 1, // 加粗闪烁
Block = 2, // 加粗
UnderlineBlinking = 3, // 下划线闪烁
Underline = 4, // 下划线
Bar = 5, // 条形
BarBlinking = 6, // 条形闪烁
};
Shape shape;//存储光标形状信息
};
Cursor cursor() const { return cursor_; } // 返回当前屏幕光标信息
void SetCursor(Cursor cursor) { cursor_ = cursor; } // 设置当前屏幕光标信息
Box stencil; // 记录屏幕显示区域边界
protected:
int dimx_; // 屏幕区域宽度(从左到右)
int dimy_; // 屏幕区域高度(从上到下)
std::vector<std::vector<Pixel>> pixels_; // 屏幕区域像素信息
Cursor cursor_; // 屏幕光标信息
};
namespace Dimension {
Dimensions Fixed(int); // Fixed 返回(int,int)大小的终端外观尺寸
Dimensions Full(); // Full返回当前终端程序的外观尺寸
}
// 定义屏幕绘制区域的边界信息
struct Box {
int x_min = 0; // Box的最左边界坐标
int x_max = 0; // Box的最右边界坐标
int y_min = 0; // Box的最上边界坐标
int y_max = 0; // Box的最下边界坐标
static auto Intersection(Box a, Box b) -> Box; // 返回两个Box区域的交集部分形成的Box
static auto Union(Box a, Box b) -> Box; // 返回两个Box区域的并集部分形成的Box
bool Contain(int x, int y) const; // 判断(x,y)坐标是否在Box内
bool operator==(const Box& other) const;
bool operator!=(const Box& other) const;
};