Paragraph

io.worxbend.tui.widgets.Paragraph
See theParagraph companion object
final case class Paragraph(text: Text, alignment: Alignment = ..., overflow: Overflow = ..., scrollY: Int = ..., scrollX: Int = ..., style: Style = ...) extends Widget, Measured

Multi-line styled text with alignment and optional wrapping.

alignment places every line, and it is the last word rather than the first: a Line that carries an alignment of its own overrides it for that one row, and failing that the Text's own alignment overrides it for every row. A wrapped line keeps its alignment on every row it spills onto.

style is likewise the outermost style layer, not the only one. Every character is drawn with this style, then the text's own io.worxbend.tui.core.Text.style on top of it, then the line's, then the span's — each step a io.worxbend.tui.core.Style.patch, so the more specific layer wins wherever it speaks. A Paragraph given a style therefore no longer erases a style the Text was built with; it sits underneath it.

With Overflow.Wrap the text breaks at word boundaries: a word moves to the next row whole rather than being cut in half, and the blanks that sat at the break are dropped instead of becoming a stray indent on the next row. A word longer than the whole width has nowhere to go, so that one is broken between grapheme clusters — never inside one, so a wide character or emoji is never split. With Overflow.Clip, the default, long lines are cut at the area edge. Overflow.WrapTrimmed and Overflow.WrapPreserved wrap the same way and differ only in what becomes of the blanks at the head of a row — see Overflow for the three renderings side by side.

A line that is still wider than the area — one that is clipped, or a single unbreakable word — loses the side away from its alignment: a right-aligned line keeps its end, a centred one loses as much from each side, and a left-aligned one keeps its beginning. Keeping the beginning of a right-aligned line would hide exactly the part the alignment was chosen to show, which for a path or a timestamp is the part that identifies it.

scrollY and scrollX make the area a window into a larger document rather than the whole of it. scrollY skips that many composed rows before drawing: with Overflow.Wrap those are wrapped rows, not source lines, so scrolling a reflowed document by one moves the view by exactly one screen row whatever the width — which is the case ScrollView cannot express, because it scrolls a rendered widget and has to be told its content height. scrollX skips that many columns from the start of every row it draws, which only has anything to skip under Overflow.Clip: wrapped rows are never wider than the area. Both are clamped at zero, and scrolling past the end of the text leaves the area blank rather than failing.

style is the paragraph's own style, and it is painted over the whole area — not only the cells that carry a character. A paragraph given a background colour therefore reads as a block of colour with text in it, which is what a caller sets a background for; each span's style is then layered on top of it for the text itself.

heightAt reports the rows the paragraph needs from the same overflow field render draws with, so the two cannot disagree about whether the text wraps. Measurement memoization belongs to this immutable value: it keeps one height answer (the latest width) and one natural width, not rows or a global content index. Copies/rebuilt paragraphs start fresh, and the text must obey its immutable collection contract. Rendering never forces either measurement. Concurrent measurements are safe; rendering still inherits Buffer's caller-owned confinement.

Attributes

Companion
object
Graph
Supertypes
trait Serializable
trait Product
trait Equals
trait Measured
trait Widget
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

override def heightAt(width: Int): Option[Int]

The rows this text occupies at width — the measurement counterpart of render, and always an answer: a paragraph always knows its own height. A clipping paragraph is one row per line; a wrapping one counts the rows wrapLine would produce, which is why the two share that function rather than each doing the arithmetic.

The rows this text occupies at width — the measurement counterpart of render, and always an answer: a paragraph always knows its own height. A clipping paragraph is one row per line; a wrapping one counts the rows wrapLine would produce, which is why the two share that function rather than each doing the arithmetic.

A wrapping paragraph given no columns at all (width <= 0) measures zero rows, because zero rows is what it draws: there is nothing to wrap into, wrapLine returns no rows and render returns immediately on an empty area. It used to answer one row per source line there, which is the one width at which measurement and rendering could disagree. Zero is an answer, not a refusal — io.worxbend.tui.core.Measured reserves None for "cannot say", and a paragraph with no columns can say. A clipping paragraph is still one row per line at any width, because clipping never consults the width to decide how many rows there are.

The scroll offsets are deliberately not subtracted. This is the height of the whole text, which is what a caller sizing a viewport or driving a Scrollbar beside it needs: the scrollbar's contentLength is the document, and its position is the same scrollY the paragraph was given. A scrolled paragraph still occupies whatever rows its area gives it.

Attributes

Definition Classes
Measured
def render(area: Rect, buffer: Buffer): Unit
override def widthAt(height: Int): Option[Int]

The columns this text needs so that no line is clipped and no line has to wrap — the width counterpart of heightAt, and always an answer, because the longest line is a property of the text itself and needs no layout to work out.

The columns this text needs so that no line is clipped and no line has to wrap — the width counterpart of heightAt, and always an answer, because the longest line is a property of the text itself and needs no layout to work out.

The height argument is ignored: a paragraph flows top to bottom, so how many rows it is given does not change how wide it would like to be. The answer is the same for both Overflow modes on purpose — Overflow.Clip needs this width to avoid cutting a line off, Overflow.Wrap needs it to avoid reflowing one. It is the longest line, not the longest word: at the longest-word width the text still fits, but only after wrapping, which is the thing this number exists to avoid.

Attributes

Definition Classes
Measured

Inherited methods

def productElementNames: Iterator[String]

An iterator over the names of all the elements of this product.

An iterator over the names of all the elements of this product.

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

An iterator over all the elements of this product.

An iterator over all the elements of this product.

Attributes

Returns

in the default implementation, an Iterator[Any]

Inherited from:
Product