表格线识别通用库文档
载入中...
搜索中...
未找到
printer.hpp
浏览该文件的文档.
1/*
2 * @Description: 打印器类 头文件及其实现
3 * @Version:
4 * @Autor: dreamy-xay
5 * @date: 2024-01-23
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-04
8 */
9
10#ifndef COMMON_PRINTER_HPP
11#define COMMON_PRINTER_HPP
12
13#include <cstring>
14#include <iostream>
15#include <regex>
16
18#include "common/enum.h"
19#include "common/macro.h"
20#include "common/type.h"
22
23namespace cm {
24
32class Printer {
34 protected:
35 std::string default_split_string; // 默认打印分隔字符串
36 std::string output_color; // 默认输出内容的颜色
37 std::string split_string; // 临时打印分隔字符串
38 bool use_temp_split_string; // 是否使用临时分隔字符串
39 bool is_cancel_color; // 打印时是否撤销了颜色
40 bool is_first_print; // 是否第一次打印
41
42 std::string output_color_cache; // 默认输出内容的颜色缓存
44 public:
45 Printer(const std::string& default_split_string = " ", const std::string& output_color = Color::BLUE);
46 ~Printer();
47
48 template <typename T>
49 static bool IsMatchString(T arg, const char* match_string, bool is_regex = false);
50 static bool IsMatchString(const char* arg, const char* match_string, bool is_regex = false);
51
52 template <typename T>
54 template <typename T, typename... ARGS>
55 Printer& Print(T arg, ARGS... args);
56
57 template <typename... ARGS>
58 std::ostream& operator()(ARGS... args);
59
60 Printer& SetOutputColor(const std::string& output_color);
61 Printer& SetSplitString(const std::string& split_string);
62
63 private:
64 template <typename T>
65 void PrintArg(T arg, bool is_last_arg = false);
66
67 virtual std::string GetPrintPrefix(bool is_last_arg);
68
69 template <typename T>
70 static std::string ToString(T arg);
71 static std::string ToString(const char* arg);
72};
73
85
94
114template <typename T>
115inline bool Printer::IsMatchString(T arg, const char* match_string, bool is_regex) {
116 return false;
117}
118
157inline bool Printer::IsMatchString(const char* arg, const char* match_string, bool is_regex) {
158 if (is_regex)
159 return std::regex_match(arg, std::regex(match_string));
160 else
161 return std::strcmp(arg, match_string) == 0;
162}
163
185template <typename T>
187 // 是否特殊字符串
188 bool is_special_string = Printer::IsMatchString(arg, "__NO_COLOR__") || Printer::IsMatchString(arg, "__SPLIT=.*__", true);
189
190 // 如果不是特殊字符串则打印参数
192 this->PrintArg(arg, true);
193
194 // 如果取消了颜色打印则恢复颜色打印
195 if (is_cancel_color) {
196 is_cancel_color = false;
197 output_color = output_color_cache; // 恢复缓存颜色
199 }
200
201 // 如果使用了临时分隔字符串,则清除
203 use_temp_split_string = false;
204 split_string.clear();
205 }
206
207 is_first_print = true; // 重载是否第一次打印变量,保证下一次打印重头开始
208
209 return *this;
210}
211
249template <typename T, typename... ARGS>
251 // 如果需要取消打印颜色,则取消,并且不打印参数
252 if (Printer::IsMatchString(arg, "__NO_COLOR__")) {
253 is_cancel_color = true;
254 output_color_cache = output_color; // 缓存原始输出颜色
255 output_color = "";
257 } else if (Printer::IsMatchString(arg, "__SPLIT=.*__", true)) { // 设置临时分隔字符串
259 // 更新分隔字符串
260 std::string arg_str = Printer::ToString(arg);
261 std::size_t start = arg_str.find_first_of("=") + 1;
262 std::size_t end = arg_str.find_last_of("__") - 1;
263 split_string = arg_str.substr(start, end - start);
264 } else // 否则打印参数
265 this->PrintArg(arg);
266
267 // 递归调用不定参数打印
268 this->Print(args...);
269
270 return *this;
271}
272
308template <typename... ARGS>
309inline std::ostream& Printer::operator()(ARGS... args) {
310 this->Print(args...);
311
312 return std::cout;
313}
314
364inline Printer& Printer::SetOutputColor(const std::string& output_color) {
365 this->output_color = output_color;
366
367 return *this;
368}
369
410inline Printer& Printer::SetSplitString(const std::string& split_string) {
412
413 return *this;
414}
415
432template <typename T>
433inline void Printer::PrintArg(T arg, bool is_last_arg) {
434 if (is_first_print) // 第一次打印不输出分隔符
435 is_first_print = false;
436 else
438
439 std::cout << this->GetPrintPrefix(is_last_arg) << output_color << arg << Color::RESET; // 打印参数值
440}
441
453inline std::string Printer::GetPrintPrefix(bool is_last_arg) {
454 return "";
455}
456
470template <typename T>
471inline std::string Printer::ToString(T arg) {
472 return "";
473}
474
486inline std::string Printer::ToString(const char* arg) {
487 return arg;
488}
489
490} // namespace cm
491
492#endif
static void RestoreColor()
恢复默认颜色设置
Definition color.hpp:144
static std::string RESET
结束符,重置所有颜色设置
Definition color.hpp:48
static std::string BLUE
蓝色
Definition color.hpp:34
static void CancelColor()
取消所有颜色
Definition color.hpp:133
点类
Definition point.hpp:52
打印器类
Definition printer.hpp:32
Printer(const std::string &default_split_string=" ", const std::string &output_color=Color::BLUE)
打印器类的带参构造函数
Definition printer.hpp:84
std::ostream & operator()(ARGS... args)
实现不定参数的打印
Definition printer.hpp:309
static bool IsMatchString(T arg, const char *match_string, bool is_regex=false)
判断泛型参数是否是字符串参数
Definition printer.hpp:115
Printer & Print(T arg)
实现单个参数的打印
Definition printer.hpp:186
Printer & SetSplitString(const std::string &split_string)
设置默认输出内容的分隔字符串
Definition printer.hpp:410
~Printer()
打印器类的析构函数
Definition printer.hpp:93
Printer & SetOutputColor(const std::string &output_color)
设置默认输出内容的颜色
Definition printer.hpp:364