Source File
doc.go
Belonging Package
github.com/pancsta/cview
/*Package cview implements rich widgets for terminal based user interfaces.See the demos folder and the example application provided with theNewApplication documentation for usage examples.TypesThis package is built on top of tcell, which provides the types necessary tocreate a terminal-based application (e.g. EventKey). For information oninherited types see the tcell documentation.tcell: https://github.com/gdamore/tcellBase PrimitiveWidgets must implement the Primitive interface. All widgets embed the baseprimitive, Box, and thus inherit its functions. This isn't necessarilyrequired, but it makes more sense than reimplementing Box's functionality ineach widget.WidgetsThe following widgets are available:Button - Button which is activated when the user selects it.CheckBox - Selectable checkbox for boolean values.DropDown - Drop-down selection field.Flex - A Flexbox based layout manager.Form - Form composed of input fields, drop down selections, checkboxes, andbuttons.Grid - A grid based layout manager.InputField - Single-line text entry field.List - A navigable text list with optional keyboard shortcuts.Modal - A centered window with a text message and one or more buttons.Panels - A panel based layout manager.ProgressBar - Indicates the progress of an operation.TabbedPanels - Panels widget with tabbed navigation.Table - A scrollable display of tabular data. Table cells, rows, or columnsmay also be highlighted.TextView - A scrollable window that displays multi-colored text. Text mayalso be highlighted.TreeView - A scrollable display for hierarchical data. Tree nodes can behighlighted, collapsed, expanded, and more.Window - A draggable and resizable container.Widgets may be used without an application created via NewApplication, allowingthem to be integrated into any tcell-based application.ConcurrencyAll functions may be called concurrently (they are thread-safe). When calledfrom multiple threads, functions will block until the application or widgetbecomes available. Function calls may be queued with Application.QueueUpdate toavoid blocking.Unicode SupportThis package supports unicode characters including wide characters.Keyboard ShortcutsWidgets use keyboard shortcuts (a.k.a. keybindings) such as arrow keys andH/J/K/L by default. You may replace these defaults by modifying the shortcutslisted in Keys. You may also override keyboard shortcuts globally by setting ahandler with Application.SetInputCapture.cbind is a library which simplifies the process of adding support for customkeyboard shortcuts to your application. It allows setting handlers forEventKeys. It also translates between EventKeys and human-readable strings suchas "Alt+Enter". This makes it possible to store keybindings in a configurationfile.cbind: https://code.rocketnine.space/tslocum/cbindBracketed Paste ModeBracketed paste mode is enabled by default. It may be disabled by callingApplication.EnableBracketedPaste before Application.Run. The following demoshows how to handle paste events and process pasted text.tcell bracketed paste demo: https://github.com/gdamore/tcell/blob/master/_demos/mouse.goMouse SupportMouse support may be enabled by calling Application.EnableMouse beforeApplication.Run. See the example application provided with theApplication.EnableMouse documentation.Double clicks are treated single clicks by default. Specify a maximum durationbetween clicks with Application.SetDoubleClickInterval to enable double clicks.A standard duration is provided as StandardDoubleClick.Mouse events are passed to:- The handler set with SetMouseCapture, which is reserved for use by applicationdevelopers to permanently intercept mouse events. Return nil to stoppropagation.- The MouseHandler method of the topmost widget under the mouse.ColorsThroughout this package, colors are specified using the tcell.Color type.Functions such as tcell.GetColor(), tcell.NewHexColor(), and tcell.NewRGBColor()can be used to create colors from W3C color names or RGB values.Almost all strings which are displayed can contain color tags. Color tags areW3C color names or six hexadecimal digits following a hash tag, wrapped insquare brackets. Examples:This is a [red]warning[white]!The sky is [#8080ff]blue[#ffffff].A color tag changes the color of the characters following that color tag. Thisapplies to almost everything from box titles, list text, form item labels, totable cells. In a TextView, this functionality must be explicitly enabled. Seethe TextView documentation for more information.Color tags may contain not just the foreground (text) color but also thebackground color and additional flags. In fact, the full definition of a colortag is as follows:[<foreground>:<background>:<flags>]Each of the three fields can be left blank and trailing fields can be omitted.(Empty square brackets "[]", however, are not considered color tags.) Colorsthat are not specified will be left unchanged. A field with just a dash ("-")means "reset to default".You can specify the following flags (some flags may not be supported by yourterminal):l: blinkb: boldd: dimi: italicr: reverse (switch foreground and background color)u: underlines: strikethroughExamples:[yellow]Yellow text[yellow:red]Yellow text on red background[:red]Red background, text color unchanged[yellow::u]Yellow text underlined[::bl]Bold, blinking text[::-]Colors unchanged, flags reset[-]Reset foreground color[-:-:-]Reset everything[:]No effect[]Not a valid color tag, will print square brackets as they areIn the rare event that you want to display a string such as "[red]" or"[#00ff1a]" without applying its effect, you need to put an opening squarebracket before the closing square bracket. Note that the text inside thebrackets will be matched less strictly than region or colors tags. I.e. anycharacter that may be used in color or region tags will be recognized. Examples:[red[] will be output as [red]["123"[] will be output as ["123"][#6aff00[[] will be output as [#6aff00[][a#"[[[] will be output as [a#"[[][] will be output as [] (see color tags above)[[] will be output as [[] (not an escaped tag)You can use the Escape() function to insert brackets automatically where needed.Setting the background color of a primitive to tcell.ColorDefault will use thedefault terminal background color. To enable transparency (allowing one or moreprimitives to display behind a primitive) call SetBackgroundTransparent. Thescreen is not cleared before drawing the application. Overlaying transparentwidgets directly onto the screen may result in artifacts. To resolve this, adda blank, non-transparent Box to the bottom layer of the interface via Panels,or set a handler via SetBeforeDrawFunc which clears the screen.StylesWhen primitives are instantiated, they are initialized with colors taken fromthe global Styles variable. You may change this variable to adapt the look andfeel of the primitives to your preferred style.Scroll BarsScroll bars are supported by the following widgets: List, Table, TextView andTreeView. Each widget will display scroll bars automatically when there areadditional items offscreen. See SetScrollBarColor and SetScrollBarVisibility.Hello WorldThe following is an example application which shows a box titled "Greetings"containing the text "Hello, world!":package mainimport ("code.rocketnine.space/tslocum/cview")func main() {tv := cview.NewTextView()tv.SetText("Hello, world!").SetBorder(true).SetTitle("Greetings")if err := cview.NewApplication().SetRoot(tv, true).Run(); err != nil {panic(err)}}First, we create a TextView with a border and a title. Then we create anapplication, set the TextView as its root primitive, and run the event loop.The application exits when the application's Stop() function is called or whenCtrl-C is pressed.If we have a primitive which consumes key presses, we call the application'sSetFocus() function to redirect all key presses to that primitive. Mostprimitives then offer ways to install handlers that allow you to react to anyactions performed on them.DemosThe "demos" subdirectory contains a demo for each widget, as well as apresentation which gives an overview of the widgets and how they may be used.*/package cview
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |