123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/atridad/wrapped-cli/pkg/config"
|
|
)
|
|
|
|
type MenuScreen struct {
|
|
connections []config.Connection
|
|
selected int
|
|
newSelected bool
|
|
confirmed bool
|
|
}
|
|
|
|
func NewMenuScreen(cfg *config.Config) *MenuScreen {
|
|
return &MenuScreen{
|
|
connections: cfg.Connections,
|
|
selected: 0,
|
|
confirmed: false,
|
|
}
|
|
}
|
|
|
|
func (m *MenuScreen) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m *MenuScreen) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "ctrl+c":
|
|
return m, tea.Quit
|
|
case "up", "k":
|
|
if m.selected > 0 {
|
|
m.selected--
|
|
}
|
|
case "down", "j":
|
|
menuItems := len(m.connections) + 1
|
|
if m.selected < menuItems-1 {
|
|
m.selected++
|
|
}
|
|
case "enter":
|
|
if m.selected == len(m.connections) {
|
|
m.newSelected = true
|
|
} else if m.selected < len(m.connections) {
|
|
m.confirmed = true
|
|
}
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (m *MenuScreen) View() string {
|
|
var s string
|
|
s += lipgloss.NewStyle().
|
|
Foreground(lipgloss.Color("212")).
|
|
Bold(true).
|
|
Render("Gitea Wrapped") + "\n\n"
|
|
|
|
if len(m.connections) == 0 {
|
|
s += "No saved connections yet.\n\n"
|
|
|
|
newPrefix := " "
|
|
if m.selected == 0 {
|
|
newPrefix = "→ "
|
|
}
|
|
if m.selected == 1 {
|
|
}
|
|
|
|
s += fmt.Sprintf("%s+ New Connection\n", newPrefix)
|
|
|
|
s += "\n" + lipgloss.NewStyle().
|
|
Foreground(lipgloss.Color("8")).
|
|
Render("↑/↓ navigate | Enter to select | Ctrl+C to quit")
|
|
} else {
|
|
s += "Select a connection:\n\n"
|
|
|
|
for i, conn := range m.connections {
|
|
prefix := " "
|
|
if i == m.selected {
|
|
prefix = "→ "
|
|
}
|
|
s += fmt.Sprintf("%s%s (%s)\n", prefix, conn.Name, conn.URL)
|
|
}
|
|
|
|
newPrefix := " "
|
|
if m.selected == len(m.connections) {
|
|
newPrefix = "→ "
|
|
}
|
|
if m.selected == len(m.connections)+1 {
|
|
}
|
|
|
|
s += fmt.Sprintf("%s+ New Connection\n", newPrefix)
|
|
|
|
s += "\n" + lipgloss.NewStyle().
|
|
Foreground(lipgloss.Color("8")).
|
|
Render("↑/↓ navigate | Enter to select | Ctrl+C to quit")
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func (m *MenuScreen) IsNewConnection() bool {
|
|
return m.newSelected
|
|
}
|
|
|
|
func (m *MenuScreen) GetSelectedName() string {
|
|
if m.confirmed && m.selected < len(m.connections) {
|
|
return m.connections[m.selected].Name
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (m *MenuScreen) ResetSelection() {
|
|
m.newSelected = false
|
|
m.confirmed = false
|
|
m.selected = 0
|
|
}
|