Use lambda, fix wrap pointer

This commit is contained in:
Scott Lahteine 2019-06-15 15:29:15 -05:00
parent 1aa60bce32
commit 24c23b60fa

View File

@ -204,33 +204,36 @@ millis_t next_button_update_ms;
SETCURSOR(x, y); SETCURSOR(x, y);
if (!string) return; if (!string) return;
auto _newline = [&x, &y]() {
x = 0; y++; // move x to string len (plus space)
SETCURSOR(0, y); // simulate carriage return
};
uint8_t *p = (uint8_t*)string; uint8_t *p = (uint8_t*)string;
wchar_t ch; wchar_t ch;
if (wordwrap) { if (wordwrap) {
uint8_t *wrd = p, c = 0; uint8_t *wrd = nullptr, c = 0;
// find the end of the part
for (;;) { for (;;) {
if (!wrd) wrd = p; // Get word start /before/ advancing
p = get_utf8_value_cb(p, cb_read_byte, &ch); p = get_utf8_value_cb(p, cb_read_byte, &ch);
const bool eol = !ch; const bool eol = !ch; // zero ends the string
// End or a break between phrases?
if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') { if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
if (!c && ch == ' ') continue; // collapse extra spaces if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
if (x + c > LCD_WIDTH && x >= (LCD_WIDTH) / 4) { // should it wrap? // Past the right and the word is not too long?
x = 0; y++; // move x to string len (plus space) if (x + c > LCD_WIDTH && x >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
SETCURSOR(0, y); // simulate carriage return
}
c += !eol; // +1 so the space will be printed c += !eol; // +1 so the space will be printed
x += c; // advance x to new position x += c; // advance x to new position
while (c) { // character countdown while (c) { // character countdown
--c; // count down to zero --c; // count down to zero
wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
lcd_put_wchar(ch); // word (plus space) to the LCD lcd_put_wchar(ch); // character to the LCD
} }
if (eol) break; // all done if (eol) break; // all done!
wrd = nullptr; // set up for next word wrd = nullptr; // set up for next word
} }
else { else c++; // count word characters
if (!wrd) wrd = p; // starting a new word?
c++; // count word characters
}
} }
} }
else { else {
@ -239,10 +242,7 @@ millis_t next_button_update_ms;
if (!ch) break; if (!ch) break;
lcd_put_wchar(ch); lcd_put_wchar(ch);
x++; x++;
if (x >= LCD_WIDTH) { if (x >= LCD_WIDTH) _newline();
x = 0; y++;
SETCURSOR(0, y);
}
} }
} }
} }