refactor: zbylé tři nálezy z review — rozpočet, rename, duplicity

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-09-21 22:37:20 +02:00
1 parent f2b952ec4a
commit b4b425bc51
5 files changed
+104 -31

No files matched your search

+34 -12
View File
@@ -175,15 +175,25 @@ void CommonCLI::savePrefs() {
_prefs->clearDirty();
}
// Rozpočet na jméno pro současné nastavení advertu. Staví prázdný builder
// a ptá se JEHO, ať `set`, `get` i samotný advert počítají z jednoho místa --
// druhá kopie toho výpočtu už jednou zapomněla na FEAT1/FEAT2.
size_t CommonCLI::nameBudget() const {
if (_prefs->advert_loc_policy == ADVERT_LOC_NONE) {
return AdvertDataBuilder(ADV_TYPE_NONE).nameBudget();
}
return AdvertDataBuilder(ADV_TYPE_NONE, NULL, 0.0, 0.0).nameBudget();
}
// Name as it goes on the air: the configured name, plus the solar marker and
// either a charge level or the external-power glyph. Returns the bare name in
// every other case -- an indicator that does not fit is dropped whole rather
// than letting the UTF-8 clamp eat half of it silently.
const char* CommonCLI::advertName(bool with_loc) {
const char* CommonCLI::advertName(size_t budget) {
if (!_prefs->batt_icon) return _prefs->node_name;
size_t nlen = strlen(_prefs->node_name);
if (!mesh::battIconFits(nlen, with_loc)) return _prefs->node_name;
if (!mesh::battIconFits(nlen, budget)) return _prefs->node_name;
char glyph[8];
if (_board->isExternalPowered()) {
@@ -207,14 +217,20 @@ const char* CommonCLI::advertName(bool with_loc) {
}
uint8_t CommonCLI::buildAdvertData(uint8_t node_type, uint8_t* app_data) {
// Builder se postaví bez jména, protože rozpočet na jméno závisí na tom, co
// v něm už je (poloha, případné FEAT). Teprve pak se jméno doplní -- jinak
// by se remainder musel počítat podruhé jinde a rozejít se s encodeTo().
if (_prefs->advert_loc_policy == ADVERT_LOC_NONE) {
AdvertDataBuilder builder(node_type, advertName(false));
AdvertDataBuilder builder(node_type);
builder.setName(advertName(builder.nameBudget()));
return builder.encodeTo(app_data);
} else if (_prefs->advert_loc_policy == ADVERT_LOC_SHARE) {
AdvertDataBuilder builder(node_type, advertName(true), _sensors->node_lat, _sensors->node_lon);
AdvertDataBuilder builder(node_type, NULL, _sensors->node_lat, _sensors->node_lon);
builder.setName(advertName(builder.nameBudget()));
return builder.encodeTo(app_data);
} else {
AdvertDataBuilder builder(node_type, advertName(true), _prefs->node_lat, _prefs->node_lon);
AdvertDataBuilder builder(node_type, NULL, _prefs->node_lat, _prefs->node_lon);
builder.setName(advertName(builder.nameBudget()));
return builder.encodeTo(app_data);
}
}
@@ -686,7 +702,16 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
if (isValidName(&config[5])) {
StrHelper::strncpy(_prefs->node_name, &config[5], sizeof(_prefs->node_name));
savePrefs();
strcpy(reply, "OK");
// Přejmenování může vytlačit zapnutou ikonku z advertu, a to je změna,
// kterou by nikdo neviděl: `set batt.icon` i `get batt.icon` na to
// upozorňují, ale rename je ta cesta, kterou operátor jde později --
// a bez tohohle by mu indikátor zmizel ze vzduchu bez jediné hlášky.
if (_prefs->batt_icon && !mesh::battIconFits(strlen(_prefs->node_name), nameBudget())) {
sprintf(reply, "OK - ALE jméno má %d B, s ikonkou se vejde jen %d B - batt.icon se zahodí",
(int)strlen(_prefs->node_name), (int)(nameBudget() - mesh::battIconWidth()));
} else {
strcpy(reply, "OK");
}
} else {
strcpy(reply, "Error, bad chars");
}
@@ -835,14 +860,12 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
savePrefs();
if (!_prefs->batt_icon) {
strcpy(reply, "off");
} else if (!mesh::battIconFits(strlen(_prefs->node_name),
_prefs->advert_loc_policy != ADVERT_LOC_NONE)) {
} else if (!mesh::battIconFits(strlen(_prefs->node_name), nameBudget())) {
// Say so now rather than let the operator wonder why nothing shows up:
// the icon is dropped whole when it doesn't fit.
sprintf(reply, "on - BUT name is %d B, max %d B with icon - icon omitted",
(int)strlen(_prefs->node_name),
(int)(mesh::advertNameBudget(_prefs->advert_loc_policy != ADVERT_LOC_NONE)
- mesh::battIconWidth()));
(int)(nameBudget() - mesh::battIconWidth()));
} else {
strcpy(reply, "on - shows on next advert");
}
@@ -910,10 +933,9 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
// Report what actually goes on the air, not just the flag: the indicator
// is dropped when the name leaves no room, and the voltage is the only way
// to tell "not there yet" from "board reads nothing".
bool with_loc = _prefs->advert_loc_policy != ADVERT_LOC_NONE;
if (!_prefs->batt_icon) {
strcpy(reply, "> off");
} else if (!mesh::battIconFits(strlen(_prefs->node_name), with_loc)) {
} else if (!mesh::battIconFits(strlen(_prefs->node_name), nameBudget())) {
strcpy(reply, "> on (omitted, name too long)");
} else if (_board->isExternalPowered()) {
strcpy(reply, "> on (external power)");