Documentation
¶
Overview ¶
Package breadcrumb is a trail of the places that lead to the current one, "Home / Docs / Guide", that can be walked back along: Left and Right move a cursor between the places, and Enter or Space chooses the one it is on. With the mouse on, so does a click.
Choosing reports the place and changes nothing: the caller owns the trail and shortens it, or goes elsewhere, as it sees fit.
Stability: experimental. Its API may change in any minor release.
Example ¶
Left moves back along the trail; Enter chooses the place.
package main
import (
"fmt"
"strings"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/breadcrumb"
)
func main() {
path := breadcrumb.New("Home", "Docs", "Guide")
path.Focus()
path, _ = path.Update(tui.Key{Type: tui.KeyLeft})
// The row has a blank cell at each end, trimmed here for the comparison.
fmt.Println(strings.TrimSpace(ansi.StripANSI(path.View())))
_, cmd := path.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(breadcrumb.ChosenMsg).Label)
}
Output: Home /<Docs>/ Guide Docs
Index ¶
- type ChosenMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Cursor() int
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m *Model) SetCursor(i int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) Width() int
- 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 ChosenMsg ¶
type ChosenMsg struct {
// ID is the trail's ID.
ID string
// Index is the chosen place's position in Items.
Index int
// Label is the chosen place's text.
Label string
}
ChosenMsg is delivered (via the Cmd Update returns) when a place is chosen, by key or by pointer. Choosing the last place, the current one, is reported like any other.
type KeyMap ¶
type KeyMap struct {
Prev keymap.Binding // move to the place before
Next keymap.Binding // move to the place after
First keymap.Binding // move to the first place
Last keymap.Binding // move to the last place
Choose keymap.Binding // choose the place under the cursor
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns Left and Right, Home and End, and Enter or Space.
type Model ¶
type Model struct {
// ID is carried by ChosenMsg, to tell one trail's choice from another's.
ID string
// Items are the places, from the root to the current one, which is the
// last.
Items []string
// Separator stands between two places. Empty is read as "/".
Separator string
// Max, when above 1, is the most places drawn: a longer trail keeps its
// first place and its last Max-1, with an ellipsis for the ones between.
// The folded places cannot be chosen.
Max int
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 place chooses it. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the trail.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one trail. The zero value is an empty trail; build one with New.
func (Model) Bindings ¶
Bindings returns the actions the trail honours, with descriptions, for help text.
func (Model) Cursor ¶
Cursor returns the index in Items of the place the cursor is on: the last place until a key or a click has moved it. It is -1 for an empty trail.
func (*Model) Focus ¶
Focus gives the trail keyboard focus. It returns no Cmd; the result is there so a trail can stand where any focusable widget does.
func (Model) LayoutNode ¶
LayoutNode adapts the widget to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the trail as plain text for accessible output (see tui.Linearizer): one line per place with its position, and ", current" on the last, e.g. "Docs, place 2 of 3". Every place is listed, the folded ones too. An empty trail reads "No places".
func (*Model) SetCursor ¶
SetCursor moves the cursor to place i. An index that is out of range, or names a place folded into the ellipsis, is ignored.
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) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentBreadcrumb, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the cursor on Left, Right, Home and End, stopping at the ends, and chooses the place under it on Enter or Space, when the trail has focus. With Mouse on, a left press on a place moves the cursor to it and chooses it.
func (Model) View ¶
View renders the places on one row with the separator between them, each with a blank cell either side: " Home / Docs / Guide ". The last place, the current one, is bold. The place under the cursor of a trail that has focus takes angle brackets in its two cells, " Home /<Docs>/ Guide ", so focus reads without colour and does not change the width. An empty trail renders "".