Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
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
104private:
105 static std::shared_ptr<IFormatter>& formatter() {
106 static std::shared_ptr<IFormatter> f =
107 std::make_shared<ConsoleFormatter>();
108 return f;
109 }
110
111 static Format& current_format() {
112 static Format fmt = Format::TEXT;
113 return fmt;
114 }
115};
116
122DEFINE_COLOR_HELPER(MAGENTA)
126DEFINE_COLOR_HELPER(UNDERLINE)
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 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
Definition function.hpp:22
COLOR
Enumeration of available colors for formatted messages.
Definition utils.hpp:111