JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
CursesHandler.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 20/08/2019.
3 //
4 
5 #ifndef JODA_CURSESHANDLER_H
6 #define JODA_CURSESHANDLER_H
7 
8 #include <joda/misc/RJFwd.h>
9 #include <rapidjson/rapidjson.h>
10 
11 #include <codecvt>
12 #include <cstdint>
13 #include <locale>
14 #include <vector>
15 
16 #include "CursesFWD.h"
17 
19  typedef RJChar::Ch Ch;
20 
21  public:
22  CursesPrettyHandler(WINDOW *win, size_t idention = jDefaultIdention)
23  : identionSize(idention), win(win), colors(has_colors() == TRUE) {}
24 
25  bool Null() {
26  writePrefix();
27  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NULL));
28  writeWString("null");
29  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NULL));
30  return true;
31  }
32 
33  bool Bool(bool b) {
34  writePrefix();
35  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_BOOL));
36  if (b)
37  writeWString("true");
38  else
39  writeWString("false");
40  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_BOOL));
41  return true;
42  }
43 
44  bool Int(int i) {
45  writePrefix();
46  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
47  writeWString(std::to_string(i));
48  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
49  return true;
50  }
51 
52  bool Uint(unsigned i) {
53  writePrefix();
54  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
55  writeWString(std::to_string(i));
56  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
57  return true;
58  }
59 
60  bool Int64(int64_t i) {
61  writePrefix();
62  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
63  writeWString(std::to_string(i));
64  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
65  return true;
66  }
67 
68  bool Uint64(uint64_t i) {
69  writePrefix();
70  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
71  writeWString(std::to_string(i));
72  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
73  return true;
74  }
75 
76  bool Double(double d) {
77  writePrefix();
78  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
79  writeWString(std::to_string(d));
80  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
81  return true;
82  }
83 
84  bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy) {
85  writePrefix();
86  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
87  auto basicString = std::string(str, length);
88  writeWString(basicString);
89  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_NUMBER));
90  return true;
91  }
92 
93  bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
94  writePrefix();
95  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_STRING));
96  auto basicString = std::string(str, length);
97  basicString = "\"" + basicString + "\"";
98  writeWString(basicString);
99  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_STRING));
100  return true;
101  }
102 
103  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
104  writePrefix();
105  if (colors) wattron(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_KEY));
106  auto basicString = std::string(str, length);
107  basicString = "\"" + basicString + "\"";
108  writeWString(basicString);
109  if (colors) wattroff(win, COLOR_PAIR(JODA_JSON_OUTPUT_COLOR_KEY));
110  return true;
111  }
112 
113  bool StartObject() {
114  writePrefix();
115  writeWString("{");
116  levels.emplace_back(false);
117  return true;
118  }
119 
120  bool EndObject(rapidjson::SizeType memberCount) {
121  auto empty = levels.back().valueCount == 0;
122  levels.pop_back();
123  if (!empty) {
124  writeWString("\n");
125  writeIndent();
126  }
127  writeWString("}");
128  return true;
129  }
130 
131  bool StartArray() {
132  writePrefix();
133  writeWString("[");
134  levels.emplace_back(true);
135  return true;
136  }
137 
138  bool EndArray(rapidjson::SizeType elementCount) {
139  auto empty = levels.back().valueCount == 0;
140  levels.pop_back();
141  if (!empty) {
142  writeWString("\n");
143  writeIndent();
144  }
145  writeWString("]");
146  return true;
147  }
148 
149  private:
150  void writeIndent() {
151  std::string str;
152  str.insert(0, identionSize * levels.size(), ' ');
153  writeWString(str);
154  }
155 
156  void writePrefix() {
157  if (!levels.empty()) { // this value is not at root
158  auto &level = levels.back();
159 
160  if (level.inArray) {
161  if (level.valueCount > 0) {
162  writeWString(
163  ","); // add comma if it is not the first element in array
164  }
165  writeWString("\n");
166  writeIndent();
167  } else { // in object
168  if (level.valueCount > 0) {
169  if (level.valueCount % 2 == 0) {
170  writeWString(",\n");
171  } else {
172  writeWString(": ");
173  }
174  } else
175  writeWString("\n");
176 
177  if (level.valueCount % 2 == 0) writeIndent();
178  }
179  level.valueCount++;
180  }
181  }
182 
183  void writeWString(const std::string &str) {
184  std::wstring_convert<std::codecvt_utf8<wchar_t>> cv;
185 
186  std::wstring wstr = cv.from_bytes(str);
187  waddwstr(win, wstr.c_str());
188  }
189 
190  size_t identionSize;
191  WINDOW *win;
192  bool colors;
193 
194  struct Level {
195  Level(bool inArray_) : valueCount(0), inArray(inArray_) {}
196 
197  size_t valueCount;
198  bool inArray;
199  };
200 
201  std::vector<Level> levels;
202 
203  static constexpr size_t jDefaultIdention = 4;
204 };
205 
206 #endif // JODA_CURSESHANDLER_H
Definition: CursesHandler.h:18
bool Bool(bool b)
Definition: CursesHandler.h:33
bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CursesHandler.h:84
bool StartArray()
Definition: CursesHandler.h:131
bool Double(double d)
Definition: CursesHandler.h:76
bool Null()
Definition: CursesHandler.h:25
bool Int(int i)
Definition: CursesHandler.h:44
bool StartObject()
Definition: CursesHandler.h:113
bool Int64(int64_t i)
Definition: CursesHandler.h:60
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CursesHandler.h:103
bool Uint(unsigned i)
Definition: CursesHandler.h:52
bool EndArray(rapidjson::SizeType elementCount)
Definition: CursesHandler.h:138
CursesPrettyHandler(WINDOW *win, size_t idention=jDefaultIdention)
Definition: CursesHandler.h:22
bool Uint64(uint64_t i)
Definition: CursesHandler.h:68
bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CursesHandler.h:93
bool EndObject(rapidjson::SizeType memberCount)
Definition: CursesHandler.h:120
#define JODA_JSON_OUTPUT_COLOR_NUMBER
Definition: CursesFWD.h:8
#define JODA_JSON_OUTPUT_COLOR_BOOL
Definition: CursesFWD.h:10
#define JODA_JSON_OUTPUT_COLOR_STRING
Definition: CursesFWD.h:9
#define JODA_JSON_OUTPUT_COLOR_KEY
Definition: CursesFWD.h:12
#define JODA_JSON_OUTPUT_COLOR_NULL
Definition: CursesFWD.h:11