Application Lifecycle
Relevant source files
The following files were used as context for generating this wiki page:
The Application Lifecycle of the Wizard Tutorial project governs the initialization, execution, and termination of a terminal-based multi-step wizard. Built using the Bubble Tea framework, the application follows The Elm Architecture (TEA), where the lifecycle is driven by three primary methods: Init, Update, and View.
The application manages a sequence of questions, each associated with a specific input component (short text or long text), and transitions through these states based on user interaction until a final summary is displayed.
Sources: README.md:1-15, main.go:107-124
Program Initialization
The lifecycle begins in the main function, where the application state is configured. This involves defining the sequence of questions and initializing the primary Main model.
Bootstrapping Process
- Question Definition: A slice of
Questionstructs is created using constructor functions (newShortQuestion,newLongQuestion). - Model Instantiation: The
Newfunction initializes theMainstruct with default styles and the question set. - Logging: A debug log file is initialized via
tea.LogToFile. - Program Start: A
tea.Programis created with theMainmodel and theAltScreenoption, then executed usingp.Run().
Sources: main.go:107-124
Component Initialization Flow
The following diagram illustrates the startup sequence of the wizard:
The startup sequence ensures that the terminal environment is prepared and the first input component begins its internal lifecycle (e.g., cursor blinking).
Sources: main.go:57-59, main.go:107-124, input.go:37-39
Core Runtime Loop (Update & View)
Once the program is running, it enters a loop managed by the Bubble Tea framework. The Update function handles messages (events), while the View function renders the UI based on the current state.
Message Handling
The Update function processes several types of messages:
- WindowSizeMsg: Updates the model's width and height to ensure proper centering of the UI.
- KeyMsg:
ctrl+c/q: Triggerstea.Quitto terminate the lifecycle.enter: Captures the current input value, stores it in theQuestionstruct, and transitions to the next question.
Sources: main.go:61-82
State Transitions
The application transitions through questions using the Next() method. When the final question is answered, the done flag is set to true, altering the lifecycle stage from "Data Entry" to "Summary".
This sequence shows how user input triggers a state change within the Main model and coordinates with the underlying input component.
Sources: main.go:70-80, main.go:99-105
Component Lifecycle: Input Interface
The wizard abstracts different input types (short and long) through an Input interface. Each component must implement specific lifecycle methods.
| Method | Description | Source |
|---|---|---|
Blink() | Returns a command to initiate cursor blinking. | input.go:11 |
Focus() | Returns a command to give the component focus. | input.go:13 |
Blur() | Returns a message indicating the component has lost focus. | input.go:12 |
Update(msg) | Processes messages specific to the internal component logic. | input.go:16 |
View() | Renders the specific input field (textinput or textarea). | input.go:17 |
Termination and Cleanup
The application lifecycle concludes in two ways:
- User Interruption: Pressing
qorctrl+creturns atea.Quitcommand, which restores the terminal state and exits the process. - Completion: Upon setting
m.done = true, theViewfunction switches from rendering the wizard UI to rendering a summary of all questions and answers.
func (m Main) View() string {
current := m.questions[m.index]
if m.done {
var output string
for _, q := range m.questions {
output += fmt.Sprintf("%s: %s\n", q.question, q.answer)
}
return output
}
// ... rendering logic for active wizard
}
Sources: main.go:85-93, main.go:72-74
Data Structure Overview
The lifecycle state is maintained primarily within the Main and Question structs.
| Struct | Field | Type | Description |
|---|---|---|---|
Main | index | int | Current question being displayed. |
Main | questions | []Question | The full list of wizard steps. |
Main | done | bool | Flag indicating the wizard is finished. |
Question | answer | string | The value captured from the user input. |
Question | input | Input | The UI component for this specific question. |
Sources: main.go:25-32, main.go:34-38
The application lifecycle effectively manages the transition from a dynamic, interactive input phase to a static summary phase, ensuring all terminal resources are handled correctly via the underlying framework.