carousel

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 New

func New(slides ...string) Model

New returns a carousel of the given slides, showing the first.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the carousel honours, with descriptions, for help text.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (*Model) Focus

func (m *Model) Focus() tui.Cmd

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) Focused

func (m Model) Focused() bool

Focused reports whether the carousel has keyboard focus.

func (Model) Index

func (m Model) Index() int

Index returns the position in Slides of the slide showing, or -1 when there are none.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the widget to a layout.Node of its own width and its Rows. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

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

func (m *Model) SetIndex(i int)

SetIndex shows slide i, held within the slides there are. It delivers no ChangedMsg.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

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

func (m Model) Size() (w, h int)

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

func (m Model) Tokens() theme.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

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

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

func (m Model) View() string

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 "".

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL