| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- ---
- title: Text
- description: Display styled text content
- order: 1
- skill:
- entry: true
- intents: [text, styling, content, selection]
- ---
- # Text
- Display styled text content with support for colors, attributes, and text selection.
- ## Basic Usage
- ### Renderable API
- ```typescript
- import { TextRenderable, createCliRenderer } from "@opentui/core"
- const renderer = await createCliRenderer()
- const text = new TextRenderable(renderer, {
- id: "greeting",
- content: "Hello, OpenTUI!",
- fg: "#00FF00",
- })
- renderer.root.add(text)
- ```
- ### Construct API
- ```typescript
- import { Text, createCliRenderer } from "@opentui/core"
- const renderer = await createCliRenderer()
- renderer.root.add(
- Text({
- content: "Hello, OpenTUI!",
- fg: "#00FF00",
- }),
- )
- ```
- ## Text attributes
- Combine multiple text attributes using bitwise OR:
- ```typescript
- import { TextRenderable, TextAttributes } from "@opentui/core"
- const styledText = new TextRenderable(renderer, {
- id: "styled",
- content: "Important Message",
- fg: "#FFFF00",
- attributes: TextAttributes.BOLD | TextAttributes.UNDERLINE,
- })
- ```
- ### Available attributes
- | Attribute | Description |
- | ------------------------------ | ------------------ |
- | `TextAttributes.BOLD` | Bold text |
- | `TextAttributes.DIM` | Dimmed text |
- | `TextAttributes.ITALIC` | Italic text |
- | `TextAttributes.UNDERLINE` | Underlined text |
- | `TextAttributes.BLINK` | Blinking text |
- | `TextAttributes.INVERSE` | Inverted colors |
- | `TextAttributes.HIDDEN` | Hidden text |
- | `TextAttributes.STRIKETHROUGH` | Strikethrough text |
- ## Template literals for rich text
- Use the `t` template literal for inline styling within a single text element:
- ```typescript
- import { TextRenderable, t, bold, underline, fg, bg, italic } from "@opentui/core"
- const richText = new TextRenderable(renderer, {
- id: "rich",
- content: t`${bold("Important:")} ${fg("#FF0000")(underline("Warning!"))} Normal text`,
- })
- ```
- ### Available style functions
- ```typescript
- import { t, bold, dim, italic, underline, blink, reverse, strikethrough, fg, bg } from "@opentui/core"
- // Basic attributes
- t`${bold("bold text")}`
- t`${italic("italic text")}`
- t`${underline("underlined")}`
- t`${strikethrough("deleted")}`
- // Colors
- t`${fg("#FF0000")("red text")}`
- t`${bg("#0000FF")("blue background")}`
- // Combining styles
- t`${bold(fg("#FFFF00")("bold yellow"))}`
- ```
- ## Positioning
- ```typescript
- const text = new TextRenderable(renderer, {
- id: "positioned",
- content: "Absolute position",
- position: "absolute",
- left: 10,
- top: 5,
- })
- ```
- ## Text selection
- Enable text selection for copying:
- ```typescript
- const selectableText = new TextRenderable(renderer, {
- id: "selectable",
- content: "Select me!",
- selectable: true, // Default is true
- })
- const nonSelectable = new TextRenderable(renderer, {
- id: "label",
- content: "Button Label",
- selectable: false, // Disable selection
- })
- ```
- ## Properties
- | Property | Type | Default | Description |
- | -------------------------------- | --------------------------------- | ------------ | ---------------------------- |
- | `content` | `string \| StyledText` | `""` | The text content to display |
- | `fg` | `string \| RGBA` | - | Foreground (text) color |
- | `bg` | `string \| RGBA` | - | Background color |
- | `attributes` | `TextAttributes` | `0` | Text styling attributes |
- | `selectable` | `boolean` | `true` | Whether text can be selected |
- | `position` | `"relative" \| "absolute"` | `"relative"` | Positioning mode |
- | `left`, `top`, `right`, `bottom` | `number \| "auto" \| "{number}%"` | - | Position offsets |
- ## Example: status bar
- ```typescript
- import { Text, Box, t, bold, fg } from "@opentui/core"
- const statusBar = Box(
- {
- position: "absolute",
- bottom: 0,
- width: "100%",
- height: 1,
- backgroundColor: "#333333",
- flexDirection: "row",
- justifyContent: "space-between",
- paddingLeft: 1,
- paddingRight: 1,
- },
- Text({
- content: t`${bold("myfile.ts")} - ${fg("#888888")("TypeScript")}`,
- }),
- Text({
- content: t`Ln ${fg("#00FF00")("42")}, Col ${fg("#00FF00")("15")}`,
- }),
- )
- renderer.root.add(statusBar)
- ```
|