Keybinding Handlers
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Keybinding Handlers in this project are implemented within the Update method of the main Bubble Tea model. Their primary purpose is to capture and process user keyboard input to navigate through the terminal wizard, manage state transitions between questions, and facilitate application termination. The handlers provide a mechanism for translating raw terminal key events into specific application actions such as submitting an answer or quitting the program.
Sources: main.go:81-100, README.md:1-10
Architecture and Input Processing
The application uses a centralized message-handling loop where tea.KeyMsg types are evaluated. When a key is pressed, the Update function switches on the string representation of the key to determine the appropriate response. Input handling is split between global application shortcuts and specific field-level interactions.
Global Keybindings
Global keybindings are reserved for application-level control flow. These are intercepted before the message is passed down to specific input components.
| Key | Action | Implementation Detail |
|---|---|---|
ctrl+c | Quit | Returns tea.Quit command |
q | Quit | Returns tea.Quit command |
enter | Submit / Next | Saves current answer and increments question index |
Sources: main.go:87-97
Navigation and State Flow
The enter key triggers a specific sequence of logic. It evaluates whether the user is on the final question to set the done state, captures the current input value, and advances to the next question using the Next() method.
The flow diagram above illustrates the decision logic within the keybinding handler.
Sources: main.go:85-100
Input Component Handling
If a keypress does not match a global handler (like enter or q), it is delegated to the Update method of the active Input interface. This allows individual components like shortAnswerField and longAnswerField to handle character typing, backspacing, and cursor movement internally.
Interface Implementation
The Input interface ensures that regardless of whether the user is typing in a textinput or a textarea, the key messages are handled consistently.
The sequence diagram shows how key messages are delegated from the main application loop down to specific input components.
Sources: input.go:7-16, main.go:99-100
Component Specifics
- Short Answer: Wraps
textinput.Model. Focuses on single-line key handling. - Long Answer: Wraps
textarea.Model. Focuses on multi-line text entry and navigation.
Sources: input.go:21-23, input.go:68-70
Key Functions and Methods
| Function | File | Description |
|---|---|---|
Update(msg tea.Msg) | main.go:81 | The primary entry point for all keybinding logic. |
Next() | main.go:121 | Manages the index transition when enter is pressed. |
Value() | input.go:64, input.go:111 | Retrieves the string content from an input after a keybinding action. |
Blur() | input.go:60, input.go:107 | Returns a command to stop focusing an input, triggered after enter. |
Sources: main.go:81-127, input.go:7-115
Summary
The Keybinding Handlers in the wizard tutorial provide a streamlined way to manage terminal interactions. By combining global shortcuts (Quit, Submit) with delegated component-level input handling, the system maintains a clear separation between application flow and text entry logic. This ensures that the user can navigate the multi-step wizard intuitively using standard terminal conventions.