Adding Wizard Steps
Relevant source files
The following files were used as context for generating this wiki page:
The wizard system is built using the Bubble Tea framework to create interactive terminal-based step-by-step forms. "Adding Wizard Steps" refers to the process of defining new questions and associating them with specific input types (short or long text) to capture user data sequentially.
The system utilizes a central Main model to manage a collection of Question objects. Each step in the wizard corresponds to a single Question, which contains the prompt text, the user's response, and an underlying Input interface implementation that handles the rendering and logic of the input field.
Sources: main.go:24-40, README.md:1-15
Core Data Structures
The wizard's architecture relies on three primary structures to manage state and individual steps.
The Question Struct
The Question struct is the fundamental unit of a wizard step. It encapsulates the prompt, the storage for the final answer, and the interactive input component.
| Field | Type | Description |
|---|---|---|
question | string | The text prompt displayed to the user. |
answer | string | The value captured from the input field upon completion. |
input | Input | An interface implementation (Short or Long) for data entry. |
Sources: main.go:34-38
The Main Model
The Main struct manages the orchestration of the wizard, including the current progress and styling.
| Field | Type | Description |
|---|---|---|
index | int | The zero-based index of the current active question. |
questions | []Question | A slice containing all defined steps in the wizard. |
done | bool | A flag indicating all steps are completed. |
styles | *Styles | Visual configuration using Lip Gloss. |
Sources: main.go:24-32
Defining Input Types
The system abstracts input behavior through an Input interface, allowing for different types of text entry (e.g., single-line vs. multi-line).
The interface ensures that the Main model can interact with any input type uniformly during the Update and View cycles.
Sources: input.go:7-16, input.go:62-64
Input Implementation Comparison
| Feature | Short Answer (textinput) | Long Answer (textarea) |
|---|---|---|
| Constructor | NewShortAnswerField() | NewLongAnswerField() |
| Usage | Single-line strings (e.g., Name) | Multi-line text (e.g., Quotes) |
| Focus | Auto-focused on init | Auto-focused on init |
Sources: input.go:18-30, input.go:66-78
Step Progression Logic
The wizard progresses through steps by incrementing the index in the Main model when an "enter" key event is detected.
When the user submits an answer, the Next() method handles the index increment. If the current index is the last in the questions slice, the wizard marks itself as done to trigger the final summary view.
Sources: main.go:78-89, main.go:111-117
Implementation Example
To add steps to the wizard, instances of Question are created using helper functions and passed to the New constructor in the main function.
func main() {
questions := []Question{
newShortQuestion("what is your name?"),
newShortQuestion("what is your favourite editor?"),
newLongQuestion("what's your favourite quote?"),
}
main := New(questions)
// ... program execution
}
Sources: main.go:119-126
Summary of Execution Flow
The following sequence diagram illustrates how a single step is handled within the Bubble Tea Update loop.
The Main model acts as a proxy, forwarding messages to the active question's input field and capturing the value only when the step is submitted.
Sources: main.go:73-93, input.go:54-60