Framework-level helpers for inspecting and rebuilding styled terminal lines.
Most app code should stay in Tui.Screen. Reach for this module
when you need to preserve styling while transforming rendered text. Typical use
cases are highlighting search matches in an already-styled buffer, applying
syntax coloring to rendered output, splitting lines at arbitrary character
positions without losing styles, or implementing diff-style overlays. The
tui-widgets package uses this internally for its search and layout features.
import Tui.Screen as Screen
import Tui.Screen.Advanced as Advanced
screen : Screen.Screen
screen =
Screen.concat
[ Screen.text "hello "
, Screen.text "world" |> Screen.fg Ansi.Color.green
]
screen
|> Advanced.toLines
|> List.head
|> Maybe.withDefault []
|> Advanced.fromLine
|> Screen.toStringA styled segment within a rendered line. Obtain spans by calling
toLines on a rendered Tui.Screen.Screen. Inspect the style
field with styleForeground,
styleBackground,
styleAttributes, and
styleHyperlink.
Flatten a Tui.Screen.Screen into rendered lines of styled spans.
import Ansi.Color
import Tui.Screen as Screen
import Tui.Screen.Advanced as Advanced
lines : List Advanced.Line
lines =
Screen.concat
[ Screen.text "hello "
, Screen.text "world" |> Screen.fg Ansi.Color.green
]
|> Advanced.toLinesRebuild a single rendered line from styled spans.
An empty line becomes Tui.Screen.blank, preserving the
fact that it takes up one terminal row.
Typical use is a round-trip: obtain spans with toLines, transform
them (e.g. to highlight search matches), and reassemble with fromLine.
import Tui.Screen as Screen
import Tui.Screen.Advanced as Advanced
source : Screen.Screen
source =
Screen.text "hello" |> Screen.bold
roundTripped : Screen.Screen
roundTripped =
source
|> Advanced.toLines
|> List.head
|> Maybe.withDefault []
|> Advanced.fromLineGetters for the style field of a Span. The Style type from
Tui.Screen is opaque so that future releases can add
fields without breaking user code; these getters are the supported way
to inspect it.
Read the foreground color of a Style.
Read the background color of a Style.
Read the attribute list of a Style.
import Tui.Attribute as Attr
import Tui.Screen.Advanced as Advanced
if List.member Attr.Bold (Advanced.styleAttributes span.style) then
...Read the hyperlink URL of a Style, if any.
For lower-level access, FlatStyle is a plain record with one Bool
field per Tui.Attribute.Attribute plus the
color/hyperlink fields. Conversion to/from a Style is provided for
framework-level consumers that need to manipulate styles as records.
Flat record representation of a Style, with one
Bool per attribute plus color and hyperlink fields.
Convert an opaque Style to its flat record form.
Convert a flat record back to an opaque Style. Inverse of
styleToFlatStyle.
A rendered screen line represented as styled spans.