wizard

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package wizard provides a step-navigation state machine for multi-step flows: Next/Back advance or retreat the current step (clamped at the bounds), and View renders the step indicator via widgets.Stepper. It pairs naturally with widgets.Form/FormField for per-step validation errors, but owns no field state itself — the caller keeps that.

Example

Next moves to the following step only when the step's validation passes.

package main

import (
	"errors"
	"fmt"

	"github.com/ows4444/tui/wizard"
)

func main() {
	m := wizard.New("account", "profile", "done")
	err := m.Next(func() error { return errors.New("name is required") })
	fmt.Println(m.Current(), err)
	err = m.Next(func() error { return nil })
	fmt.Println(m.Current(), err)
	m.Back()
	fmt.Println(m.Current())
}
Output:
0 name is required
1 <nil>
0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model holds the ordered step titles and which one is current. Like the rest of this library, it's a plain value the caller stores and mutates via the returned Model — there's no hidden state.

func New

func New(titles ...string) Model

New constructs a Model at step 0 for the given titles.

func (*Model) Back

func (m *Model) Back()

Back retreats the current step by one, clamped at 0.

func (Model) Current

func (m Model) Current() int

Current returns the current step index. For a zero-step Model it returns 0.

func (Model) LayoutNode

func (m Model) LayoutNode(t theme.Theme) layout.Node

LayoutNode adapts the step bar to a layout.Node, drawn with theme t (as View is). Measure reports its natural size, one row. When the bar is wider than the allotted width, Render scrolls it so the current step is visible. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the steps as plain text for accessible output (see tui.Linearizer): one line per step, "<title>, step K of N, completed", "current" or "upcoming". No connectors or markers.

func (*Model) Next

func (m *Model) Next(validate func() error) error

Next runs validate; if it returns a non-nil error, the step does not advance and the error is returned unchanged. If validate returns nil, the current step advances by one, clamped so it never exceeds the last step index.

func (Model) Tokens

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

Tokens returns the colour tokens the step bar renders with under t: t's roles, overridden by any theme.WithTokens(theme.ComponentWizard, ...) and then by WithTokens.

func (Model) View

func (m Model) View(t theme.Theme) string

View renders the step indicator via widgets.Stepper, with t resolved for theme.ComponentWizard and the tokens set by WithTokens.

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 passed to View, so only the roles tok names change. A second call replaces the first.

Jump to

Keyboard shortcuts

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