text.mdx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ---
  2. title: Text
  3. description: Display styled text content
  4. order: 1
  5. skill:
  6. entry: true
  7. intents: [text, styling, content, selection]
  8. ---
  9. # Text
  10. Display styled text content with support for colors, attributes, and text selection.
  11. ## Basic Usage
  12. ### Renderable API
  13. ```typescript
  14. import { TextRenderable, createCliRenderer } from "@opentui/core"
  15. const renderer = await createCliRenderer()
  16. const text = new TextRenderable(renderer, {
  17. id: "greeting",
  18. content: "Hello, OpenTUI!",
  19. fg: "#00FF00",
  20. })
  21. renderer.root.add(text)
  22. ```
  23. ### Construct API
  24. ```typescript
  25. import { Text, createCliRenderer } from "@opentui/core"
  26. const renderer = await createCliRenderer()
  27. renderer.root.add(
  28. Text({
  29. content: "Hello, OpenTUI!",
  30. fg: "#00FF00",
  31. }),
  32. )
  33. ```
  34. ## Text attributes
  35. Combine multiple text attributes using bitwise OR:
  36. ```typescript
  37. import { TextRenderable, TextAttributes } from "@opentui/core"
  38. const styledText = new TextRenderable(renderer, {
  39. id: "styled",
  40. content: "Important Message",
  41. fg: "#FFFF00",
  42. attributes: TextAttributes.BOLD | TextAttributes.UNDERLINE,
  43. })
  44. ```
  45. ### Available attributes
  46. | Attribute | Description |
  47. | ------------------------------ | ------------------ |
  48. | `TextAttributes.BOLD` | Bold text |
  49. | `TextAttributes.DIM` | Dimmed text |
  50. | `TextAttributes.ITALIC` | Italic text |
  51. | `TextAttributes.UNDERLINE` | Underlined text |
  52. | `TextAttributes.BLINK` | Blinking text |
  53. | `TextAttributes.INVERSE` | Inverted colors |
  54. | `TextAttributes.HIDDEN` | Hidden text |
  55. | `TextAttributes.STRIKETHROUGH` | Strikethrough text |
  56. ## Template literals for rich text
  57. Use the `t` template literal for inline styling within a single text element:
  58. ```typescript
  59. import { TextRenderable, t, bold, underline, fg, bg, italic } from "@opentui/core"
  60. const richText = new TextRenderable(renderer, {
  61. id: "rich",
  62. content: t`${bold("Important:")} ${fg("#FF0000")(underline("Warning!"))} Normal text`,
  63. })
  64. ```
  65. ### Available style functions
  66. ```typescript
  67. import { t, bold, dim, italic, underline, blink, reverse, strikethrough, fg, bg } from "@opentui/core"
  68. // Basic attributes
  69. t`${bold("bold text")}`
  70. t`${italic("italic text")}`
  71. t`${underline("underlined")}`
  72. t`${strikethrough("deleted")}`
  73. // Colors
  74. t`${fg("#FF0000")("red text")}`
  75. t`${bg("#0000FF")("blue background")}`
  76. // Combining styles
  77. t`${bold(fg("#FFFF00")("bold yellow"))}`
  78. ```
  79. ## Positioning
  80. ```typescript
  81. const text = new TextRenderable(renderer, {
  82. id: "positioned",
  83. content: "Absolute position",
  84. position: "absolute",
  85. left: 10,
  86. top: 5,
  87. })
  88. ```
  89. ## Text selection
  90. Enable text selection for copying:
  91. ```typescript
  92. const selectableText = new TextRenderable(renderer, {
  93. id: "selectable",
  94. content: "Select me!",
  95. selectable: true, // Default is true
  96. })
  97. const nonSelectable = new TextRenderable(renderer, {
  98. id: "label",
  99. content: "Button Label",
  100. selectable: false, // Disable selection
  101. })
  102. ```
  103. ## Properties
  104. | Property | Type | Default | Description |
  105. | -------------------------------- | --------------------------------- | ------------ | ---------------------------- |
  106. | `content` | `string \| StyledText` | `""` | The text content to display |
  107. | `fg` | `string \| RGBA` | - | Foreground (text) color |
  108. | `bg` | `string \| RGBA` | - | Background color |
  109. | `attributes` | `TextAttributes` | `0` | Text styling attributes |
  110. | `selectable` | `boolean` | `true` | Whether text can be selected |
  111. | `position` | `"relative" \| "absolute"` | `"relative"` | Positioning mode |
  112. | `left`, `top`, `right`, `bottom` | `number \| "auto" \| "{number}%"` | - | Position offsets |
  113. ## Example: status bar
  114. ```typescript
  115. import { Text, Box, t, bold, fg } from "@opentui/core"
  116. const statusBar = Box(
  117. {
  118. position: "absolute",
  119. bottom: 0,
  120. width: "100%",
  121. height: 1,
  122. backgroundColor: "#333333",
  123. flexDirection: "row",
  124. justifyContent: "space-between",
  125. paddingLeft: 1,
  126. paddingRight: 1,
  127. },
  128. Text({
  129. content: t`${bold("myfile.ts")} - ${fg("#888888")("TypeScript")}`,
  130. }),
  131. Text({
  132. content: t`Ln ${fg("#00FF00")("42")}, Col ${fg("#00FF00")("15")}`,
  133. }),
  134. )
  135. renderer.root.add(statusBar)
  136. ```