ASCO Aerial Autonomy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
html_utils.h
Go to the documentation of this file.
1 #pragma once
2 #include <iomanip> // precision
3 #include <sstream>
4 #include <stdexcept>
5 #include <string>
6 
10 struct Colors {
14  static constexpr const char *red = "\"#DC143C\"";
18  static constexpr const char *green = "\"#7FFF00\"";
22  static constexpr const char *blue = "\"#4169E1\"";
26  static constexpr const char *grey = "\"#D3D3D3\"";
30  static constexpr const char *yellow = "\"#FFFF94\"";
34  static constexpr const char *white = "\"#FFF\"";
38  static constexpr const char *black = "\"#000\"";
39 };
40 
45 public:
50  division_string_stream << std::fixed << std::setprecision(2);
51  division_string_stream << "<div>";
52  }
60  void addHeader(std::string header, int level = 1) {
61  std::string h_name = "h" + std::to_string(level);
62  division_string_stream << "<" << h_name << " align=\"center\" >" << header
63  << "</" << h_name << ">";
64  }
65 
71  void addText(std::string text) {
72  division_string_stream << text << std::endl;
73  }
74 
80  std::string getDivisionText() {
81  return division_string_stream.str() + "</div>";
82  }
83 
84 private:
85  std::stringstream division_string_stream;
86 };
87 
92 public:
97  HtmlTableWriter(int width = 120) : width_(width), row_ended(true) {
98  table_string_stream << std::fixed << std::setprecision(2);
99  table_string_stream << "<table border=\"1\">";
100  }
101 
115  template <typename DataT>
116  void addCell(const DataT &data, std::string header = "",
117  std::string bg_color = Colors::white, int colspan = 1) {
118  if (row_ended) {
119  throw std::runtime_error("Cannot add cell without beginning row");
120  }
121  std::string width_text = "\"" + std::to_string(colspan * width_) + "\"";
122  table_string_stream << "<td width=" << width_text << " colspan=\""
123  << std::to_string(colspan) << "\" bgcolor=" << bg_color
124  << " >";
125  if (!header.empty()) {
126  table_string_stream << header << ": ";
127  }
128  table_string_stream << data;
129  table_string_stream << "</td>";
130  }
131 
135  void beginRow() {
136  if (!row_ended) {
137  endRow();
138  }
139  table_string_stream << "<tr>";
140  row_ended = false;
141  }
142 
152  void addHeader(std::string header, std::string text_color = Colors::black,
153  int colspan = 1) {
154  if (row_ended) {
155  throw std::runtime_error("Cannot add cell without beginning row");
156  }
157  std::string width_text = "\"" + std::to_string(colspan * width_) + "\"";
158  table_string_stream << "<th width=" << width_text << " colspan=\""
159  << std::to_string(colspan) << "\" >";
160  table_string_stream << "<font color=" << text_color << ">" << header
161  << "</font>";
162  table_string_stream << "</th>";
163  }
164 
170  std::string getTableString() {
171  if (!row_ended) {
172  endRow();
173  }
174  return table_string_stream.str() + "</table>";
175  }
176 
177 private:
178  std::stringstream table_string_stream;
179  int width_;
180  bool row_ended;
181 
185  void endRow() {
186  row_ended = true;
187  table_string_stream << "</tr>";
188  }
189 };
static constexpr const char * white
White color hex code.
Definition: html_utils.h:34
static constexpr const char * green
Green color hex code.
Definition: html_utils.h:18
HtmlDivisionWriter()
Constructor.
Definition: html_utils.h:49
std::string getTableString()
Get the html table in string format.
Definition: html_utils.h:170
void addText(std::string text)
Add text to the division.
Definition: html_utils.h:71
static constexpr const char * grey
Grey color hex code.
Definition: html_utils.h:26
Add a division to html.
Definition: html_utils.h:44
static constexpr const char * blue
Blue color hex code.
Definition: html_utils.h:22
void addCell(const DataT &data, std::string header="", std::string bg_color=Colors::white, int colspan=1)
Add a cell to the table. Should be called after beginning a row. Otherwise will throw an exception...
Definition: html_utils.h:116
void addHeader(std::string header, int level=1)
Add a header inside division.
Definition: html_utils.h:60
void beginRow()
Begin a new row in the table.
Definition: html_utils.h:135
Helper class that defines colors in hex format.
Definition: html_utils.h:10
std::string getDivisionText()
Create a divsion html in string format.
Definition: html_utils.h:80
void addHeader(std::string header, std::string text_color=Colors::black, int colspan=1)
Add a table header. Should be called after beginning a row.
Definition: html_utils.h:152
static constexpr const char * yellow
Yellow color hex code.
Definition: html_utils.h:30
HtmlTableWriter(int width=120)
constructor initializes the table header
Definition: html_utils.h:97
Helper class to write html tables to text format.
Definition: html_utils.h:91
static constexpr const char * black
Black color hex code.
Definition: html_utils.h:38
static constexpr const char * red
Red color hex code.
Definition: html_utils.h:14