Skip to content

Commit 34877de

Browse files
committed
Add ansi_colour.h and tidy up headers
Add new common/include/ansi_colour.h providing ansi::Colours helpers for foreground/background ANSI escape sequences. Apply consistent formatting and small API-preserving cleanups across multiple headers (namespace/bracing style, whitespace, line breaks) and update copyright years to 2025-2026. Affected files: common/include/common/utils/utils_array.h, common/include/firmware/pixeldmx/show.h, and several json headers (json_format_helpers.h, json_helpers.h, json_jsondoc.h, json_key.h, json_params_base.h, json_parsehelper.h, json_parser.h, json_tokenizer.h). Changes are stylistic or minor refactors only; no functional behavior changes intended.
1 parent a80480e commit 34877de

15 files changed

Lines changed: 313 additions & 487 deletions

File tree

common/include/ansi_colour.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @file ansi_colour.h
3+
*
4+
*/
5+
/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
#ifndef ANSI_COLOUR_H_
27+
#define ANSI_COLOUR_H_
28+
29+
namespace ansi {
30+
// https://github.com/shiena/ansicolor/blob/master/README.md
31+
struct Colours {
32+
enum class Colour {
33+
kBlack,
34+
kRed,
35+
kGreen,
36+
kYellow,
37+
kBlue,
38+
kMagenta,
39+
kCyan,
40+
kWhite,
41+
kDefault
42+
};
43+
44+
struct Fg {
45+
static constexpr char kBlack[] = "\x1b[30m";
46+
static constexpr char kRed[] = "\x1b[31m";
47+
static constexpr char kGreen[] = "\x1b[32m";
48+
static constexpr char kYellow[] = "\x1b[33m";
49+
static constexpr char kWhite[] = "\x1b[37m";
50+
static constexpr char kDefault[] = "\x1b[39m";
51+
};
52+
53+
static constexpr const char* Foreground(Colour colour) {
54+
switch (colour) {
55+
case Colour::kBlack:
56+
return ansi::Colours::Fg::kBlack;
57+
break;
58+
case Colour::kRed:
59+
return ansi::Colours::Fg::kRed;
60+
break;
61+
case Colour::kGreen:
62+
return ansi::Colours::Fg::kGreen;
63+
break;
64+
case Colour::kYellow:
65+
return ansi::Colours::Fg::kYellow;
66+
break;
67+
case Colour::kWhite:
68+
return ansi::Colours::Fg::kWhite;
69+
break;
70+
default:
71+
return ansi::Colours::Fg::kDefault;
72+
break;
73+
}
74+
};
75+
76+
struct Bg {
77+
static constexpr char kBlack[] = "\x1b[40m";
78+
static constexpr char kRed[] = "\x1b[41m";
79+
static constexpr char kGreen[] = "\x1b[42m";
80+
static constexpr char kYellow[] = "\x1b[43m";
81+
static constexpr char kWhite[] = "\x1b[47m";
82+
static constexpr char kDefault[] = "\x1b[49m";
83+
};
84+
85+
static constexpr const char* Background(Colour colour) {
86+
switch (colour) {
87+
case Colour::kBlack:
88+
return ansi::Colours::Bg::kBlack;
89+
break;
90+
case Colour::kRed:
91+
return ansi::Colours::Bg::kRed;
92+
break;
93+
case Colour::kGreen:
94+
return ansi::Colours::Bg::kGreen;
95+
break;
96+
case Colour::kYellow:
97+
return ansi::Colours::Bg::kYellow;
98+
break;
99+
case Colour::kWhite:
100+
return ansi::Colours::Bg::kWhite;
101+
break;
102+
default:
103+
return ansi::Colours::Bg::kDefault;
104+
break;
105+
}
106+
};
107+
};
108+
} // namespace ansi
109+
110+
#endif // ANSI_COLOUR_H_

common/include/common/utils/utils_array.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file utils_array.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,12 +28,11 @@
2828

2929
#include <cstddef>
3030

