obm/internal/prompt/prompt.go

60 lines
1.5 KiB
Go

// Package prompt handles interactive user prompts and confirmations.
package prompt
import (
"bufio"
"fmt"
"os"
"strings"
)
// Confirm asks the user a yes/no question and returns true for yes.
func Confirm(message string) bool {
fmt.Printf("%s [y/N]: ", message)
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return false
}
return strings.TrimSpace(strings.ToLower(input)) == "y"
}
// PromptString asks the user for a string input with the given label.
func PromptString(label string) string {
fmt.Printf("%s: ", label)
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return ""
}
return strings.TrimSpace(input)
}
// SummaryLine prints a single line in the summary format.
func SummaryLine(key, value string) {
fmt.Printf(" %-20s %s\n", key+":", value)
}
// SummarySection prints a section header in the summary format.
func SummarySection(title string) {
fmt.Printf("\n[%s]\n", title)
}
// SummaryDisplay prints a formatted summary of key-value pairs.
// The pairs are printed in order, with sections delimited by empty keys.
func SummaryDisplay(title string, sections map[string][]Field) bool {
fmt.Printf("\n=== %s ===\n", title)
for sectionName, fields := range sections {
SummarySection(sectionName)
for _, field := range fields {
SummaryLine(field.Key, field.Value)
}
}
return Confirm("\nProceed?")
}
// Field represents a key-value pair for summary display.
type Field struct {
Key string
Value string
}