doc.go 6.5 KB

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