diff --git a/README.md b/README.md index f7be1aba..24d38a8e 100644 --- a/README.md +++ b/README.md @@ -345,6 +345,7 @@ size_t TText::next(TStringView text); size_t TText::prev(TStringView text, size_t index); size_t TText::wseek(TStringView text, int count, Boolean incRemainder=True); void TText::eat(TScreenCell *cell, size_t n, size_t &width, TStringView text, size_t &bytes); +void TText::fill(TSpan cells, size_t width, TStringView text, TCellAttribs attr); void TText::next(TStringView text, size_t &bytes, size_t &width); void TText::wseek(TStringView text, size_t &index, size_t &remainder, int count); ``` diff --git a/include/tvision/ttext.h b/include/tvision/ttext.h index e182f353..71913550 100644 --- a/include/tvision/ttext.h +++ b/include/tvision/ttext.h @@ -137,6 +137,7 @@ class TText { static size_t wseek(TStringView text, int count, Boolean incRemainder=True); static void eat(TScreenCell *cell, size_t n, size_t &width, TStringView text, size_t &bytes); + static size_t fill(TSpan cells, size_t width, TStringView text, TCellAttribs attr); static void next(TStringView text, size_t &bytes, size_t &width); static void wseek(TStringView text, size_t &index, size_t &remainder, int count); @@ -169,6 +170,22 @@ inline void TText::eat( TScreenCell *cell, size_t n, size_t &width, } } +#pragma warn -inl + +inline size_t TText::fill( TSpan cells, size_t width, + TStringView text, TCellAttribs attr ) +{ + if (cells.size()) { + size_t count = min(min(cells.size(), width), text.size()); + for (size_t i = 0; i < count; ++i) + ::setCell(cells[i], text[i], attr); + return count; + } + return 0; +} + +#pragma warn .inl + inline void TText::next(TStringView text, size_t &bytes, size_t &width) { if (text.size()) { @@ -284,6 +301,26 @@ inline void TText::eat( TScreenCell *cell, size_t n, size_t &width, } } +inline size_t TText::fill( TSpan cells, size_t width, + TStringView text, TCellAttribs attr ) +// Fills at most 'width' 'cells' with characters from 'text'. +// Returns the number of cells filled. Note that one cell is always one display column. +{ + if (cells.size()) { + if (cells.size() < width) + width = cells.size(); + size_t w = 0, b = 0; + while (w < width && b < text.size()) { + ::setAttr(cells[w], attr); + TText::eat(&cells[w], width - w, w, text.substr(b), b); + } + // TText::eat always increases 'w' by the width of the processed + // text, but it never fills more cells than there are available. + return std::min(w, cells.size()); + } + return 0; +} + inline void TText::next(TStringView text, size_t &bytes, size_t &width) // Measures the length and width of the first character in 'text'. //