31-
namespace common
32-
{
33-
template <typename T, size_t N> constexpr size_t ArraySize(const T (&)[N]) noexcept
34-
{
31+
namespace common {
32+
template <typename T, size_t N>
33+
constexpr size_t ArraySize(const T (&)[N]) noexcept {
3534
return N;
3635
}
3736
} // namespace common
3837

39-
#endif // COMMON_UTILS_UTILS_ARRAY_H_
38+
#endif // COMMON_UTILS_UTILS_ARRAY_H_

common/include/firmware/pixeldmx/show.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,20 @@
3232
#include "display.h"
3333
#include "pixelpatterns.h"
3434

35-
namespace common::firmware::pixeldmx
36-
{
37-
inline void Show(uint32_t line, pixelpatterns::Pattern pattern = pixelpatterns::Pattern::kNone)
38-
{
35+
namespace common::firmware::pixeldmx {
36+
inline void Show(uint32_t line, pixelpatterns::Pattern pattern = pixelpatterns::Pattern::kNone) {
3937
auto& configuration = PixelDmxConfiguration::Get();
4038
auto* display = Display::Get();
4139
assert(display != nullptr);
4240

4341
display->ClearEndOfLine();
44-
display->Printf(line, "%s:%d G%d %s", pixel::GetTypeName(configuration.GetType()), configuration.GetCount(), configuration.GetGroupingCount(),
45-
pixel::GetMapName(configuration.GetMap()));
42+
display->Printf(line, "%s:%d G%d %s",
43+
pixel::GetTypeName(configuration.GetType()),
44+
configuration.GetCount(), configuration.GetGroupingCount(),
45+
pixel::GetMapName(configuration.GetMap()));
4646
display->ClearLine(8); // Status line
4747

48-
if (pattern != pixelpatterns::Pattern::kNone)
49-
{
48+
if (pattern != pixelpatterns::Pattern::kNone) {
5049
display->ClearLine(6);
5150
display->Printf(6, "%s:%u", PixelPatterns::GetName(pattern), static_cast<uint32_t>(pattern));
5251
}

common/include/json/json_format_helpers.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,27 @@
3030
#include <cstdint>
3131
#include <cassert>
3232

33-
namespace format
34-
{
33+
namespace format {
3534
constexpr size_t kFloatBufferSize = 8; // For "%.2f", "%.1f"
3635
constexpr size_t kOffsetBufferSize = 12; // For timezone offsets e.g. "+01:00"
3736

38-
inline void Append2Digits(char*& p, uint32_t v)
39-
{
37+
inline void Append2Digits(char*& p, uint32_t v) {
4038
*p++ = static_cast<char>('0' + (v / 10));
4139
*p++ = static_cast<char>('0' + (v % 10));
4240
}
4341

44-
inline const char* Float(float value, char (&buf)[kFloatBufferSize], const char* fmt = "%.2f")
45-
{
42+
inline const char* Float(float value, char (&buf)[kFloatBufferSize], const char* fmt = "%.2f") {
4643
snprintf(buf, sizeof(buf), fmt, value);
4744
return buf;
4845
}
4946

50-
inline const char* UtcOffset(int32_t hours, uint32_t minutes, char (&buf)[kOffsetBufferSize])
51-
{
47+
inline const char* UtcOffset(int32_t hours, uint32_t minutes, char (&buf)[kOffsetBufferSize]) {
5248
const auto kNegative = hours < 0;
5349
if (kNegative) hours = -hours;
5450

5551
auto* p = buf;
5652

57-
if (hours != 0)
58-
{
53+
if (hours != 0) {
5954
*p++ = kNegative ? '-' : '+';
6055
}
6156

common/include/json/json_helpers.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@
3131

3232
#include "json/json_jsondoc.h"
3333

34-
namespace json::helpers
35-
{
34+
namespace json::helpers {
3635
// Template wrapper for consistent JSON serialization pattern.
37-
template <typename Callback>
38-
uint32_t Serialize(char* buffer, uint32_t length, Callback&& callback)
39-
{
36+
template <typename Callback> uint32_t Serialize(char* buffer, uint32_t length, Callback&& callback) {
4037
assert(buffer != nullptr);
4138
assert(length != 0);
4239

@@ -47,4 +44,4 @@ uint32_t Serialize(char* buffer, uint32_t length, Callback&& callback)
4744
}
4845
} // namespace json::helpers
4946

50-
#endif // JSON_JSON_HELPERS_H_
47+
#endif // JSON_JSON_HELPERS_H_

common/include/json/json_jsondoc.h

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json_jsondoc.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -30,31 +30,26 @@
3030
#include <cassert>
3131
#include <cstdio>
3232

33-
class JsonDoc
34-
{
33+
class JsonDoc {
3534
public:
36-
JsonDoc(char* buf, uint32_t max_len) : buf_(buf), max_len_(max_len)
37-
{
35+
JsonDoc(char* buf, uint32_t max_len) : buf_(buf), max_len_(max_len) {
3836
assert(buf != nullptr);
3937
assert(max_len > 2); // Need at least space for {}
4038
Write("{");
4139
}
4240

4341
~JsonDoc() = default;
4442

45-
class KeyProxy
46-
{
43+
class KeyProxy {
4744
public:
4845
KeyProxy(JsonDoc& doc, const char* key) : doc_(doc), key_(key) {}
4946

50-
KeyProxy& operator=(const char* value)
51-
{
47+
KeyProxy& operator=(const char* value) {
5248
doc_.WriteField(key_, value);
5349
return *this;
5450
}
5551

56-
KeyProxy& operator=(uint32_t value)
57-
{
52+
KeyProxy& operator=(uint32_t value) {
5853
doc_.WriteField(key_, value);
5954
return *this;
6055
}
@@ -71,16 +66,13 @@ class JsonDoc
7166
uint32_t Size() const { return pos_; }
7267

7368
private:
74-
int CopyString(char* dst, size_t size, const char* src)
75-
{
76-
if (size == 0)
77-
{
69+
int CopyString(char* dst, size_t size, const char* src) {
70+
if (size == 0) {
7871
return 0;
7972
}
8073

8174
size_t length = 0;
82-
while ((length < size - 1) && (*src != '\0'))
83-
{
75+
while ((length < size - 1) && (*src != '\0')) {
8476
*dst++ = *src++;
8577
++length;
8678
}
@@ -89,10 +81,8 @@ class JsonDoc
8981
return static_cast<int>(length);
9082
}
9183

92-
void WriteField(const char* key, const char* value)
93-
{
94-
if (!first_)
95-
{
84+
void WriteField(const char* key, const char* value) {
85+
if (!first_) {
9686
Write(",");
9787
}
9888

@@ -104,10 +94,8 @@ class JsonDoc
10494
first_ = false;
10595
}
10696

107-
void WriteField(const char* key, uint32_t value)
108-
{
109-
if (!first_)
110-
{
97+
void WriteField(const char* key, uint32_t value) {
98+
if (!first_) {
11199
Write(",");
112100
}
113101

@@ -119,14 +107,10 @@ class JsonDoc
119107
char* p = num_buf + sizeof(num_buf);
120108
*--p = '\0';
121109

122-
if (value == 0)
123-
{
110+
if (value == 0) {
124111
*--p = '0';
125-
}
126-
else
127-
{
128-
while (value != 0)
129-
{
112+
} else {
113+
while (value != 0) {
130114
*--p = static_cast<char>('0' + (value % 10));
131115
value /= 10;
132116
}
@@ -136,22 +120,17 @@ class JsonDoc
136120
first_ = false;
137121
}
138122

139-
void Write(const char* s)
140-
{
141-
if (pos_ >= max_len_)
142-
{
123+
void Write(const char* s) {
124+
if (pos_ >= max_len_) {
143125
return;
144126
}
145127

146128
int ret = CopyString(buf_ + pos_, max_len_ - pos_, s);
147129

148130
// CopyString always null-terminates and returns number of chars copied (excluding null)
149-
if (ret >= 0 && static_cast<uint32_t>(ret) + pos_ < max_len_)
150-
{
131+
if (ret >= 0 && static_cast<uint32_t>(ret) + pos_ < max_len_) {
151132
pos_ += static_cast<uint32_t>(ret);
152-
}
153-
else
154-
{
133+
} else {
155134
pos_ = max_len_; // Clamp to signal overflow
156135
}
157136
}
@@ -162,4 +141,4 @@ class JsonDoc
162141
uint32_t pos_{0};
163142
};
164143

165-
#endif // JSON_JSON_JSONDOC_H_
144+
#endif // JSON_JSON_JSONDOC_H_

0 commit comments

Comments
 (0)