Main View Layout
Relevant source files
The following files were used as context for generating this wiki page:
The Main View Layout serves as the primary user interface orchestration layer for the Terminal Wizard. Built using the Bubble Tea framework and styled with Lip Gloss, it manages the lifecycle of a sequential questionnaire, handling state transitions between active questions and the final results display.
Sources: main.go:1-13, README.md:1-15
Architecture and Core Components
The layout is centered around the Main struct, which implements the tea.Model interface. It maintains the state of the wizard, including the current question index, the collection of questions, and terminal dimensions for responsive rendering.
Data Structures
| Component | Type | Description |
|---|---|---|
Main | struct | The root model managing application state, styles, and progress. |
Question | struct | Holds the prompt string, the user's answer, and the input component. |
Styles | struct | Defines the visual appearance of the interface using Lip Gloss. |
Sources: main.go:15-38
Visual Composition
The layout utilizes a vertical stack to present the user with a question and its corresponding input field. These elements are centered within the terminal window using the lipgloss.Place utility.
The diagram above illustrates how the View function constructs the visual hierarchy by joining elements vertically and centering them in the available terminal space.
Sources: main.go:94-113
State Management and Flow
The layout operates in three primary states: Loading, Active (Questioning), and Completed.
Interaction Flow
- Loading: Before the terminal dimensions are received via
tea.WindowSizeMsg, a "loading..." message is displayed. - Active: The model renders the current
Questionand delegates input handling to theInputinterface. - Completed: Upon answering the final question, the
doneflag is set to true, and the layout switches to a summary view.
Sources: main.go:70-90, input.go:9-18
Navigation Logic
Navigation is unidirectional. When the user submits an answer (via the enter key), the Next() method increments the internal index. If the wizard reaches the end of the question set, it resets or marks the session as finished.
func (m *Main) Next() {
if m.index < len(m.questions)-1 {
m.index++
} else {
m.index = 0
}
}
Sources: main.go:115-121
Dynamic Input Rendering
The layout supports polymorphic input types through the Input interface. This allows the Main view to render different UI components (like textinput or textarea) based on the question type without changing the core layout logic.
| Input Type | Underlying Component | Purpose |
|---|---|---|
shortAnswerField | textinput.Model | Single-line user input for names or brief identifiers. |
longAnswerField | textarea.Model | Multi-line input for quotes, descriptions, or long-form text. |
Sources: input.go:20-112, main.go:44-59
Summary Rendering
Once the done state is reached, the standard layout is bypassed in favor of a simple list. This view iterates through the questions slice and prints each question paired with its captured answer.
if m.done {
var output string
for _, q := range m.questions {
output += fmt.Sprintf("%s: %s\n", q.question, q.answer)
}
return output
}
Sources: main.go:96-103
The Main View Layout effectively decouples visual styling and state management from the specific input mechanisms, providing a flexible framework for terminal-based wizards.