doc.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Package tview implements rich widgets for terminal based user interfaces. The
  3. widgets provided with this package are useful for data exploration and data
  4. entry.
  5. # Widgets
  6. The package implements the following widgets:
  7. - [TextView]: A scrollable window that display multi-colored text. Text may
  8. also be highlighted.
  9. - [TextArea]: An editable multi-line text area.
  10. - [Table]: A scrollable display of tabular data. Table cells, rows, or columns
  11. may also be highlighted.
  12. - [TreeView]: A scrollable display for hierarchical data. Tree nodes can be
  13. highlighted, collapsed, expanded, and more.
  14. - [List]: A navigable text list with optional keyboard shortcuts.
  15. - [InputField]: One-line input fields to enter text.
  16. - [DropDown]: Drop-down selection fields.
  17. - [Checkbox]: Selectable checkbox for boolean values.
  18. - [Image]: Displays images.
  19. - [Button]: Buttons which get activated when the user selects them.
  20. - [Form]: Forms composed of input fields, drop down selections, checkboxes,
  21. and buttons.
  22. - [Modal]: A centered window with a text message and one or more buttons.
  23. - [Grid]: A grid based layout manager.
  24. - [Flex]: A Flexbox based layout manager.
  25. - [Pages]: A page based layout manager.
  26. The package also provides Application which is used to poll the event queue and
  27. draw widgets on screen.
  28. # Hello World
  29. The following is a very basic example showing a box with the title "Hello,
  30. world!":
  31. package main
  32. import (
  33. "github.com/rivo/tview"
  34. )
  35. func main() {
  36. box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
  37. if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
  38. panic(err)
  39. }
  40. }
  41. First, we create a box primitive with a border and a title. Then we create an
  42. application, set the box as its root primitive, and run the event loop. The
  43. application exits when the application's [Application.Stop] function is called
  44. or when Ctrl-C is pressed.
  45. If we have a primitive which consumes key presses, we call the application's
  46. [Application.SetFocus] function to redirect all key presses to that primitive.
  47. Most primitives then offer ways to install handlers that allow you to react to
  48. any actions performed on them.
  49. # More Demos
  50. You will find more demos in the "demos" subdirectory. It also contains a
  51. presentation (written using tview) which gives an overview of the different
  52. widgets and how they can be used.
  53. # Colors
  54. Throughout this package, colors are specified using the [tcell.Color] type.
  55. Functions such as [tcell.GetColor], [tcell.NewHexColor], and [tcell.NewRGBColor]
  56. can be used to create colors from W3C color names or RGB values.
  57. Almost all strings which are displayed can contain color tags. Color tags are
  58. W3C color names or six hexadecimal digits following a hash tag, wrapped in
  59. square brackets. Examples:
  60. This is a [red]warning[white]!
  61. The sky is [#8080ff]blue[#ffffff].
  62. A color tag changes the color of the characters following that color tag. This
  63. applies to almost everything from box titles, list text, form item labels, to
  64. table cells. In a TextView, this functionality has to be switched on explicitly.
  65. See the TextView documentation for more information.
  66. Color tags may contain not just the foreground (text) color but also the
  67. background color and additional flags. In fact, the full definition of a color
  68. tag is as follows:
  69. [<foreground>:<background>:<flags>]
  70. Each of the three fields can be left blank and trailing fields can be omitted.
  71. (Empty square brackets "[]", however, are not considered color tags.) Colors
  72. that are not specified will be left unchanged. A field with just a dash ("-")
  73. means "reset to default".
  74. You can specify the following flags (some flags may not be supported by your
  75. terminal):
  76. l: blink
  77. b: bold
  78. i: italic
  79. d: dim
  80. r: reverse (switch foreground and background color)
  81. u: underline
  82. s: strike-through
  83. Examples:
  84. [yellow]Yellow text
  85. [yellow:red]Yellow text on red background
  86. [:red]Red background, text color unchanged
  87. [yellow::u]Yellow text underlined
  88. [::bl]Bold, blinking text
  89. [::-]Colors unchanged, flags reset
  90. [-]Reset foreground color
  91. [-:-:-]Reset everything
  92. [:]No effect
  93. []Not a valid color tag, will print square brackets as they are
  94. In the rare event that you want to display a string such as "[red]" or
  95. "[#00ff1a]" without applying its effect, you need to put an opening square
  96. bracket before the closing square bracket. Note that the text inside the
  97. brackets will be matched less strictly than region or colors tags. I.e. any
  98. character that may be used in color or region tags will be recognized. Examples:
  99. [red[] will be output as [red]
  100. ["123"[] will be output as ["123"]
  101. [#6aff00[[] will be output as [#6aff00[]
  102. [a#"[[[] will be output as [a#"[[]
  103. [] will be output as [] (see color tags above)
  104. [[] will be output as [[] (not an escaped tag)
  105. You can use the Escape() function to insert brackets automatically where needed.
  106. # Styles
  107. When primitives are instantiated, they are initialized with colors taken from
  108. the global Styles variable. You may change this variable to adapt the look and
  109. feel of the primitives to your preferred style.
  110. # Unicode Support
  111. This package supports unicode characters including wide characters.
  112. # Concurrency
  113. Many functions in this package are not thread-safe. For many applications, this
  114. may not be an issue: If your code makes changes in response to key events, it
  115. will execute in the main goroutine and thus will not cause any race conditions.
  116. If you access your primitives from other goroutines, however, you will need to
  117. synchronize execution. The easiest way to do this is to call
  118. [Application.QueueUpdate] or [Application.QueueUpdateDraw] (see the function
  119. documentation for details):
  120. go func() {
  121. app.QueueUpdateDraw(func() {
  122. table.SetCellSimple(0, 0, "Foo bar")
  123. })
  124. }()
  125. One exception to this is the io.Writer interface implemented by [TextView]. You
  126. can safely write to a [TextView] from any goroutine. See the [TextView]
  127. documentation for details.
  128. You can also call [Application.Draw] from any goroutine without having to wrap
  129. it in [Application.QueueUpdate]. And, as mentioned above, key event callbacks
  130. are executed in the main goroutine and thus should not use
  131. [Application.QueueUpdate] as that may lead to deadlocks.
  132. # Type Hierarchy
  133. All widgets listed above contain the [Box] type. All of [Box]'s functions are
  134. therefore available for all widgets, too. Please note that if you are using the
  135. functions of [Box] on a subclass, they will return a *Box, not the subclass. So
  136. while tview supports method chaining in many places, these chains must be broken
  137. when using [Box]'s functions. Example:
  138. // This will cause "textArea" to be an empty Box.
  139. textArea := tview.NewTextArea().
  140. SetMaxLength(256).
  141. SetPlaceholder("Enter text here").
  142. SetBorder(true)
  143. You will need to call [Box.SetBorder] separately:
  144. textArea := tview.NewTextArea().
  145. SetMaxLength(256).
  146. SetPlaceholder("Enter text here")
  147. texArea.SetBorder(true)
  148. All widgets also implement the [Primitive] interface.
  149. The tview package is based on https://github.com/gdamore/tcell. It uses types
  150. and constants from that package (e.g. colors and keyboard values).
  151. */
  152. package tview