fetch_ml/cmd/tui/internal/controller/settings.go
Jeremie Fraeys 803677be57 feat: implement Go backend with comprehensive API and internal packages
- Add API server with WebSocket support and REST endpoints
- Implement authentication system with API keys and permissions
- Add task queue system with Redis backend and error handling
- Include storage layer with database migrations and schemas
- Add comprehensive logging, metrics, and telemetry
- Implement security middleware and network utilities
- Add experiment management and container orchestration
- Include configuration management with smart defaults
2025-12-04 16:53:53 -05:00

126 lines
3.4 KiB
Go

package controller
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/jfraeys/fetch_ml/cmd/tui/internal/model"
)
// Settings-related command factories and handlers
func (c *Controller) updateSettingsContent(m model.State) tea.Cmd {
var content strings.Builder
// API Key Status section
statusStyle := lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
BorderForeground(lipgloss.AdaptiveColor{Light: "#d8dee9", Dark: "#4c566a"}). // borderfg
Padding(0, 1).
Width(m.SettingsView.Width - 4)
if m.SettingsIndex == 0 {
statusStyle = statusStyle.
BorderForeground(lipgloss.AdaptiveColor{Light: "#3498db", Dark: "#7aa2f7"}) // activeBorderfg
}
statusContent := fmt.Sprintf("%s API Key Status\n%s",
getSettingsIndicator(m, 0),
getAPIKeyStatus(m))
content.WriteString(statusStyle.Render(statusContent))
content.WriteString("\n")
// API Key Input section
inputStyle := lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
BorderForeground(lipgloss.AdaptiveColor{Light: "#d8dee9", Dark: "#4c566a"}).
Padding(0, 1).
Width(m.SettingsView.Width - 4)
if m.SettingsIndex == 1 {
inputStyle = inputStyle.
BorderForeground(lipgloss.AdaptiveColor{Light: "#3498db", Dark: "#7aa2f7"})
}
inputContent := fmt.Sprintf("%s Enter New API Key\n%s",
getSettingsIndicator(m, 1),
m.ApiKeyInput.View())
content.WriteString(inputStyle.Render(inputContent))
content.WriteString("\n")
// Save Configuration section
saveStyle := lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
BorderForeground(lipgloss.AdaptiveColor{Light: "#d8dee9", Dark: "#4c566a"}).
Padding(0, 1).
Width(m.SettingsView.Width - 4)
if m.SettingsIndex == 2 {
saveStyle = saveStyle.
BorderForeground(lipgloss.AdaptiveColor{Light: "#3498db", Dark: "#7aa2f7"})
}
saveContent := fmt.Sprintf("%s Save Configuration\n[Enter]",
getSettingsIndicator(m, 2))
content.WriteString(saveStyle.Render(saveContent))
content.WriteString("\n")
// Current API Key display
keyStyle := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#666", Dark: "#999"}).
Italic(true)
keyContent := fmt.Sprintf("Current API Key: %s", maskAPIKey(m.ApiKey))
content.WriteString(keyStyle.Render(keyContent))
return func() tea.Msg { return SettingsContentMsg(content.String()) }
}
func (c *Controller) handleSettingsAction(m *model.State) tea.Cmd {
switch m.SettingsIndex {
case 0: // API Key Status - do nothing
return nil
case 1: // Enter New API Key - do nothing, Enter key disabled
return nil
case 2: // Save Configuration
if m.ApiKeyInput.Value() != "" {
m.ApiKey = m.ApiKeyInput.Value()
m.ApiKeyInput.SetValue("")
m.Status = "Configuration saved (in-memory only)"
return c.updateSettingsContent(*m)
} else if m.ApiKey != "" {
m.Status = "Configuration saved (in-memory only)"
} else {
m.ErrorMsg = "No API key to save"
}
}
return nil
}
// Helper functions for settings
func getSettingsIndicator(m model.State, index int) string {
if index == m.SettingsIndex {
return "▶"
}
return " "
}
func getAPIKeyStatus(m model.State) string {
if m.ApiKey != "" {
return "✓ API Key is set\n" + maskAPIKey(m.ApiKey)
}
return "⚠ No API Key configured"
}
func maskAPIKey(key string) string {
if key == "" {
return "(not set)"
}
if len(key) <= 8 {
return "****"
}
return key[:4] + "****" + key[len(key)-4:]
}