ascii-font.mdx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ---
  2. title: ASCIIFont
  3. description: Display text using ASCII art fonts
  4. order: 14
  5. ---
  6. # ASCIIFont
  7. Display text using ASCII art fonts with multiple font styles available. Great for titles, headers, and decorative text.
  8. ## Basic Usage
  9. ### Renderable API
  10. ```typescript
  11. import { ASCIIFontRenderable, RGBA, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const title = new ASCIIFontRenderable(renderer, {
  14. id: "title",
  15. text: "OPENTUI",
  16. font: "tiny",
  17. color: RGBA.fromInts(255, 255, 255, 255),
  18. })
  19. renderer.root.add(title)
  20. ```
  21. ### Construct API
  22. ```typescript
  23. import { ASCIIFont, createCliRenderer } from "@opentui/core"
  24. const renderer = await createCliRenderer()
  25. renderer.root.add(
  26. ASCIIFont({
  27. text: "HELLO",
  28. font: "block",
  29. color: "#00FF00",
  30. }),
  31. )
  32. ```
  33. ## Available Fonts
  34. OpenTUI includes several ASCII art font styles:
  35. ```typescript
  36. // Small, compact font
  37. {
  38. font: "tiny"
  39. }
  40. // Block style font
  41. {
  42. font: "block"
  43. }
  44. // Shaded style font
  45. {
  46. font: "shade"
  47. }
  48. // Slick style font
  49. {
  50. font: "slick"
  51. }
  52. // Large font
  53. {
  54. font: "huge"
  55. }
  56. // Grid style font
  57. {
  58. font: "grid"
  59. }
  60. // Pallet style font
  61. {
  62. font: "pallet"
  63. }
  64. ```
  65. ## Positioning
  66. Position the ASCII text anywhere on screen:
  67. ```typescript
  68. const title = new ASCIIFontRenderable(renderer, {
  69. id: "title",
  70. text: "TITLE",
  71. font: "block",
  72. color: RGBA.fromHex("#FFFF00"),
  73. x: 10,
  74. y: 2,
  75. })
  76. ```
  77. ## Properties
  78. | Property | Type | Default | Description |
  79. | ----------------- | ---------------------------- | --------------- | -------------------------- |
  80. | `text` | `string` | `""` | Text to display |
  81. | `font` | `ASCIIFontName` | `"tiny"` | Font style to use |
  82. | `color` | `ColorInput \| ColorInput[]` | `"#FFFFFF"` | Text color(s) |
  83. | `backgroundColor` | `ColorInput` | `"transparent"` | Background color |
  84. | `selectable` | `boolean` | `true` | Whether text is selectable |
  85. | `selectionBg` | `ColorInput` | - | Selection background color |
  86. | `selectionFg` | `ColorInput` | - | Selection foreground color |
  87. | `x` | `number` | - | X position offset |
  88. | `y` | `number` | - | Y position offset |
  89. ## Example: Welcome Screen
  90. ```typescript
  91. import { Box, ASCIIFont, Text, createCliRenderer } from "@opentui/core"
  92. const renderer = await createCliRenderer()
  93. const welcomeScreen = Box(
  94. {
  95. width: "100%",
  96. height: "100%",
  97. flexDirection: "column",
  98. alignItems: "center",
  99. justifyContent: "center",
  100. },
  101. ASCIIFont({
  102. text: "OPENTUI",
  103. font: "huge",
  104. color: "#00FFFF",
  105. }),
  106. Text({
  107. content: "Terminal UI Framework",
  108. fg: "#888888",
  109. }),
  110. Text({
  111. content: "Press any key to continue...",
  112. fg: "#444444",
  113. }),
  114. )
  115. renderer.root.add(welcomeScreen)
  116. ```
  117. ## Dynamic Text
  118. Update the text content dynamically:
  119. ```typescript
  120. const counter = new ASCIIFontRenderable(renderer, {
  121. id: "counter",
  122. text: "0",
  123. font: "block",
  124. color: RGBA.fromHex("#FF0000"),
  125. })
  126. let count = 0
  127. setInterval(() => {
  128. count++
  129. counter.text = count.toString()
  130. }, 1000)
  131. ```
  132. ## Color Effects
  133. Create gradient-like effects by positioning multiple ASCII fonts:
  134. ```typescript
  135. import { Box, ASCIIFont } from "@opentui/core"
  136. const gradientTitle = Box(
  137. {},
  138. ASCIIFont({
  139. text: "HELLO",
  140. font: "block",
  141. color: "#FF0000",
  142. }),
  143. // Overlay with offset for shadow effect
  144. ASCIIFont({
  145. text: "HELLO",
  146. font: "block",
  147. color: "#880000",
  148. left: 1,
  149. top: 1,
  150. }),
  151. )
  152. ```