Wizard Step Logic
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Wizard Step Logic represents the core state management and transition system for a terminal-based multi-step questionnaire. Built using the Bubble Tea framework, this system allows users to navigate through a sequence of input fields, capturing data in a structured format before displaying a final summary. It utilizes Lip Gloss for layout and terminal styling.
Sources: README.md:1-12, main.go:25-32
Core Architecture
The system is architected around a central Main model that orchestrates a collection of Question objects. Each question acts as an individual "step" in the wizard, containing its own prompt, stored answer, and a specialized input component.
Data Structures
| Structure | Description | Key Fields |
|---|---|---|
Main | The primary state machine managing the wizard lifecycle. | index (current step), questions (slice of steps), done (completion status) |
Question | Represents a single step in the wizard. | question (string), answer (string), input (Input interface) |
Styles | Defines visual appearance using Lip Gloss. | BorderColor, InputField |
Sources: main.go:13-23, main.go:25-36
Input Interface and Components
The wizard uses an Input interface to abstract different types of terminal inputs (short text vs. long text). This allows the main logic to remain decoupled from the specific implementation of the UI components.
Sources: input.go:9-18, input.go:24-26, input.go:73-75
Step Transition Logic
Transitions between steps occur primarily through the Update method of the Main model. When a user submits an answer (typically by pressing "enter"), the system captures the current input value, moves to the next index, and shifts focus to the new input component.
Transition Flow
The following diagram illustrates the sequence of events when a user moves from one question to the next.
Sources: main.go:72-87, main.go:108-114
State Management
The wizard tracks progress using a zero-based index. The Next() function increments this index until the end of the question slice is reached.
func (m *Main) Next() {
if m.index < len(m.questions)-1 {
m.index++
} else {
m.index = 0
}
}
Sources: main.go:108-114
Component Initialization
The wizard is initialized with a slice of predefined questions. The system supports different question types via factory functions that wrap Bubble Tea components like textinput and textarea.
| Function | Purpose | Implementation Detail |
|---|---|---|
newShortQuestion | Creates a single-line input step. | Uses textinput with a placeholder. |
newLongQuestion | Creates a multi-line input step. | Uses textarea with a placeholder. |
DefaultStyles | Sets up the UI theme. | Uses color "36" and a normal border style. |
Sources: main.go:38-56, input.go:28-36, input.go:77-85
View Rendering and Completion
The View() method handles conditional rendering based on whether the wizard is in progress or completed.
- In-Progress: The current question and its associated input field are joined vertically and centered in the terminal window using
lipgloss.Place. - Completed: When
m.doneis true, the system iterates through allquestionsand prints the final key-value pairs of questions and answers.
Sources: main.go:89-106
Conclusion
The Wizard Step Logic provides a robust foundation for building interactive CLI tools. By decoupling the step management (Main model) from the input implementation (Input interface), the system remains extensible, allowing for new types of wizard steps to be added with minimal changes to the core navigation logic.
Sources: main.go:116-130, input.go:9-18