Skip to content

Commit

Permalink
Add function TText::fill
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Aug 21, 2020
1 parent b376b70 commit 7fdf689
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TScreenCell> 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);
```
Expand Down
37 changes: 37 additions & 0 deletions include/tvision/ttext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TScreenCell> 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);

Expand Down Expand Up @@ -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<TScreenCell> 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()) {
Expand Down Expand Up @@ -284,6 +301,26 @@ inline void TText::eat( TScreenCell *cell, size_t n, size_t &width,
}
}

inline size_t TText::fill( TSpan<TScreenCell> 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'.
//
Expand Down

0 comments on commit 7fdf689

Please sign in to comment.