xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / input.go

5.8 KB
1package tui
2
3import (
4	"fmt"
5
6	"github.com/charmbracelet/bubbles/key"
7	"github.com/charmbracelet/bubbles/textinput"
8	tea "github.com/charmbracelet/bubbletea"
9)
10
11// InputMode represents the current input mode.
12type InputMode int
13
14const (
15	InputModeNone InputMode = iota
16	InputModeBroadcast
17	InputModeMessage
18	InputModeNewMysis
19	InputModeConfigProvider
20	InputModeConfigModel
21)
22
23const maxHistorySize = 100
24
25// InputModel handles text input for messages.
26type InputModel struct {
27	textInput    textinput.Model
28	mode         InputMode
29	targetID     string   // For direct messages
30	history      []string // Previous messages
31	historyIndex int      // Current position in history (-1 = not browsing)
32	draft        string   // Saved draft when browsing history
33}
34
35// NewInputModel creates a new input model.
36func NewInputModel() InputModel {
37	ti := textinput.New()
38	ti.Placeholder = "Type a message..."
39	ti.CharLimit = 1000
40	ti.Width = 60
41
42	return InputModel{
43		textInput:    ti,
44		mode:         InputModeNone,
45		history:      make([]string, 0, maxHistorySize),
46		historyIndex: -1,
47	}
48}
49
50// SetMode sets the input mode and updates the prompt.
51func (m *InputModel) SetMode(mode InputMode, targetID string) {
52	m.mode = mode
53	m.targetID = targetID
54	m.textInput.Reset()
55
56	switch mode {
57	case InputModeBroadcast:
58		m.textInput.Placeholder = "Broadcast message to all myses..."
59		m.textInput.Prompt = inputPromptStyle.Render("⬧") + "  "
60	case InputModeMessage:
61		m.textInput.Placeholder = "Message to mysis..."
62		m.textInput.Prompt = inputPromptStyle.Render("⬥") + "  "
63	case InputModeNewMysis:
64		m.textInput.Placeholder = "Enter mysis name..."
65		m.textInput.Prompt = inputPromptStyle.Render("⬡") + "  "
66	case InputModeConfigProvider:
67		m.textInput.Placeholder = "Enter provider (ollama-qwen/zen-nano/zen-pickle)..."
68		m.textInput.Prompt = inputPromptStyle.Render("⚙") + "  "
69	case InputModeConfigModel:
70		m.textInput.Placeholder = "Enter model name..."
71		m.textInput.Prompt = inputPromptStyle.Render("cfg") + "  "
72	default:
73		m.textInput.Placeholder = ""
74		m.textInput.Prompt = ""
75	}
76
77	if mode != InputModeNone {
78		m.textInput.Focus()
79	} else {
80		m.textInput.Blur()
81	}
82}
83
84// Mode returns the current input mode.
85func (m InputModel) Mode() InputMode {
86	return m.mode
87}
88
89// TargetID returns the target mysis ID for direct messages.
90func (m InputModel) TargetID() string {
91	return m.targetID
92}
93
94// Value returns the current input value.
95func (m InputModel) Value() string {
96	return m.textInput.Value()
97}
98
99// IsActive returns true if input is active.
100func (m InputModel) IsActive() bool {
101	return m.mode != InputModeNone
102}
103
104// Focus returns the command to start the text input cursor.
105func (m InputModel) Focus() tea.Cmd {
106	return textinput.Blink
107}
108
109// History key bindings
110var historyKeys = struct {
111	Up   key.Binding
112	Down key.Binding
113}{
114	Up:   key.NewBinding(key.WithKeys("up")),
115	Down: key.NewBinding(key.WithKeys("down")),
116}
117
118// Update handles input updates.
119func (m InputModel) Update(msg tea.Msg) (InputModel, tea.Cmd) {
120	// Handle history navigation for message modes
121	if keyMsg, ok := msg.(tea.KeyMsg); ok {
122		if m.mode == InputModeBroadcast || m.mode == InputModeMessage {
123			switch {
124			case key.Matches(keyMsg, historyKeys.Up):
125				m.navigateHistory(1) // Go back in history
126				return m, nil
127			case key.Matches(keyMsg, historyKeys.Down):
128				m.navigateHistory(-1) // Go forward in history
129				return m, nil
130			}
131		}
132	}
133
134	var cmd tea.Cmd
135	m.textInput, cmd = m.textInput.Update(msg)
136	return m, cmd
137}
138
139// navigateHistory moves through the history.
140// direction: 1 = older (up), -1 = newer (down)
141func (m *InputModel) navigateHistory(direction int) {
142	if len(m.history) == 0 {
143		return
144	}
145
146	// Save current input as draft when starting to browse
147	if m.historyIndex == -1 && direction == 1 {
148		m.draft = m.textInput.Value()
149	}
150
151	newIndex := m.historyIndex + direction
152
153	// Clamp to valid range
154	if newIndex < -1 {
155		newIndex = -1
156	}
157	if newIndex >= len(m.history) {
158		newIndex = len(m.history) - 1
159	}
160
161	m.historyIndex = newIndex
162
163	// Update input value
164	if m.historyIndex == -1 {
165		// Back to draft
166		m.textInput.SetValue(m.draft)
167		m.textInput.CursorEnd()
168	} else {
169		// Show history item (most recent is at end of slice)
170		historyIdx := len(m.history) - 1 - m.historyIndex
171		m.textInput.SetValue(m.history[historyIdx])
172		m.textInput.CursorEnd()
173	}
174}
175
176// View renders the input.
177func (m InputModel) View() string {
178	if m.mode == InputModeNone {
179		return ""
180	}
181	return inputStyle.Render(m.textInput.View())
182}
183
184// ViewAlways renders the input bar, showing placeholder when not active.
185func (m InputModel) ViewAlways(width int, sending bool, sendingLabel, spinnerView string) string {
186	// Ensure minimum width to prevent negative or zero width
187	if width < 10 {
188		width = 10
189	}
190
191	if m.mode != InputModeNone {
192		// Active - show the actual input
193		return inputStyle.Width(width - 2).Render(m.textInput.View())
194	}
195	if sending {
196		indicator := fmt.Sprintf("%s %s", spinnerView, sendingLabel)
197		return inputStyle.Width(width - 2).Render(indicator)
198	}
199	// Inactive - show placeholder prompt
200	placeholder := dimmedStyle.Render("Press 'm' to message, 'b' to broadcast...")
201	return inputStyle.Width(width - 2).Render(placeholder)
202}
203
204// Reset clears the input.
205func (m *InputModel) Reset() {
206	m.textInput.Reset()
207	m.mode = InputModeNone
208	m.targetID = ""
209	m.historyIndex = -1
210	m.draft = ""
211	m.textInput.Blur()
212}
213
214// AddToHistory adds a message to the history.
215func (m *InputModel) AddToHistory(message string) {
216	if message == "" {
217		return
218	}
219
220	// Avoid duplicate consecutive entries
221	if len(m.history) > 0 && m.history[len(m.history)-1] == message {
222		return
223	}
224
225	m.history = append(m.history, message)
226
227	// Trim if too large
228	if len(m.history) > maxHistorySize {
229		m.history = m.history[len(m.history)-maxHistorySize:]
230	}
231}
232
233// SetWidth sets the input width.
234func (m *InputModel) SetWidth(width int) {
235	m.textInput.Width = width - 4 // Account for padding/border
236}