6119
My
mysis / files
mysis / internal / tui / input.go
4.6 KB
1package tui 2 3import ( 4 "github.com/charmbracelet/bubbles/key" 5 "github.com/charmbracelet/bubbles/textinput" 6 tea "github.com/charmbracelet/bubbletea" 7 "github.com/charmbracelet/lipgloss" 8 "github.com/xonecas/mysis/internal/styles" 9) 10 11const maxHistorySize = 100 12 13// Input handles text input with history navigation. 14type Input struct { 15 textInput textinput.Model 16 history []string // Previous messages 17 historyIndex int // Current position in history (-1 = not browsing) 18 draft string // Saved draft when browsing history 19 width int 20} 21 22// NewInput creates a new input component. 23func NewInput(width int) Input { 24 ti := textinput.New() 25 ti.Placeholder = "Type message or command..." 26 ti.Prompt = "> " 27 ti.CharLimit = 2000 28 ti.Width = width - 6 // Account for: border (2) + padding (2) + prompt (2) 29 ti.Focus() 30 31 // Set text input colors to match our theme 32 ti.PromptStyle = InputPromptStyle 33 ti.TextStyle = InputTextStyle 34 ti.PlaceholderStyle = InputPlaceholderStyle 35 36 return Input{ 37 textInput: ti, 38 history: make([]string, 0, maxHistorySize), 39 historyIndex: -1, 40 width: width, 41 } 42} 43 44// SetWidth updates the input width. 45func (i *Input) SetWidth(width int) { 46 i.width = width 47 // Account for: border (2) + padding (2) + prompt (2) = 6 chars 48 i.textInput.Width = width - 6 49} 50 51// Focus focuses the input. 52func (i *Input) Focus() tea.Cmd { 53 return i.textInput.Focus() 54} 55 56// Blur blurs the input. 57func (i *Input) Blur() { 58 i.textInput.Blur() 59} 60 61// Value returns the current input value. 62func (i Input) Value() string { 63 return i.textInput.Value() 64} 65 66// SetValue sets the input value. 67func (i *Input) SetValue(value string) { 68 i.textInput.SetValue(value) 69} 70 71// Reset clears the input. 72func (i *Input) Reset() { 73 i.textInput.Reset() 74 i.historyIndex = -1 75 i.draft = "" 76} 77 78// AddToHistory adds a message to the history. 79func (i *Input) AddToHistory(message string) { 80 if message == "" { 81 return 82 } 83 84 // Avoid duplicate consecutive entries 85 if len(i.history) > 0 && i.history[len(i.history)-1] == message { 86 return 87 } 88 89 i.history = append(i.history, message) 90 91 // Trim if too large 92 if len(i.history) > maxHistorySize { 93 i.history = i.history[len(i.history)-maxHistorySize:] 94 } 95} 96 97// History key bindings 98var historyKeys = struct { 99 Up key.Binding 100 Down key.Binding 101}{ 102 Up: key.NewBinding(key.WithKeys("up")), 103 Down: key.NewBinding(key.WithKeys("down")), 104} 105 106// Update handles input updates. 107func (i Input) Update(msg tea.Msg) (Input, tea.Cmd) { 108 // Handle history navigation 109 if keyMsg, ok := msg.(tea.KeyMsg); ok { 110 switch { 111 case key.Matches(keyMsg, historyKeys.Up): 112 i.navigateHistory(1) // Go back in history 113 return i, nil 114 case key.Matches(keyMsg, historyKeys.Down): 115 i.navigateHistory(-1) // Go forward in history 116 return i, nil 117 } 118 } 119 120 var cmd tea.Cmd 121 i.textInput, cmd = i.textInput.Update(msg) 122 return i, cmd 123} 124 125// navigateHistory moves through the history. 126// direction: 1 = older (up), -1 = newer (down) 127func (i *Input) navigateHistory(direction int) { 128 if len(i.history) == 0 { 129 return 130 } 131 132 // Save current input as draft when starting to browse 133 if i.historyIndex == -1 && direction == 1 { 134 i.draft = i.textInput.Value() 135 } 136 137 newIndex := i.historyIndex + direction 138 139 // Clamp to valid range 140 if newIndex < -1 { 141 newIndex = -1 142 } 143 if newIndex >= len(i.history) { 144 newIndex = len(i.history) - 1 145 } 146 147 i.historyIndex = newIndex 148 149 // Update input value 150 if i.historyIndex == -1 { 151 // Back to draft 152 i.textInput.SetValue(i.draft) 153 i.textInput.CursorEnd() 154 } else { 155 // Show history item (most recent is at end of slice) 156 historyIdx := len(i.history) - 1 - i.historyIndex 157 i.textInput.SetValue(i.history[historyIdx]) 158 i.textInput.CursorEnd() 159 } 160} 161 162// View renders the input. 163func (i Input) View() string { 164 // Check if input is empty - render custom placeholder with background 165 // The textinput component's placeholder doesn't respect our background color 166 if i.textInput.Value() == "" { 167 // Render prompt 168 prompt := InputPromptStyle.Render(i.textInput.Prompt) 169 170 // Render placeholder with remaining width 171 placeholderStyle := lipgloss.NewStyle(). 172 Background(styles.ColorBg). 173 Foreground(styles.ColorMuted) 174 175 placeholder := prompt + placeholderStyle.Render(i.textInput.Placeholder) 176 return InputBorderStyle.Width(i.width).Render(placeholder) 177 } 178 179 // The textinput renders its own content when focused or has text 180 content := i.textInput.View() 181 182 // Wrap content with background style at full width 183 bgStyle := lipgloss.NewStyle(). 184 Background(styles.ColorBg). 185 Width(i.width - 2) 186 187 wrappedContent := bgStyle.Render(content) 188 189 // Apply border (top only) with full width 190 return InputBorderStyle.Width(i.width).Render(wrappedContent) 191}