[ Widgets ]
Text
koto::Text renders single-line or multi-line UTF-8 text into an area with configurable horizontal alignment, soft word wrapping, overflow clipping or ellipsis truncation, and TextAttr styling (Header: koto/text.hpp).
Quick example
Section titled “Quick example”Text("koto C++23 Terminal UI\nSecond line") .align(TextAlign::Center) .fg(Color::Cyan) .bold() .draw(frame, text_area); koto C++23 Terminal UI Second lineBehavior
Section titled “Behavior”- Hard line breaks (
\n):Text::draw()splitsm_contenton\nfirst (src/text.cpp:104-106) and ignores\rcodepoints (src/text.cpp:191, 238). - Horizontal alignment: For each paragraph line, when
m_alignisTextAlign::CenterorTextAlign::Rightandtext_width(line) < area.width,start_colis offset toarea.col + (area.width - line_w) / 2orarea.col + (area.width - line_w)(src/text.cpp:110-125). - No-wrap mode (
TextWrap::None, default):- Tabs (
\t) advancecolto the next 4-column tab stop relative toarea.col, filling intermediate cells with spaces (src/text.cpp:243-255). - When a grapheme would exceed
area.right()(col + w > area.right()), ifm_overflow == TextOverflow::Ellipsisandarea.right() > area.col,Textoverwrites the last cell on the row (area.right() - 1) withCell{U'…', m_fg, m_bg}and skips the rest of the line (src/text.cpp:260-268).
- Tabs (
- Word-wrap mode (
.wrap()/TextWrap::Wrap):- Scans tokens of contiguous spaces/tabs (
' 'or'\t') or contiguous non-whitespace graphemes (src/text.cpp:142-168). - Leading spaces/tabs at the start of a wrapped row (
col == area.col) are skipped (src/text.cpp:136-140). - If a word token does not fit on the current line (
col + token_width > area.right() && col > area.col),Textadvancesrow++and resetscol = area.colbefore drawing the word. Ifrow >= area.bottom()andm_overflow == TextOverflow::Ellipsis, it writesU'…'at(row - 1, area.right() - 1)and returns (src/text.cpp:171-183). - Words longer than
area.widthwrap character-by-character atarea.right().
- Scans tokens of contiguous spaces/tabs (
Methods
Section titled “Methods”| Method | Description | Default |
|---|---|---|
Text(std::string_view content = "") | Constructs a Text widget with content copied into m_content. | content = "" |
fg(Color fg) | Sets the text foreground color. | COLOR_DEFAULT |
bg(Color bg) | Sets the text background color (applied only to cells where glyphs/spaces are drawn). | COLOR_DEFAULT |
wrap() | Enables soft word-wrapping (m_text_wrap = TextWrap::Wrap). | TextWrap::None |
clip() | Sets overflow mode to TextOverflow::Clip. | TextOverflow::Clip |
ellipsis() | Sets overflow mode to TextOverflow::Ellipsis (U'…'). | TextOverflow::Clip |
overflow(TextOverflow o) | Explicitly sets TextOverflow::Clip or TextOverflow::Ellipsis. | TextOverflow::Clip |
align(TextAlign a) | Sets horizontal alignment (TextAlign::Left, Center, Right). | TextAlign::Left |
bold(bool b = true) | Sets or clears TextAttr::Bold. | false |
dim(bool d = true) | Sets or clears TextAttr::Dim. | false |
italic(bool i = true) | Sets or clears TextAttr::Italic. | false |
underline(bool u = true) | Sets or clears TextAttr::Underline. | false |
reverse(bool r = true) | Sets or clears TextAttr::Reverse. | false |
attr(TextAttr a) | Bitwise-ORs a into m_attrs (e.g. TextAttr::Strike). | 0 |
draw(Frame& frame, Area area) | Renders the text into frame within area. | None |
Gotchas
Section titled “Gotchas”- Strikethrough requires
.attr(TextAttr::Strike):Textprovides convenience methods.bold(),.dim(),.italic(),.underline(), and.reverse(), whileTextAttr::Strikeis applied via.attr(TextAttr::Strike). Textdoes not fill trailing or leading blank cells:Text::draw()only writes cells occupied by characters inm_content. If you want the entireareafilled with a background color, draw aFill(bg)orBox().filled(true).bg(bg)first.Textcopiesstd::string_viewintostd::string m_content: Although the constructor takesstd::string_view,m_contentis astd::stringmember (include/koto/text.hpp:56).