Plaquette
 
Loading...
Searching...
No Matches
PlotterFormat.h
1/*
2 * PlotterFormat.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2025 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#ifndef PQ_PLOTTER_FORMAT_H_
21#define PQ_PLOTTER_FORMAT_H_
22
23#include "PqCore.h"
24
25namespace pq {
26
34struct LabelView {
35 const char* ptr = nullptr;
36 uint16_t len = 0;
37
39 bool empty() const { return (ptr == nullptr || len == 0); }
40};
41
63 // Row framing (shared by values and header, if any).
64 const char* plotBegin = "";
65 const char* plotEnd = "";
66 const char* rowBegin = "";
67 const char* rowEnd = "\r\n";
68 const char* separator = ",";
69
70 // Templates:
71 // - valueTemplate is expanded for each value in normal rows.
72 // - keyTemplate is expanded for each key in the header row (when enabled).
73 const char* valueTemplate = "$v";
74 const char* keyTemplate = "$k";
75
76 // If true, Plotter may emit a header row (typically only when labels exist).
77 bool headerEnabled = false;
78
79 // If true, prints separator after every element (including the last).
80 // If false, prints separator only between elements.
81 bool trailingSeparator = false;
82 bool trailingRowEnd = true;
83
84 // Fallback string used when $k is requested but no label exists for this index
85 // (labels missing entirely, labels shorter than value count, empty token, etc.).
86 // If nullptr or empty, $k prints nothing in those cases.
87 const char* keyFallback = "value_$i";
88
89 // ---- Convenience helpers used by Plotter (short and inline) ----
90
91 static void printString(Print& out, const char* str) {
92 if (str && *str) out.print(str);
93 }
94
95 void beginPlot(Print& out) const { printString(out, plotBegin); }
96 void endPlot(Print& out) const {
97 printString(out, plotEnd);
98 }
99 void beginRow(Print& out) const { printString(out, rowBegin); }
100 void endRow(Print& out) const {
101 printString(out, rowEnd);
102 }
103 void sep(Print& out) const { printString(out, separator); }
104
115 void printValueElement(Print& out,
116 uint16_t index,
117 LabelView label,
118 float value,
119 uint8_t digits,
120 bool isFirst) const;
121
128 void printKeyElement(Print& out,
129 uint16_t index,
130 LabelView label,
131 uint8_t digits,
132 bool isFirst) const;
133
134private:
135 // Core template expansion: implemented in .cpp to keep the header light.
136 static void _writeTemplate(Print& out,
137 const char* templ,
138 uint16_t index,
139 LabelView label,
140 float value,
141 uint8_t digits,
142 const char* keyFallback);
143
144 static void _writeKey(Print& out,
145 LabelView label,
146 uint16_t index,
147 const char* keyFallback);
148};
149
150} // namespace pq
151
152#endif // PQ_PLOTTER_FORMAT_H_
Lightweight non-owning view of a label (no allocation).
Definition PlotterFormat.h:34
bool empty() const
True if no label is available.
Definition PlotterFormat.h:39
Formatting specification used by Plotter to render values and (optionally) a header.
Definition PlotterFormat.h:62
void printValueElement(Print &out, uint16_t index, LabelView label, float value, uint8_t digits, bool isFirst) const
Print one element for a normal (value) row.
Definition PlotterFormat.cpp:24
void printKeyElement(Print &out, uint16_t index, LabelView label, uint8_t digits, bool isFirst) const
Print one element for a header (key) row.
Definition PlotterFormat.cpp:40