breadcrumb

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

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 New

func New(items ...string) Model

New returns a trail of the given places, the last being the current one.

func (Model) Bindings

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

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

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (Model) Cursor

func (m Model) Cursor() int

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

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

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

func (m Model) Focused() bool

Focused reports whether the trail has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the widget to a layout.Node of its own width on one row. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

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

func (m *Model) SetCursor(i int)

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

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) 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.ComponentBreadcrumb, ...) and then by WithTokens.

func (Model) Update

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

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

func (m Model) View() string

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

func (Model) Width

func (m Model) Width() int

Width is the number of cells View takes: each entry with a cell either side of it, and the separators between.

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