### Minimal End-to-End Example Source: https://github.com/yarlson/tap/blob/main/README.LLM.md A complete, runnable example demonstrating the integration of text prompts, validation, confirmation, and output messages within a Go program using the tap library. ```go package main import ( "context" "fmt" "strings" "errors" "github.com/yarlson/tap" ) func main() { ctx := context.Background() name := tap.Text(ctx, tap.TextOptions{Message: "Name:"}) email := tap.Text(ctx, tap.TextOptions{ Message: "Email:", Validate: func(s string) error { if !strings.Contains(s, "@") { return errors.New("please enter a valid email") } return nil }, }) ok := tap.Confirm(ctx, tap.ConfirmOptions{Message: fmt.Sprintf("Submit for %s?", name)}) if ok { tap.Outro(fmt.Sprintf("Saved %s", email)) } } ``` -------------------------------- ### Spinner, Progress Bar, and Stream Output Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Provides examples for using `Spinner`, `Progress`, and `Stream` to provide visual feedback during long-running operations. Includes starting, updating, and stopping these elements, along with options for customization. ```go sp := tap.NewSpinner(tap.SpinnerOptions{Indicator: "dots"}) sp.Start("Connecting") // ... do work ... sp.Stop("Connected", 0) pr := tap.NewProgress(tap.ProgressOptions{Style: "heavy", Max: 100, Size: 40}) pr.Start("Processing…") for i := 0; i < 10; i++ { pr.Advance(10, fmt.Sprintf("%d/10", i+1)) } pr.Stop("Complete", 0) st := tap.NewStream(tap.StreamOptions{ShowTimer: true}) st.Start("Building project") st.WriteLine("step 1: deps") st.WriteLine("step 2: compile") st.Stop("Done", 0) ``` -------------------------------- ### Install Tap CLI Source: https://github.com/yarlson/tap/blob/main/README.md Installs the latest version of the Tap library for Go projects using the go get command. ```bash go get github.com/yarlson/tap@latest ``` -------------------------------- ### Go Quick Start Example Source: https://github.com/yarlson/tap/blob/main/README.md A basic Go program demonstrating Tap's usage for displaying an intro message, prompting for user input, and showing a confirmation prompt. ```go package main import ( "context" "fmt" "github.com/yarlson/tap" ) func main() { ctx := context.Background() tap.Intro("Welcome! 👋") name := tap.Text(ctx, tap.TextOptions{ Message: "What's your name?", Placeholder: "Enter your name...", }) confirmed := tap.Confirm(ctx, tap.ConfirmOptions{ Message: fmt.Sprintf("Hello %s! Continue?", name), }) if confirmed { tap.Outro("Let's go! 🎉") } } ``` -------------------------------- ### Running Tap Examples Source: https://github.com/yarlson/tap/blob/main/README.md These bash commands show how to run the example applications provided with the Tap library. Each command demonstrates a different interactive terminal feature, such as text input, selection menus, progress bars, and a complete workflow. ```bash go run examples/text/main.go # Text input ``` ```bash go run examples/select/main.go # Selection menus ``` ```bash go run examples/progress/main.go # Progress bars ``` ```bash go run examples/multiple/main.go # Complete workflow ``` -------------------------------- ### Tap Intro and Outro Messages in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Demonstrates the use of tap.Intro and tap.Outro for displaying introductory titles and concluding messages in the terminal. ```go func tap.Intro(title string) func tap.Outro(message string) ``` -------------------------------- ### Import Tap Library in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Demonstrates how to import the necessary packages from the Tap library for use in a Go program. This includes context, fmt, and the Tap library itself. ```go import ( "context" "fmt" "github.com/yarlson/tap" ) ``` -------------------------------- ### Tap Progress Bar in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Shows how to implement and control a terminal progress bar. Includes options for style, max value, and size, with methods to start, advance, update messages, and stop. ```go type tap.ProgressOptions struct { Style string; Max, Size int } type tap.Progress struct { /* unexported */ } func tap.NewProgress(opts tap.ProgressOptions) *tap.Progress func (p *tap.Progress) Start(msg string) func (p *tap.Progress) Advance(step int, msg string) func (p *tap.Progress) Message(msg string) func (p *tap.Progress) Stop(msg string, code int) ``` -------------------------------- ### Tap Context Handling in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Explains the importance of context.Context for Tap prompts, enabling cancellation and timeouts. Shows how cancelled contexts result in zero values. ```go // Use context.Background() for basic usage, or pass custom contexts for cancellation/timeouts // If context is cancelled, prompts return zero values (empty string, false, etc.) ``` -------------------------------- ### Tap Spinner Management in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Provides methods for creating and managing a terminal spinner. Includes starting, updating messages, and stopping the spinner with status codes. ```go type tap.SpinnerOptions struct { Indicator string; Frames []string; Delay time.Duration; CancelMessage, ErrorMessage string } type tap.Spinner struct { /* unexported */ } func tap.NewSpinner(opts tap.SpinnerOptions) *tap.Spinner func (s *tap.Spinner) Start(msg string) func (s *tap.Spinner) Message(msg string) func (s *tap.Spinner) Stop(msg string, code int) func (s *tap.Spinner) IsCanceled() bool ``` -------------------------------- ### Tap Stream Output in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Demonstrates how to create and manage a live output stream in the terminal. Supports showing a timer, writing lines, piping reader input, and stopping the stream. ```go type tap.StreamOptions struct { ShowTimer bool } type tap.Stream struct { /* unexported */ } func tap.NewStream(opts tap.StreamOptions) *tap.Stream func (s *tap.Stream) Start(msg string) func (s *tap.Stream) WriteLine(line string) func (s *tap.Stream) Pipe(r io.Reader) func (s *tap.Stream) Stop(msg string, code int) ``` -------------------------------- ### Tap Text Input Prompt in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Shows how to use the tap.Text function to get string input from the user. It includes options for message, placeholder, default value, initial value, and validation. ```go func tap.Text(ctx context.Context, opts tap.TextOptions) string ``` -------------------------------- ### Basic Text, Confirm, and Outro Prompts Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Demonstrates the fundamental usage of the tap library for gathering user input via text prompts and confirmation dialogs, and displaying output messages. It uses `context.Background()` for managing request lifecycles. ```go ctx := context.Background() name := tap.Text(ctx, tap.TextOptions{Message: "What is your name?"}) lang := tap.Text(ctx, tap.TextOptions{Message: fmt.Sprintf("Hi %s! Favorite language?", name)}) proceed := tap.Confirm(ctx, tap.ConfirmOptions{Message: "Proceed?", InitialValue: true}) if proceed { tap.Outro("Let's go! 🎉") } ``` -------------------------------- ### Tap Box Formatting in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Illustrates how to create formatted boxes with titles and messages using tap.Box. Includes options for alignment, padding, borders, and rounded corners. ```go type tap.BoxOptions struct { Columns int; WidthFraction float64; WidthAuto bool; TitlePadding, ContentPadding int; TitleAlign, ContentAlign tap.BoxAlignment; Rounded, IncludePrefix bool; FormatBorder func(string) string } func tap.Box(message string, title string, opts tap.BoxOptions) func tap.GrayBorder(s string) string func tap.CyanBorder(s string) string ``` -------------------------------- ### Tap Confirmation Prompt in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Demonstrates the tap.Confirm function to get a boolean confirmation from the user. Options include message, active/inactive labels, and initial value. ```go func tap.Confirm(ctx context.Context, opts tap.ConfirmOptions) bool ``` -------------------------------- ### Tap Select Prompt in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Shows how to use the generic tap.Select function for single-item selection from a list. It supports custom types for options and initial values. ```go type tap.SelectOption[T any] struct { Value T; Label, Hint string } type tap.SelectOptions[T any] struct { Message string; Options []tap.SelectOption[T]; InitialValue *T; MaxItems *int } func tap.Select[T any](ctx context.Context, opts tap.SelectOptions[T]) T ``` -------------------------------- ### Testing Terminal I/O Overrides Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Explains how to test interactive terminal prompts by overriding the standard input and output streams with mock implementations. This allows feeding predefined input and asserting the output. ```go in := core.NewMockReadable() out := core.NewMockWritable() tap.SetTermIO(in, out) defer tap.SetTermIO(nil, nil) _ = tap.Text(ctx, tap.TextOptions{Message: "Your name:"}) // feed input in.EmitKeypress("A", core.Key{Name: "a"}) in.EmitKeypress("", core.Key{Name: "return"}) // assert frames in out.Buffer ``` -------------------------------- ### Multi-Select Prompt with Typed Values Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Demonstrates the `MultiSelect` prompt, enabling users to choose multiple options from a list. It supports generic types for values and allows pre-selecting initial values. ```go langs := tap.MultiSelect(ctx, tap.MultiSelectOptions[string]{ Message: "Choose languages:", Options: []tap.SelectOption[string]{ {Value: "go", Label: "Go"}, {Value: "py", Label: "Python"}, {Value: "js", Label: "JavaScript"}, }, InitialValues: []string{"go"}, }) fmt.Println(langs) // []string{"go", ...} ``` -------------------------------- ### Go Context Support and Cancellation Source: https://github.com/yarlson/tap/blob/main/README.md Illustrates how to integrate Go's context package with Tap prompts for timeouts and cancellation. Examples show setting a timeout for a text prompt and cancelling a confirm prompt after a delay. ```go // With timeout ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() name := tap.Text(ctx, tap.TextOptions{ Message: "Enter your name (30s timeout):", }) // With cancellation ctx, cancel := context.WithCancel(context.Background()) go func() { time.Sleep(5*time.Second) cancel() // Cancel after 5 seconds }() result := tap.Confirm(ctx, tap.ConfirmOptions{ Message: "Quick decision needed:", }) ``` -------------------------------- ### Tap Multi-Select Prompt in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Illustrates the generic tap.MultiSelect function for selecting multiple items from a list. It allows specifying initial selections and a maximum number of items. ```go type tap.MultiSelectOptions[T any] struct { Message string; Options []tap.SelectOption[T]; InitialValues []T; MaxItems *int } func tap.MultiSelect[T any](ctx context.Context, opts tap.MultiSelectOptions[T]) []T ``` -------------------------------- ### Tap Password Input Prompt in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Illustrates using the tap.Password function for masked input. It shares the same options as tap.Text, ensuring user input is hidden. ```go func tap.Password(ctx context.Context, opts tap.PasswordOptions) string ``` -------------------------------- ### Select Prompt with Typed Values Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Illustrates using the `Select` prompt with generic type support for options. This allows defining custom types for selection values, enhancing type safety and code clarity. ```go type Env string env := tap.Select(ctx, tap.SelectOptions[Env]{ Message: "Choose environment:", Options: []tap.SelectOption[Env]{ {Value: "dev", Label: "Development", Hint: "Local"}, {Value: "prod", Label: "Production", Hint: "Live"}, }, }) ``` -------------------------------- ### Tap Input Validation in Go Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Details how to implement input validation for text and password prompts using a Validate function. Non-nil errors prevent submission and display an error message. ```go // Provide Validate: func(string) error { ... } // If non-nil error is returned on submit, the prompt stays active and shows a yellow error line ``` -------------------------------- ### Go Styled Messages Source: https://github.com/yarlson/tap/blob/main/README.md Shows how to display styled messages using Tap's Box component in Go. This example creates a message box with rounded borders, custom border colors, and centered title and content alignment. ```go // Message box with custom styling tap.Box("This is important information!", "⚠️ Warning", tap.BoxOptions{ Rounded: true, FormatBorder: tap.CyanBorder, TitleAlign: tap.BoxAlignCenter, ContentAlign: tap.BoxAlignCenter, }) ``` -------------------------------- ### Text Input with Validation Source: https://github.com/yarlson/tap/blob/main/README.LLM.md Shows how to implement custom validation for text input using a provided function. The validation function receives the input string and must return an error if the input is invalid, preventing submission. ```go email := tap.Text(ctx, tap.TextOptions{ Message: "Enter email:", Validate: func(s string) error { if !strings.Contains(s, "@") { return errors.New("please enter a valid email") } return nil }, }) ``` -------------------------------- ### Mock Terminal I/O for Go Tests Source: https://github.com/yarlson/tap/blob/main/README.md This Go code snippet demonstrates how to use Tap's testing utilities to mock terminal input and output. It shows how to create mock readers and writers, override the terminal I/O, simulate keypresses, and assert the output of a prompt function. The `defer tap.SetTermIO(nil, nil)` ensures that the original terminal I/O is restored after the test. ```go func TestYourPrompt(t *testing.T) { // Create mock I/O mockInput := tap.NewMockReadable() mockOutput := tap.NewMockWritable() // Override terminal I/O for testing tap.SetTermIO(mockInput, mockOutput) defer tap.SetTermIO(nil, nil) // Simulate user input go func() { mockInput.EmitKeypress("test", tap.Key{Name: "t"}) mockInput.EmitKeypress("", tap.Key{Name: "return"}) }() result := tap.Text(ctx, tap.TextOptions{Message: "Enter text:"}) assert.Equal(t, "test", result) } ``` -------------------------------- ### Running Go Tests Source: https://github.com/yarlson/tap/blob/main/README.md These bash commands illustrate how to execute Go tests for the Tap project. The first command runs all tests in the project, while the second command runs tests with the race detector enabled, which is useful for identifying race conditions. ```bash go test ./... ``` ```bash go test -race ./... # with race detection ``` -------------------------------- ### Go Progress Indicators Source: https://github.com/yarlson/tap/blob/main/README.md Shows how to implement progress bars and spinners using Tap in Go. It covers configuring progress bar styles, sizes, and updating progress with messages, as well as basic spinner usage. ```go // Progress Bar progress := tap.NewProgress(tap.ProgressOptions{ Style: "heavy", // "light", "heavy", or "block" Max: 100, Size: 40, }) progress.Start("Processing...") for i := 0; i <= 100; i += 10 { time.Sleep(200 * time.Millisecond) progress.Advance(10, fmt.Sprintf("Step %d/10", i/10+1)) } progress.Stop("Complete!", 0) // Spinner spinner := tap.NewSpinner(tap.SpinnerOptions{}) spinner.Start("Loading...") // ... do work ... spinner.Stop("Done!", 0) ``` -------------------------------- ### Go Text Input with Validation Source: https://github.com/yarlson/tap/blob/main/README.md Demonstrates how to use Tap's Text input component in Go, including setting a message, placeholder, default value, and custom validation logic for email format. ```go email := tap.Text(ctx, tap.TextOptions{ Message: "Enter your email:", Placeholder: "user@example.com", DefaultValue: "anonymous@example.com", Validate: func(input string) error { if !strings.Contains(input, "@") { return errors.New("Please enter a valid email") } return nil }, }) ``` -------------------------------- ### Go Multiple Selection Source: https://github.com/yarlson/tap/blob/main/README.md Demonstrates Tap's MultiSelect component in Go, allowing users to select multiple options from a list. It defines a slice of SelectOption and prints the user's selections. ```go languages := []tap.SelectOption[string]{ {Value: "go", Label: "Go"}, {Value: "python", Label: "Python"}, {Value: "javascript", Label: "JavaScript"}, } selected := tap.MultiSelect(ctx, tap.MultiSelectOptions[string]{ Message: "Which languages do you use?", Options: languages, }) fmt.Printf("You selected: %v\n", selected) ``` -------------------------------- ### Go Type-Safe Selection Source: https://github.com/yarlson/tap/blob/main/README.md Illustrates using Tap's Select component in Go for type-safe choices. It defines a custom type 'Environment' and provides options with labels and hints for user selection. ```go type Environment string environments := []tap.SelectOption[Environment]{ {Value: "dev", Label: "Development", Hint: "Local development"}, {Value: "staging", Label: "Staging", Hint: "Pre-production testing"}, {Value: "production", Label: "Production", Hint: "Live environment"}, } env := tap.Select(ctx, tap.SelectOptions[Environment]{ Message: "Choose deployment target:", Options: environments, }) // env is strongly typed as Environment ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.