Introduction
Relevant source files
The following files were used as context for generating this wiki page:
The Terminal Wizard is a Go-based command-line interface (CLI) application designed to facilitate sequential user input through a series of structured questions. Built using the Charm ecosystem, specifically the bubbletea TUI framework and lipgloss for styling, the project demonstrates a model-view-update architecture for terminal interactions.
Sources: README.md:1-21, main.go:1-12
The system manages a collection of question objects, each utilizing a specific input component (short text or long-form textarea). It handles window resizing, keyboard navigation, and state transitions until all questions are answered, at which point it displays a summary of the collected data.
Sources: main.go:48-52, main.go:76-104
Core Architecture
The application follows the Elm-inspired architecture of the bubbletea library, consisting of a central Main model that manages state, updates based on messages, and renders the user interface.
The Main Model
The Main struct serves as the primary state container. It tracks the current question index, the list of questions, terminal dimensions, and the completion status.
| Field | Type | Description |
|---|---|---|
styles | *Styles | Visual configuration for borders and padding. |
index | int | Current active question index in the sequence. |
questions | []Question | Slice of all defined question objects. |
width/height | int | Terminal window dimensions for responsive centering. |
done | bool | Flag indicating if all questions have been completed. |
Sources: main.go:27-34
Data Flow and State Transitions
The application transitions through states based on user keyboard input, specifically handling the enter key to move between questions.
Sources: main.go:76-104, main.go:113-120
Input System and Interface
The project defines an Input interface to abstract different types of terminal input components. This allows the Main model to interact with diverse input fields (like single-line text or multi-line areas) through a consistent API.
Input Interface
The Input interface requires the implementation of standard bubbletea methods along with value retrieval and focus management.
Sources: input.go:9-17
Implementation Details
- Short Answer: Wraps
textinput.Modelfor single-line responses. Sources: input.go:23-25 - Long Answer: Wraps
textarea.Modelfor multi-line responses. Sources: input.go:72-74
Component Styling
Visual presentation is managed via lipgloss, providing a consistent look for the wizard.
Styles Configuration
The Styles struct defines the visual boundaries and layout of the input fields.
| Component | Style Attribute | Value/Configuration |
|---|---|---|
| Border Color | lipgloss.Color | "36" (Teal/Cyan) |
| Input Field | BorderStyle | NormalBorder |
| Input Field | Padding | 1 |
| Input Field | Width | 80 characters |
Sources: main.go:14-25
UI Layout Logic
The View function uses lipgloss.Place to dynamically center the content within the terminal window based on current m.width and m.height values.
return lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Left,
current.question,
m.styles.InputField.Render(current.input.View()),
),
)
Sources: main.go:121-133
Summary of Execution
The application lifecycle begins by initializing a slice of Question objects (name, favorite editor, and favorite quote). It runs within an "AltScreen" to provide a full-terminal experience and logs debug information to debug.log. When the user completes the final question, the Update loop sets m.done to true, prompting the View function to iterate through the question slice and print all collected answers in a vertical list.
Sources: main.go:141-160, main.go:106-111