GoSetup-bubble/setup/list.go

141 lines
3.7 KiB
Go
Raw Normal View History

2025-12-26 17:45:37 +05:00
package setup
import (
"bubble/setup/ui"
"bubble/types"
"fmt"
"github.com/charmbracelet/bubbles/list"
2025-12-26 17:45:37 +05:00
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ListApplicationItem struct {
Application types.PackageApplication
2025-12-26 17:45:37 +05:00
}
func (i ListApplicationItem) Title() string {
return fmt.Sprintf("%s, v%s", i.Application.Name, i.Application.Version)
}
func (i ListApplicationItem) Description() string {
return fmt.Sprintf("%s", i.Application.Description)
}
func (i ListApplicationItem) FilterValue() string {
return i.Application.Name
}
type ListApplicationsModel struct {
List list.Model
width int
height int
}
func NewListApplicationsModel(applications []types.PackageApplication, width, height int) *ListApplicationsModel {
items := make([]list.Item, len(applications))
2025-12-26 17:45:37 +05:00
for i, app := range applications {
items[i] = ListApplicationItem{app}
2025-12-26 17:45:37 +05:00
}
delegate := list.NewDefaultDelegate()
delegate.Styles.SelectedTitle = ui.ListSelectedStyle
delegate.Styles.SelectedDesc = ui.ListSelectedStyle
delegate.ShowDescription = false
l := list.New(items, delegate, width, height)
l.Title = "Выберите приложение для настройки"
l.Styles.Title = ui.ListTitleStyle
return &ListApplicationsModel{
List: l,
width: width,
height: height,
2025-12-26 17:45:37 +05:00
}
}
func (m *ListApplicationsModel) Init() tea.Cmd {
2025-12-26 17:45:37 +05:00
return nil
}
func (m *ListApplicationsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2025-12-26 17:45:37 +05:00
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
return m, nil
2025-12-26 17:45:37 +05:00
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "enter":
selected := m.List.SelectedItem().(ListApplicationItem)
2025-12-26 17:45:37 +05:00
return m, func() tea.Msg {
return types.ShowModalMsg{
Type: types.ModalConfirm,
Title: "Начать установку",
Message: fmt.Sprintf("Вы хотит настроить приложение:\n\n%s\n\nПродолжить?", selected.Title()),
ModalButtons: []types.ModalButton{
{Text: "Да", Value: true},
{Text: "Нет", Value: false},
},
OnResult: func(result any) tea.Cmd {
if result == true {
return func() tea.Msg {
return types.SelectedAppMsg{App: selected.Application}
}
}
return nil
},
}
2025-12-26 17:45:37 +05:00
}
}
}
m.List, cmd = m.List.Update(msg)
2025-12-26 17:45:37 +05:00
return m, cmd
}
func (m *ListApplicationsModel) View() string {
contentWidth := ui.ContentWidth(m.width)
listWidth := int(float64(contentWidth) * ui.ListWithRatio)
m.List.SetSize(listWidth-6, m.height-10)
listView := m.List.View()
var desc string
if sel := m.List.SelectedItem(); sel != nil {
desc = sel.(ListApplicationItem).Description()
2025-12-26 17:45:37 +05:00
}
descView := ui.ListDescriptionStyle.Width(contentWidth - listWidth - 10).Render(desc)
body := lipgloss.JoinHorizontal(lipgloss.Top, listView, " ", descView)
container := ui.ListContainer.Width(contentWidth).Render(body)
return lipgloss.Place(m.width, m.height,
lipgloss.Center, lipgloss.Center,
container,
lipgloss.WithWhitespaceChars("░"),
lipgloss.WithWhitespaceForeground(ui.ColorGrey),
)
//var desc string
//if selected := m.List.SelectedItem(); selected != nil {
// desc = selected.(ListApplicationItem).Description()
//}
//descView := ui.ListDescriptionStyle.
// Width(contentWidth/2 - 4).
// Render(desc)
//
//body := lipgloss.JoinHorizontal(lipgloss.Top, listView, " ", descView)
//
//container := ui.ListStyle.
// Width(contentWidth).
// Render(body)
//
//return lipgloss.Place(m.width, m.height,
// lipgloss.Center, lipgloss.Center,
// container,
// lipgloss.WithWhitespaceChars("░"),
// lipgloss.WithWhitespaceForeground(ui.ColorGrey),
//)
2025-12-26 17:45:37 +05:00
}