ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
format_manager.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#define DEFINE_COLOR_HELPER(name) \
7inline std::string name(const std::string& text) { \
8 auto fmt = FormatterManager::get_formatter(); \
9 return fmt->colorize(text, Utils::name); \
10}
11
12// Manager global
14public:
15 static void set_formatter(std::shared_ptr<IFormatter> f) {
16 formatter() = std::move(f);
17 }
18
19 static std::shared_ptr<IFormatter> get_formatter() {
20 auto f = formatter();
21 if (!f) {
22 static auto fallback =
23 std::make_shared<ConsoleFormatter>();
24 return fallback;
25 }
26 return f;
27 }
28
29 static void set_format(Format f) {
30 current_format() = f;
31 }
32
33 static Format get_format() {
34 return current_format();
35 }
36
37 template <typename T>
38 static void write(
39 const std::string& template_str,
40 const std::vector<T>& data,
41 enum Format effective = Format::AUTO,
42 RowColorFn color_fn = nullptr,
43 std::ostream& out = std::cout
44 ) {
45 if (effective == Format::AUTO) {
46 effective = current_format();
47 }
48
49 if (effective != current_format()) {
50 return;
51 }
52
53 json arr = json::array();
54 if (effective == Format::JSON) {
55 for (const auto& x : data) arr.push_back(x);
56 out << arr.dump(2) << "\n";
57 return;
58 }
59
60 size_t i = 0;
61 auto formatter = get_formatter();
62 for (const auto& item : data) {
63 std::string line = TemplateRenderer::render(template_str, item, formatter);
64
65 if (color_fn && formatter) {
66 line = formatter->colorize(line, color_fn(i));
67 }
68
69 out << line << "\n";
70 ++i;
71 }
72 if (data.size() <= 0) {
73 out << template_str << "\n";
74 }
75 }
76
77 template <typename T>
78 static void write(
79 const std::string& template_str,
80 const T& item,
81 Format effective = Format::AUTO,
82 RowColorFn color_fn = nullptr,
83 std::ostream& out = std::cout
84 ) {
85 write<T>(template_str, std::vector<T>{item}, effective, color_fn, out);
86 }
87
88 static void write(const std::string& str, std::ostream& out = std::cout) {
89 write(str, std::vector<char>{}, Format::TEXT, nullptr, out);
90 }
91
92 static void warn(const std::string& str) {
93 auto fmt = get_formatter();
94
95 Utils::COLOR bold = fmt->style().at("bold");
96 Utils::COLOR warn_color = fmt->style().at("warning");
97
98 std::string warning = fmt->colorize("Warning: ", bold);
99 warning = fmt->colorize(warning, warn_color);
100
101 write(warning + str, std::cerr);
102 }
103
104 static void time(const std::string& str, std::chrono::high_resolution_clock::time_point start, std::ostream& out = std::cout) {
105 auto end = std::chrono::high_resolution_clock::now();
106
107 std::chrono::duration<double, std::milli> duration = end - start;
108 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
109
110 std::cout << str << " "
111 << std::setfill('0') << std::setw(2) << (ms / 3600000) << ":"
112 << std::setw(2) << (ms / 60000 % 60) << ":"
113 << std::setw(2) << (ms / 1000 % 60) << "."
114 << std::setw(3) << (ms % 1000)
115 << "\n";
116 }
117
118private:
119 static std::shared_ptr<IFormatter>& formatter() {
120 static std::shared_ptr<IFormatter> f =
121 std::make_shared<ConsoleFormatter>();
122 return f;
123 }
124
125 static Format& current_format() {
126 static Format fmt = Format::TEXT;
127 return fmt;
128 }
129};
130
Format
Definition: base.hpp:9
std::function< Utils::COLOR(size_t)> RowColorFn
Definition: base.hpp:16
static void write(const std::string &template_str, const T &item, Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
static void write(const std::string &template_str, const std::vector< T > &data, enum Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
static void set_formatter(std::shared_ptr< IFormatter > f)
static std::shared_ptr< IFormatter > get_formatter()
static Format get_format()
static void warn(const std::string &str)
static void write(const std::string &str, std::ostream &out=std::cout)
static void time(const std::string &str, std::chrono::high_resolution_clock::time_point start, std::ostream &out=std::cout)
static void set_format(Format f)
static std::string render(const std::string &tpl, const json &ctx, std::shared_ptr< IFormatter > formatter)
#define DEFINE_COLOR_HELPER(name)
nlohmann::json json
COLOR
Enumeration of available colors for formatted messages.
Definition: utils.hpp:111
@ BOLD
Bold text.
Definition: utils.hpp:123
@ MAGENTA
Magenta color.
Definition: utils.hpp:117
@ GRAY
Gray color.
Definition: utils.hpp:119
@ WHITE
White color.
Definition: utils.hpp:120
@ RED
Red color.
Definition: utils.hpp:113
@ GREEN
Green color.
Definition: utils.hpp:114
@ YELLOW
Yellow color.
Definition: utils.hpp:115
@ UNDERLINE
Underline text.
Definition: utils.hpp:124
@ BLUE
Blue color.
Definition: utils.hpp:116
@ CYAN
Cyan color.
Definition: utils.hpp:118