Documentation
¶
Overview ¶
Package carousel shows one slide of several at a time, with a row of dots under it that says which. Left and Right go to the slide before and after, Home and End to the first and last. With the mouse on, a click on a dot goes to its slide and a click on either half of the slide goes back or on.
A slide is text the caller has already drawn, of any number of lines; its colours and attributes are kept and every other escape sequence is dropped.
Stability: experimental. Its API may change in any minor release.
Example ¶
Right shows the next slide and fills its dot.
package main
import (
"fmt"
"strings"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/carousel"
)
func main() {
tips := carousel.New("Press ? for help", "Press / to search", "Press q to quit")
tips.Focus()
tips, cmd := tips.Update(tui.Key{Type: tui.KeyRight})
// Each row is padded to the carousel's width, trimmed here for the
// comparison.
for _, row := range strings.Split(ansi.StripANSI(tips.View()), "\n") {
fmt.Println(strings.TrimSpace(row))
}
fmt.Println(cmd().(carousel.ChangedMsg).Index)
}
Output: Press / to search <○ ● ○> 1
Index ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) Index() int
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m *Model) SetIndex(i int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Size() (w, h int)
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangedMsg ¶
type ChangedMsg struct {
// ID is the carousel's ID.
ID string
// Index is the position in Slides of the slide now showing.
Index int
}
ChangedMsg is delivered (via the Cmd Update returns) when another slide shows, by key or by pointer.
type KeyMap ¶
type KeyMap struct {
Prev keymap.Binding // go to the slide before
Next keymap.Binding // go to the slide after
First keymap.Binding // go to the first slide
Last keymap.Binding // go to the last slide
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns Left and Right, and Home and End.
type Model ¶
type Model struct {
// ID is carried by ChangedMsg, to tell one carousel's change from
// another's.
ID string
// Slides are the slides, each already drawn by the caller.
Slides []string
// Width and Height are the size of the slide area in cells; a slide
// that is larger is cut, and one that is smaller is padded on the right
// and below. Zero or less fits the widest and the tallest slide, so the
// carousel keeps one size whichever slide shows. The width is never
// less than the row under the slide needs, which is at most 7 cells
// for up to nine slides.
Width, Height int
// Loop makes Right on the last slide go to the first, and Left on the
// first go to the last. Off (the default) stops at the ends.
Loop bool
Theme theme.Theme
// KeyMap holds the keys for each action. New fills it with DefaultKeyMap;
// a Model built as a struct literal with a zero KeyMap behaves as if it
// held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
// a dot goes to its slide, and one on the left or right half of the
// slide goes to the slide before or after. Off (the default) ignores
// the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the carousel.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one carousel. The zero value has no slides; build one with New.
func (Model) Bindings ¶
Bindings returns the actions the carousel honours, with descriptions, for help text.
func (*Model) Focus ¶
Focus gives the carousel keyboard focus. It returns no Cmd; the result is there so a carousel can stand where any focusable widget does.
func (Model) Index ¶
Index returns the position in Slides of the slide showing, or -1 when there are none.
func (Model) LayoutNode ¶
LayoutNode adapts the widget to a layout.Node of its own width and its Rows. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the carousel as plain text for accessible output (see tui.Linearizer): "Slide 2 of 3" and then the slide's own text without its styling. A carousel with no slides reads "No slides".
func (*Model) SetIndex ¶
SetIndex shows slide i, held within the slides there are. It delivers no ChangedMsg.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Size ¶
Size is the number of cells and of rows View takes: the slide area and the indicator row under it. Both are 0 with no slides.
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentCarousel, ...) and then by WithTokens.
func (Model) Update ¶
Update goes to another slide on Left, Right, Home and End when the carousel has focus. With Mouse on, a left press on a dot goes to its slide, and one on the slide goes back from its left half and on from its right.
func (Model) View ¶
View renders the slide showing, cut or padded to the slide area, and under it a centred row with a dot for each slide, the one showing filled: " ○ ● ○ ". A carousel that has focus puts that row in angle brackets, "<○ ● ○>", so focus reads without colour and does not change the width. With more slides than there is room for dots the row reads " 3 / 12 ". A carousel with no slides renders "".