Manual line break on multiline text with \r

Solves #1367
This commit is contained in:
Edward Firmo
2023-12-04 09:30:18 +01:00
parent 961b2c3f4d
commit c1a513610b
2 changed files with 23 additions and 18 deletions

View File

@@ -105,6 +105,7 @@ packages:
- Fix climate page not updating properly
- 4.1.4 patch:
- Supports new `weather.get_forecasts` introduced by Home Assistant 2023.12.0
- Manual line break on multiline text with `\r`
 
## Details of noteworthy changes

View File

@@ -7,7 +7,7 @@
substitutions:
##### DON'T CHANGE THIS #####
version: "4.1.3"
version: "4.1.4"
#############################
#external_components:
@@ -2094,23 +2094,27 @@ script:
int startPos = 0;
int endPos = 0;
std::string wrappedText = "";
while (startPos < text_to_display.length()) {
while (text_to_display[startPos] == ' ' and startPos < text_to_display.length()) { startPos++; }
int endPos = startPos + line_length_limit;
if (endPos >= text_to_display.length()) endPos = text_to_display.length();
else
{
while (endPos > startPos && text_to_display[endPos] != ' ') { endPos--; }
if (endPos == startPos) endPos = startPos + line_length_limit; // Handle case of long word
}
wrappedText += text_to_display.substr(startPos, endPos-startPos);
if (endPos < text_to_display.length())
{
while (text_to_display[endPos] == ' ') { endPos--; }
if (endPos >= startPos) wrappedText += "\\r";
}
startPos = endPos + 1; // Skip the space
while (text_to_display[startPos] == ' ' and startPos < text_to_display.length()) { startPos++; }
if (text_to_display.find("\\r") != std::string::npos) {
wrappedText = text_to_display;
} else {
while (startPos < text_to_display.length()) {
while (text_to_display[startPos] == ' ' and startPos < text_to_display.length()) { startPos++; }
int endPos = startPos + line_length_limit;
if (endPos >= text_to_display.length()) endPos = text_to_display.length();
else
{
while (endPos > startPos && text_to_display[endPos] != ' ') { endPos--; }
if (endPos == startPos) endPos = startPos + line_length_limit; // Handle case of long word
}
wrappedText += text_to_display.substr(startPos, endPos-startPos);
if (endPos < text_to_display.length())
{
while (text_to_display[endPos] == ' ') { endPos--; }
if (endPos >= startPos) wrappedText += "\\r";
}
startPos = endPos + 1; // Skip the space
while (text_to_display[startPos] == ' ' and startPos < text_to_display.length()) { startPos++; }
}
}
disp1->set_component_text_printf(component.c_str(), "%s", wrappedText.c_str());